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