@@ -276,6 +276,13 @@ void ZPhysicalMemoryManager::try_enable_uncommit(size_t min_capacity, size_t max
276276}
277277
278278void ZPhysicalMemoryManager::nmt_commit (zoffset offset, size_t size) const {
279+ // NMT expects a 1-to-1 mapping between virtual and physical memory.
280+ // ZGC can temporarily have multiple virtual addresses pointing to
281+ // the same physical memory.
282+ //
283+ // When this function is called we don't know where in the virtual memory
284+ // this physical memory will be mapped. So we fake that the virtual memory
285+ // address is the heap base + the given offset.
279286 const zaddress addr = ZOffset::address (offset);
280287 MemTracker::record_virtual_memory_commit ((void *)untype (addr), size, CALLER_PC);
281288}
@@ -320,6 +327,11 @@ bool ZPhysicalMemoryManager::commit(ZPhysicalMemory& pmem) {
320327
321328 // Commit segment
322329 const size_t committed = _backing.commit (segment.start (), segment.size ());
330+
331+ // Register with NMT
332+ nmt_commit (segment.start (), committed);
333+
334+ // Register committed segment
323335 if (!pmem.commit_segment (i, committed)) {
324336 // Failed or partially failed
325337 return false ;
@@ -341,6 +353,11 @@ bool ZPhysicalMemoryManager::uncommit(ZPhysicalMemory& pmem) {
341353
342354 // Uncommit segment
343355 const size_t uncommitted = _backing.uncommit (segment.start (), segment.size ());
356+
357+ // Unregister with NMT
358+ nmt_uncommit (segment.start (), uncommitted);
359+
360+ // Deregister uncommitted segment
344361 if (!pmem.uncommit_segment (i, uncommitted)) {
345362 // Failed or partially failed
346363 return false ;
@@ -351,12 +368,16 @@ bool ZPhysicalMemoryManager::uncommit(ZPhysicalMemory& pmem) {
351368 return true ;
352369}
353370
354- void ZPhysicalMemoryManager::pretouch_view (zaddress addr, size_t size) const {
371+ void ZPhysicalMemoryManager::pretouch (zoffset offset, size_t size) const {
372+ const uintptr_t addr = untype (ZOffset::address (offset));
355373 const size_t page_size = ZLargePages::is_explicit () ? ZGranuleSize : os::vm_page_size ();
356- os::pretouch_memory ((void *)untype ( addr) , (void *)(untype ( addr) + size), page_size);
374+ os::pretouch_memory ((void *)addr, (void *)(addr + size), page_size);
357375}
358376
359- void ZPhysicalMemoryManager::map_view (zaddress_unsafe addr, const ZPhysicalMemory& pmem) const {
377+ // Map virtual memory to physcial memory
378+ void ZPhysicalMemoryManager::map (zoffset offset, const ZPhysicalMemory& pmem) const {
379+ const zaddress_unsafe addr = ZOffset::address_unsafe (offset);
380+
360381 size_t size = 0 ;
361382
362383 // Map segments
@@ -375,27 +396,9 @@ void ZPhysicalMemoryManager::map_view(zaddress_unsafe addr, const ZPhysicalMemor
375396 }
376397}
377398
378- void ZPhysicalMemoryManager::unmap_view (zaddress_unsafe addr, size_t size) const {
379- _backing.unmap (addr, size);
380- }
381-
382- void ZPhysicalMemoryManager::pretouch (zoffset offset, size_t size) const {
383- // Pre-touch all views
384- pretouch_view (ZOffset::address (offset), size);
385- }
386-
387- void ZPhysicalMemoryManager::map (zoffset offset, const ZPhysicalMemory& pmem) const {
388- const size_t size = pmem.size ();
389-
390- // Map all views
391- map_view (ZOffset::address_unsafe (offset), pmem);
392-
393- nmt_commit (offset, size);
394- }
395-
399+ // Unmap virtual memory from physical memory
396400void ZPhysicalMemoryManager::unmap (zoffset offset, size_t size) const {
397- nmt_uncommit (offset, size );
401+ const zaddress_unsafe addr = ZOffset::address_unsafe (offset);
398402
399- // Unmap all views
400- unmap_view (ZOffset::address_unsafe (offset), size);
403+ _backing.unmap (addr, size);
401404}
0 commit comments