@@ -385,7 +385,7 @@ bool ZPhysicalMemoryBacking::tmpfs_supports_transparent_huge_pages() const {
385385 return access (ZFILENAME_SHMEM_ENABLED, R_OK) == 0 ;
386386}
387387
388- ZErrno ZPhysicalMemoryBacking::fallocate_compat_mmap_hugetlbfs (zbacking_offset offset, size_t length, bool touch ) const {
388+ ZErrno ZPhysicalMemoryBacking::fallocate_compat_mmap_hugetlbfs (zbacking_offset offset, size_t length) const {
389389 // On hugetlbfs, mapping a file segment will fail immediately, without
390390 // the need to touch the mapped pages first, if there aren't enough huge
391391 // pages available to back the mapping.
@@ -398,11 +398,9 @@ ZErrno ZPhysicalMemoryBacking::fallocate_compat_mmap_hugetlbfs(zbacking_offset o
398398 // Once mapped, the huge pages are only reserved. We need to touch them
399399 // to associate them with the file segment. Note that we can not punch
400400 // hole in file segments which only have reserved pages.
401- if (touch) {
402- char * const start = (char *)addr;
403- char * const end = start + length;
404- os::pretouch_memory (start, end, _block_size);
405- }
401+
402+ // Touch the mapping (safely) to make sure it's backed by memory
403+ const bool backed = os::Linux::safe_fault_memory (addr, length, ZGranuleSize);
406404
407405 // Unmap again. From now on, the huge pages that were mapped are allocated
408406 // to this file. There's no risk of getting a SIGBUS when mapping and
@@ -412,28 +410,8 @@ ZErrno ZPhysicalMemoryBacking::fallocate_compat_mmap_hugetlbfs(zbacking_offset o
412410 return errno;
413411 }
414412
415- // Success
416- return 0 ;
417- }
418-
419- static bool safe_touch_mapping (void * addr, size_t length, size_t page_size) {
420- char * const start = (char *)addr;
421- char * const end = start + length;
422-
423- // Touching a mapping that can't be backed by memory will generate a
424- // SIGBUS. By using SafeFetch32 any SIGBUS will be safely caught and
425- // handled. On tmpfs, doing a fetch (rather than a store) is enough
426- // to cause backing pages to be allocated (there's no zero-page to
427- // worry about).
428- for (char *p = start; p < end; p += page_size) {
429- if (SafeFetch32 ((int *)p, -1 ) == -1 ) {
430- // Failed
431- return false ;
432- }
433- }
434-
435- // Success
436- return true ;
413+ // Success?
414+ return backed ? 0 : errno;
437415}
438416
439417ZErrno ZPhysicalMemoryBacking::fallocate_compat_mmap_tmpfs (zbacking_offset offset, size_t length) const {
@@ -451,7 +429,7 @@ ZErrno ZPhysicalMemoryBacking::fallocate_compat_mmap_tmpfs(zbacking_offset offse
451429 }
452430
453431 // Touch the mapping (safely) to make sure it's backed by memory
454- const bool backed = safe_touch_mapping (addr, length, _block_size);
432+ const bool backed = os::Linux::safe_fault_memory (addr, length, _block_size);
455433
456434 // Unmap again. If successfully touched, the backing memory will
457435 // be allocated to this file. There's no risk of getting a SIGBUS
@@ -461,7 +439,7 @@ ZErrno ZPhysicalMemoryBacking::fallocate_compat_mmap_tmpfs(zbacking_offset offse
461439 return errno;
462440 }
463441
464- // Success
442+ // Success?
465443 return backed ? 0 : ENOMEM;
466444}
467445
@@ -486,7 +464,7 @@ ZErrno ZPhysicalMemoryBacking::fallocate_fill_hole_compat(zbacking_offset offset
486464 // mmap/munmap (for hugetlbfs and tmpfs with transparent huge pages) or pwrite
487465 // (for tmpfs without transparent huge pages and other filesystem types).
488466 if (ZLargePages::is_explicit ()) {
489- return fallocate_compat_mmap_hugetlbfs (offset, length, false /* touch */ );
467+ return fallocate_compat_mmap_hugetlbfs (offset, length);
490468 } else if (ZLargePages::is_transparent ()) {
491469 return fallocate_compat_mmap_tmpfs (offset, length);
492470 } else {
@@ -534,18 +512,6 @@ ZErrno ZPhysicalMemoryBacking::fallocate_fill_hole(zbacking_offset offset, size_
534512}
535513
536514ZErrno ZPhysicalMemoryBacking::fallocate_punch_hole (zbacking_offset offset, size_t length) const {
537- if (ZLargePages::is_explicit ()) {
538- // We can only punch hole in pages that have been touched. Non-touched
539- // pages are only reserved, and not associated with any specific file
540- // segment. We don't know which pages have been previously touched, so
541- // we always touch them here to guarantee that we can punch hole.
542- const ZErrno err = fallocate_compat_mmap_hugetlbfs (offset, length, true /* touch */ );
543- if (err) {
544- // Failed
545- return err;
546- }
547- }
548-
549515 const int mode = FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE;
550516 if (ZSyscall::fallocate (_fd, mode, untype (offset), length) == -1 ) {
551517 // Failed
@@ -665,9 +631,7 @@ size_t ZPhysicalMemoryBacking::commit_default(zbacking_offset offset, size_t len
665631}
666632
667633size_t ZPhysicalMemoryBacking::commit (zbacking_offset offset, size_t length, uint32_t numa_id) const {
668- if (ZNUMA::is_enabled () && !ZLargePages::is_explicit ()) {
669- // The memory is required to be preferred at the time it is paged in. As a
670- // consequence we must prefer the memory when committing non-large pages.
634+ if (ZNUMA::is_enabled ()) {
671635 return commit_numa_preferred (offset, length, numa_id);
672636 }
673637
0 commit comments