c17171
From bcab8c3cd877506de75f50e0f9ed98827ed554b0 Mon Sep 17 00:00:00 2001
c17171
From: Peter Zhu <peter@peterzhu.ca>
c17171
Date: Tue, 23 Feb 2021 16:28:56 -0500
c17171
Subject: [PATCH] Use mmap for allocating heap pages
c17171
c17171
---
c17171
 configure.ac                 |  16 ++++
c17171
 gc.c                         | 149 ++++++++++++++++++++++++++---------
c17171
 test/ruby/test_gc_compact.rb |  41 ++++++----
c17171
 3 files changed, 155 insertions(+), 51 deletions(-)
c17171
c17171
diff --git a/configure.ac b/configure.ac
c17171
index 2dcebdde9f..b1b190004d 100644
c17171
--- a/configure.ac
c17171
+++ b/configure.ac
c17171
@@ -1944,6 +1944,7 @@ AC_CHECK_FUNCS(memmem)
c17171
 AC_CHECK_FUNCS(mkfifo)
c17171
 AC_CHECK_FUNCS(mknod)
c17171
 AC_CHECK_FUNCS(mktime)
c17171
+AC_CHECK_FUNCS(mmap)
c17171
 AC_CHECK_FUNCS(openat)
c17171
 AC_CHECK_FUNCS(pipe2)
c17171
 AC_CHECK_FUNCS(poll)
c17171
@@ -2666,6 +2667,21 @@ main(int argc, char *argv[])
c17171
 	rb_cv_fork_with_pthread=yes)])
c17171
     test x$rb_cv_fork_with_pthread = xyes || AC_DEFINE(CANNOT_FORK_WITH_PTHREAD)
c17171
 ])
c17171
+
c17171
+AC_CHECK_HEADERS([sys/user.h])
c17171
+AS_IF([test "x$ac_cv_func_mmap" = xyes], [
c17171
+    AC_CACHE_CHECK([whether PAGE_SIZE is compile-time const], rb_cv_const_page_size,
c17171
+	[malloc_headers=`sed -n '/MALLOC_HEADERS_BEGIN/,/MALLOC_HEADERS_END/p' ${srcdir}/gc.c`
c17171
+	AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[$malloc_headers
c17171
+            typedef char conftest_page[PAGE_SIZE];
c17171
+        ]], [[]])],
c17171
+        [rb_cv_const_page_size=yes],
c17171
+        [rb_cv_const_page_size=no])])
c17171
+])
c17171
+AS_IF([test "x$rb_cv_const_page_size" = xyes],
c17171
+    [AC_DEFINE(HAVE_CONST_PAGE_SIZE, 1)],
c17171
+    [AC_DEFINE(HAVE_CONST_PAGE_SIZE, 0)]
c17171
+)
c17171
 }
c17171
 
