]> granicus.if.org Git - gc/commitdiff
Workaround Linux NTPL lock elision bug.
authorPaul Bone <paul@bone.id.au>
Wed, 25 Jun 2014 01:17:50 +0000 (11:17 +1000)
committerPaul Bone <paul@bone.id.au>
Fri, 27 Jun 2014 02:10:06 +0000 (12:10 +1000)
glibc 2.19 on Linux x86-64 platforms includes support for lock elision,
by using Intel's TSX support when it is available.  Without modifying an
application this converts suitable critical sections that use mutex into
transactional memory critical sections.  See http://lwn.net/Articles/534758/
If a problem occurs that means that transactional memory can't be used, such
as a system call or buffer overflow, the pthreads implementation will catch
this error and retry the critical section using a normal mutex.

I noticed that since upgrading glibc that programs using Boehm GC crash, one
of these crashes was an assertion that the owner field of a mutex was
invalid.  The assertion was generated by the pthreads implementation.
I believe that there is a bug in glibc that when a mutex cannot be used
safely for transactions that some series of events causes it's owner field
to be set incorrectly (or cleared when it shouldn't be).

I've found that I can work around this problem by having Boehm GC use an
error checking mutex, which I believe doesn't use lock elision and in my
testing doesn't crash.

XXX: This work-around mostly works except for linking the feature detection
in configure.ac to the conditional compilation in pthread_support.c as there
isn't an obvious way to make it work for automake and Makefile.direct.
Could I have some help updating the build system please?

include/private/pthread_support.h:
pthread_support.c:
    Define GC_setup_mark_lock()  This procedure creates the lock specifying a
    pthread_mutexattr_t structure.  This is used to disable lock elision on
    Linux with glibc 2.19 or greater.

configure.ac:
    If we're using Linux then check for the gnu extensions required to
    identify the version of glibc at runtime.

misc.c:
    Call GC_setup_mark_lock() when initialising the collector.

configure.ac
include/private/pthread_support.h
misc.c
pthread_support.c

index 76679498d478cd7361f8b2d53ed117d904ad5f41..6853e973f4a866841daf1a3698b7cacb7b189605 100644 (file)
@@ -646,6 +646,19 @@ case "$host" in
  *) AC_MSG_RESULT(no) ;;
 esac
 
+dnl Check for specific glibc functions and definitions that we need to for
+dnl the glibc 2.19 workaround.
+HAVE_LIBC_VERSION_H=no
+HAVE_GNU_GET_LIBC_VERSION=no
+case "${host}" in
+  *-linux*)
+    AC_CHECK_HEADER([gnu/libc-version.h], HAVE_LIBC_VERSION_H=yes)
+    AC_CHECK_FUNC([gnu_get_libc_version], HAVE_GNU_GET_LIBC_VERSION=yes)
+    ;;
+esac
+AC_SUBST(HAVE_LIBC_VERSION_H)
+AC_SUBST(HAVE_GNU_GET_LIBC_VERSION)
+
 dnl Include defines that have become de facto standard.
 dnl ALL_INTERIOR_POINTERS and NO_EXECUTE_PERMISSION can be overridden
 dnl in the startup code.
index 525a9aac2f37340b24f52e0c161df1a276a47dd0..017f1941de8851a37816b6f687436350e78a47b8 100644 (file)
@@ -148,6 +148,8 @@ GC_INNER_PTHRSTART GC_thread GC_start_rtn_prepare_thread(
                                         struct GC_stack_base *sb, void *arg);
 GC_INNER_PTHRSTART void GC_thread_exit_proc(void *);
 
+GC_INNER void GC_setup_mark_lock(void);
+
 #endif /* GC_PTHREADS && !GC_WIN32_THREADS */
 
 #endif /* GC_PTHREAD_SUPPORT_H */
diff --git a/misc.c b/misc.c
index df434a12f94320eee30c9fab96753f278692ee60..dccf5f37e69f2581db677fc962641453608194a1 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -875,6 +875,9 @@ GC_API void GC_CALL GC_init(void)
         /* else */ InitializeCriticalSection (&GC_allocate_ml);
      }
 #   endif /* GC_WIN32_THREADS */
+#   if (defined(GC_PTHREADS) && !defined(GC_WIN32_THREADS))
+     GC_setup_mark_lock();
+#   endif /* GC_PTHREADS */
 #   if (defined(MSWIN32) || defined(MSWINCE)) && defined(THREADS)
       InitializeCriticalSection(&GC_write_cs);
 #   endif
index c00b93d5f9eda8f81063d198f84b371af1c4a8dd..56fc94b604023cfa7f898d02fcfcd2d069b20c2d 100644 (file)
   typedef unsigned int sem_t;
 #endif /* GC_DGUX386_THREADS */
 
+#ifdef HAVE_LIBC_VERSION_H
+# include <gnu/libc-version.h>
+#endif
+
 /* Undefine macros used to redirect pthread primitives. */
 # undef pthread_create
 # ifndef GC_NO_PTHREAD_SIGMASK
@@ -1973,12 +1977,61 @@ GC_INNER void GC_lock(void)
   /* defined.                                                           */
   static pthread_mutex_t mark_mutex =
         {0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, {0, 0}};
+#elif defined(HAVE_GNU_GET_LIBC_VERSION) && defined(HAVE_LIBC_VERSION_H)
+  static pthread_mutex_t mark_mutex;
 #else
   static pthread_mutex_t mark_mutex = PTHREAD_MUTEX_INITIALIZER;
 #endif
 
 static pthread_cond_t builder_cv = PTHREAD_COND_INITIALIZER;
 
+GC_INNER void GC_setup_mark_lock(void)
+{
+#if defined(HAVE_GNU_GET_LIBC_VERSION) && defined(HAVE_LIBC_VERSION_H)
+    pthread_mutexattr_t attr;
+    char *version_str = NULL;
+    char *strtok_save;
+    char *version_part;
+    char *version_str;
+
+    if (0 != pthread_mutexattr_init(&attr)) {
+        goto error;
+    }
+
+    /*
+    ** Check for version 2.19 or greater.
+    */
+    version_str = strdup(gnu_get_libc_version());
+    version_part = strtok_r(version_str, ".", &strtok_save);
+    if ((NULL != version_part) && (2 <= atoi(version_part))) {
+        version_part = strtok_r(NULL, ".", &strtok_save);
+        if ((NULL != version_part) && (19 <= atoi(version_part))) {
+            /*
+             * Disable lock elision on this version of glibc.
+             */
+            if (0 != pthread_mutexattr_settype(&attr,
+                        PTHREAD_MUTEX_ERRORCHECK))
+            {
+                goto error;
+            }
+        }
+    }
+
+    if (0 != pthread_mutex_init(&mark_mutex, &attr)) {
+        goto error;
+    }
+    pthread_mutexattr_destroy(&attr);
+    if (NULL != version_str) {
+        free(version_str);
+    }
+    return;
+
+error:
+    perror("Error setting up marker mutex");
+    exit(1);
+#endif /* HAVE_GNU_GET_LIBC_VERSION && HAVE_LIBC_VERSION_H */
+}
+
 GC_INNER void GC_acquire_mark_lock(void)
 {
     GC_ASSERT(GC_mark_lock_holder != NUMERIC_THREAD_ID(pthread_self()));