c17171
 : "runtime section" && {
c17171
diff --git a/gc.c b/gc.c
c17171
index f6acf3e117..6f8e5f242d 100644
c17171
--- a/gc.c
c17171
+++ b/gc.c
c17171
@@ -32,6 +32,7 @@
c17171
 #include <stdarg.h>
c17171
 #include <stdio.h>
c17171
 
c17171
+/* MALLOC_HEADERS_BEGIN */
c17171
 #ifndef HAVE_MALLOC_USABLE_SIZE
c17171
 # ifdef _WIN32
c17171
 #  define HAVE_MALLOC_USABLE_SIZE
c17171
@@ -54,6 +55,12 @@
c17171
 # endif
c17171
 #endif
c17171
 
c17171
+#if !defined(PAGE_SIZE) && defined(HAVE_SYS_USER_H)
c17171
+/* LIST_HEAD conflicts with sys/queue.h on macOS */
c17171
+# include <sys/user.h>
c17171
+#endif
c17171
+/* MALLOC_HEADERS_END */
c17171
+
c17171
 #ifdef HAVE_SYS_TIME_H
c17171
 # include <sys/time.h>
c17171
 #endif
c17171
@@ -821,6 +828,25 @@ enum {
c17171
     HEAP_PAGE_BITMAP_SIZE = (BITS_SIZE * HEAP_PAGE_BITMAP_LIMIT),
c17171
     HEAP_PAGE_BITMAP_PLANES = 4 /* RGENGC: mark, unprotected, uncollectible, marking */
c17171
 };
c17171
+#define HEAP_PAGE_ALIGN (1 << HEAP_PAGE_ALIGN_LOG)
c17171
+#define HEAP_PAGE_SIZE HEAP_PAGE_ALIGN
c17171
+
c17171
+#ifdef HAVE_MMAP
c17171
+# if HAVE_CONST_PAGE_SIZE
c17171
+/* If we have the HEAP_PAGE and it is a constant, then we can directly use it. */
c17171
+static const bool USE_MMAP_ALIGNED_ALLOC = (PAGE_SIZE <= HEAP_PAGE_SIZE);
c17171
+# elif defined(PAGE_MAX_SIZE) && (PAGE_MAX_SIZE <= HEAP_PAGE_SIZE)
c17171
+/* PAGE_SIZE <= HEAP_PAGE_SIZE */
c17171
+static const bool USE_MMAP_ALIGNED_ALLOC = true;
c17171
+# else
c17171
+/* Otherwise, fall back to determining if we can use mmap during runtime. */
c17171
+#  define USE_MMAP_ALIGNED_ALLOC (use_mmap_aligned_alloc != false)
c17171
+
c17171
+static bool use_mmap_aligned_alloc;
c17171
+# endif
c17171
+#elif !defined(__MINGW32__) && !defined(_WIN32)
c17171
+static const bool USE_MMAP_ALIGNED_ALLOC = false;
c17171
+#endif
c17171
 
c17171
 struct heap_page {
c17171
     short total_slots;
c17171
@@ -1760,14 +1786,14 @@ heap_unlink_page(rb_objspace_t *objspace, rb_heap_t *heap, struct heap_page *pag
c17171
     heap->total_slots -= page->total_slots;
c17171
 }
c17171
 
c17171
-static void rb_aligned_free(void *ptr);
c17171
+static void rb_aligned_free(void *ptr, size_t size);
c17171
 
c17171
 static void
c17171
 heap_page_free(rb_objspace_t *objspace, struct heap_page *page)
c17171
 {
c17171
     heap_allocated_pages--;
c17171
     objspace->profile.total_freed_pages++;
c17171
-    rb_aligned_free(GET_PAGE_BODY(page->start));
c17171
+    rb_aligned_free(GET_PAGE_BODY(page->start), HEAP_PAGE_SIZE);
c17171
     free(page);
c17171
 }
c17171
 
c17171
@@ -1819,7 +1845,7 @@ heap_page_allocate(rb_objspace_t *objspace)
c17171
     /* assign heap_page entry */
c17171
     page = calloc1(sizeof(struct heap_page));
c17171
     if (page == 0) {
c17171
-        rb_aligned_free(page_body);
c17171
+        rb_aligned_free(page_body, HEAP_PAGE_SIZE);
c17171
 	rb_memerror();
c17171
     }
c17171
 
c17171
@@ -3159,15 +3185,18 @@ Init_heap(void)
c17171
 {
c17171
     rb_objspace_t *objspace = &rb_objspace;
c17171
 
c17171
-#if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
c17171
-    /* If Ruby's heap pages are not a multiple of the system page size, we
c17171
-     * cannot use mprotect for the read barrier, so we must disable automatic
c17171
-     * compaction. */
c17171
-    int pagesize;
c17171
-    pagesize = (int)sysconf(_SC_PAGE_SIZE);
c17171
-    if ((HEAP_PAGE_SIZE % pagesize) != 0) {
c17171
-        ruby_enable_autocompact = 0;
c17171
-    }
c17171
+#if defined(HAVE_MMAP) && !HAVE_CONST_PAGE_SIZE && !defined(PAGE_MAX_SIZE)
c17171
+    /* Need to determine if we can use mmap at runtime. */
c17171
+# ifdef PAGE_SIZE
c17171
+    /* If the PAGE_SIZE macro can be used. */
c17171
+    use_mmap_aligned_alloc = PAGE_SIZE <= HEAP_PAGE_SIZE;
c17171
+# elif defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
c17171
+    /* If we can use sysconf to determine the page size. */
c17171
+    use_mmap_aligned_alloc = sysconf(_SC_PAGE_SIZE) <= HEAP_PAGE_SIZE;
c17171
+# else
c17171
+    /* Otherwise we can't determine the system page size, so don't use mmap. */
c17171
+    use_mmap_aligned_alloc = FALSE;
c17171
+# endif
c17171
 #endif
c17171
 
c17171
     objspace->next_object_id = INT2FIX(OBJ_ID_INITIAL);
c17171
@@ -8533,6 +8562,14 @@ gc_start_internal(rb_execution_context_t *ec, VALUE self, VALUE full_mark, VALUE
c17171
 
c17171
     /* For now, compact implies full mark / sweep, so ignore other flags */
c17171
     if (RTEST(compact)) {
c17171
+        /* If not MinGW, Windows, or does not have mmap, we cannot use mprotect for
c17171
+         * the read barrier, so we must disable compaction. */
c17171
+#if !defined(__MINGW32__) && !defined(_WIN32)
c17171
+        if (!USE_MMAP_ALIGNED_ALLOC) {
c17171
+            rb_raise(rb_eNotImpError, "Compaction isn't available on this platform");
c17171
+        }
c17171
+#endif
c17171
+
c17171
         reason |= GPR_FLAG_COMPACT;
c17171
     } else {
c17171
         if (!RTEST(full_mark))       reason &= ~GPR_FLAG_FULL_MARK;
c17171
@@ -9944,16 +9981,14 @@ gc_disable(rb_execution_context_t *ec, VALUE _)
c17171
 static VALUE
c17171
 gc_set_auto_compact(rb_execution_context_t *ec, VALUE _, VALUE v)
c17171
 {
c17171
-#if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
c17171
-    /* If Ruby's heap pages are not a multiple of the system page size, we
c17171
-     * cannot use mprotect for the read barrier, so we must disable automatic
c17171
-     * compaction. */
c17171
-    int pagesize;
c17171
-    pagesize = (int)sysconf(_SC_PAGE_SIZE);
c17171
-    if ((HEAP_PAGE_SIZE % pagesize) != 0) {
c17171
+    /* If not MinGW, Windows, or does not have mmap, we cannot use mprotect for
c17171
+     * the read barrier, so we must disable automatic compaction. */
c17171
+#if !defined(__MINGW32__) && !defined(_WIN32)
c17171
+    if (!USE_MMAP_ALIGNED_ALLOC) {
c17171
         rb_raise(rb_eNotImpError, "Automatic compaction isn't available on this platform");
c17171
     }
c17171
 #endif
c17171
+
c17171
     ruby_enable_autocompact = RTEST(v);
c17171
     return v;
c17171
 }
c17171
@@ -10350,22 +10385,54 @@ rb_aligned_malloc(size_t alignment, size_t size)
c17171
 #elif defined _WIN32
c17171
     void *_aligned_malloc(size_t, size_t);
c17171
     res = _aligned_malloc(size, alignment);
c17171
-#elif defined(HAVE_POSIX_MEMALIGN)
c17171
-    if (posix_memalign(&res, alignment, size) == 0) {
c17171
-        return res;
c17171
+#else
c17171
+    if (USE_MMAP_ALIGNED_ALLOC) {
c17171
+        GC_ASSERT(alignment % sysconf(_SC_PAGE_SIZE) == 0);
c17171
+
c17171
+        char *ptr = mmap(NULL, alignment + size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
c17171
+        if (ptr == MAP_FAILED) {
c17171
+            return NULL;
c17171
+        }
c17171
+
c17171
+        char *aligned = ptr + alignment;
c17171
+        aligned -= ((VALUE)aligned & (alignment - 1));
c17171
+        GC_ASSERT(aligned > ptr);
c17171
+        GC_ASSERT(aligned <= ptr + alignment);
c17171
+
c17171
+        size_t start_out_of_range_size = aligned - ptr;
c17171
+        GC_ASSERT(start_out_of_range_size % sysconf(_SC_PAGE_SIZE) == 0);
c17171
+        if (start_out_of_range_size > 0) {
c17171
+            if (munmap(ptr, start_out_of_range_size)) {
c17171
+                rb_bug("rb_aligned_malloc: munmap failed for start");
c17171
+            }
c17171
+        }
c17171
+
c17171
+        size_t end_out_of_range_size = alignment - start_out_of_range_size;
c17171
+        GC_ASSERT(end_out_of_range_size % sysconf(_SC_PAGE_SIZE) == 0);
c17171
+        if (end_out_of_range_size > 0) {
c17171
+            if (munmap(aligned + size, end_out_of_range_size)) {
c17171
+                rb_bug("rb_aligned_malloc: munmap failed for end");
c17171
+            }
c17171
+        }
c17171
+
c17171
+        res = (void *)aligned;
c17171
     }
c17171
     else {
c17171
-        return NULL;
c17171
+# if defined(HAVE_POSIX_MEMALIGN)
c17171
+        if (posix_memalign(&res, alignment, size) != 0) {
c17171
+            return NULL;
c17171
+        }
c17171
+# elif defined(HAVE_MEMALIGN)
c17171
+        res = memalign(alignment, size);
c17171
+# else
c17171
+        char* aligned;
c17171
+        res = malloc(alignment + size + sizeof(void*));
c17171
+        aligned = (char*)res + alignment + sizeof(void*);
c17171
+        aligned -= ((VALUE)aligned & (alignment - 1));
c17171
+        ((void**)aligned)[-1] = res;
c17171
+        res = (void*)aligned;
c17171
+# endif
c17171
     }
c17171
-#elif defined(HAVE_MEMALIGN)
c17171
-    res = memalign(alignment, size);
c17171
-#else
c17171
-    char* aligned;
c17171
-    res = malloc(alignment + size + sizeof(void*));
c17171
-    aligned = (char*)res + alignment + sizeof(void*);
c17171
-    aligned -= ((VALUE)aligned & (alignment - 1));
c17171
-    ((void**)aligned)[-1] = res;
c17171
-    res = (void*)aligned;
c17171
 #endif
c17171
 
c17171
     /* alignment must be a power of 2 */
c17171
@@ -10375,16 +10442,26 @@ rb_aligned_malloc(size_t alignment, size_t size)
c17171
 }
c17171
 
c17171
 static void
c17171
-rb_aligned_free(void *ptr)
c17171
+rb_aligned_free(void *ptr, size_t size)
c17171
 {
c17171
 #if defined __MINGW32__
c17171
     __mingw_aligned_free(ptr);
c17171
 #elif defined _WIN32
c17171
     _aligned_free(ptr);
c17171
-#elif defined(HAVE_MEMALIGN) || defined(HAVE_POSIX_MEMALIGN)
c17171
-    free(ptr);
c17171
 #else
c17171
-    free(((void**)ptr)[-1]);
c17171
+    if (USE_MMAP_ALIGNED_ALLOC) {
c17171
+        GC_ASSERT(size % sysconf(_SC_PAGE_SIZE) == 0);
c17171
+        if (munmap(ptr, size)) {
c17171
+            rb_bug("rb_aligned_free: munmap failed");
c17171
+        }
c17171
+    }
c17171
+    else {
c17171
+# if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN)
c17171
+        free(ptr);
c17171
+# else
c17171
+        free(((void**)ptr)[-1]);
c17171
+# endif
c17171
+    }
c17171
 #endif
c17171
 }
c17171
 
c17171
diff --git a/test/ruby/test_gc_compact.rb b/test/ruby/test_gc_compact.rb
c17171
index 4a8cff33f4..f5cab55ba7 100644
c17171
--- a/test/ruby/test_gc_compact.rb
c17171
+++ b/test/ruby/test_gc_compact.rb
c17171
@@ -4,12 +4,32 @@
c17171
 require 'etc'
c17171
 
c17171
 class TestGCCompact < Test::Unit::TestCase
c17171
-  class AutoCompact < Test::Unit::TestCase
c17171
+  module SupportsCompact
c17171
     def setup
c17171
       skip "autocompact not supported on this platform" unless supports_auto_compact?
c17171
       super
c17171
     end
c17171
 
c17171
+    private
c17171
+
c17171
+    def supports_auto_compact?
c17171
+      return true unless defined?(Etc::SC_PAGE_SIZE)
c17171
+
c17171
+      begin
c17171
+        return GC::INTERNAL_CONSTANTS[:HEAP_PAGE_SIZE] % Etc.sysconf(Etc::SC_PAGE_SIZE) == 0
c17171
+      rescue NotImplementedError
c17171
+      rescue ArgumentError
c17171
+      end
c17171
+
c17171
+      true
c17171
+    end
c17171
+  end
c17171
+
c17171
+  include SupportsCompact
c17171
+
c17171
+  class AutoCompact < Test::Unit::TestCase
c17171
+    include SupportsCompact
c17171
+
c17171
     def test_enable_autocompact
c17171
       before = GC.auto_compact
c17171
       GC.auto_compact = true
c17171
@@ -59,26 +79,17 @@ def test_implicit_compaction_does_something
c17171
     ensure
c17171
       GC.auto_compact = before
c17171
     end
c17171
-
c17171
-    private
c17171
-
c17171
-    def supports_auto_compact?
c17171
-      return true unless defined?(Etc::SC_PAGE_SIZE)
c17171
-
c17171
-      begin
c17171
-        return GC::INTERNAL_CONSTANTS[:HEAP_PAGE_SIZE] % Etc.sysconf(Etc::SC_PAGE_SIZE) == 0
c17171
-      rescue NotImplementedError
c17171
-      rescue ArgumentError
c17171
-      end
c17171
-
c17171
-      true
c17171
-    end
c17171
   end
c17171
 
c17171
   def os_page_size
c17171
     return true unless defined?(Etc::SC_PAGE_SIZE)
c17171
   end
c17171
 
c17171
+  def setup
c17171
+    skip "autocompact not supported on this platform" unless supports_auto_compact?
c17171
+    super
c17171
+  end
c17171
+
c17171
   def test_gc_compact_stats
c17171
     list = []
c17171
 
c17171
-- 
c17171
2.30.1 (Apple Git-130)
c17171