Blame SOURCES/gcc48-rh1180633.patch

ee890f
2016-01-22  Torvald Riegel  <triegel@redhat.com>
ee890f
ee890f
	* beginend.cc (GTM::gtm_thread::serial_lock): Put on cacheline
ee890f
	boundary.
ee890f
	(htm_fastpath): Remove.
ee890f
	(gtm_thread::begin_transaction): Fix HTM fastpath.
ee890f
	(_ITM_commitTransaction): Adapt.
ee890f
	(_ITM_commitTransactionEH): Adapt.
ee890f
	* libitm/config/linux/rwlock.h (gtm_rwlock): Add htm_fastpath member
ee890f
	and accessors.
ee890f
	* libitm/config/posix/rwlock.h (gtm_rwlock): Likewise.
ee890f
	* libitm/config/posix/rwlock.cc (gtm_rwlock::gtm_rwlock): Adapt.
ee890f
	* libitm/libitm_i.h (htm_fastpath): Remove declaration.
ee890f
	* libitm/method-serial.cc (htm_mg): Adapt.
ee890f
	(gtm_thread::serialirr_mode): Adapt.
ee890f
	* libitm/query.cc (_ITM_inTransaction, _ITM_getTransactionId): Adapt.
ee890f
ee890f
--- libitm/beginend.cc
ee890f
+++ libitm/beginend.cc
ee890f
@@ -32,7 +32,11 @@ using namespace GTM;
ee890f
 extern __thread gtm_thread_tls _gtm_thr_tls;
ee890f
 #endif
ee890f
 
ee890f
-gtm_rwlock GTM::gtm_thread::serial_lock;
ee890f
+// Put this at the start of a cacheline so that serial_lock's writers and
ee890f
+// htm_fastpath fields are on the same cacheline, so that HW transactions
ee890f
+// only have to pay one cacheline capacity to monitor both.
ee890f
+gtm_rwlock GTM::gtm_thread::serial_lock
ee890f
+  __attribute__((aligned(HW_CACHELINE_SIZE)));
ee890f
 gtm_thread *GTM::gtm_thread::list_of_threads = 0;
ee890f
 unsigned GTM::gtm_thread::number_of_threads = 0;
ee890f
 
ee890f
@@ -54,9 +58,6 @@ static pthread_mutex_t global_tid_lock = PTHREAD_MUTEX_INITIALIZER;
ee890f
 static pthread_key_t thr_release_key;
ee890f
 static pthread_once_t thr_release_once = PTHREAD_ONCE_INIT;
ee890f
 
ee890f
-// See gtm_thread::begin_transaction.
ee890f
-uint32_t GTM::htm_fastpath = 0;
ee890f
-
ee890f
 /* Allocate a transaction structure.  */
ee890f
 void *
ee890f
 GTM::gtm_thread::operator new (size_t s)
ee890f
@@ -174,9 +175,11 @@ GTM::gtm_thread::begin_transaction (uint32_t prop, const gtm_jmpbuf *jb)
ee890f
   // lock's writer flag and thus abort if another thread is or becomes a
ee890f
   // serial transaction.  Therefore, if the fastpath is enabled, then a
ee890f
   // transaction is not executing as a HW transaction iff the serial lock is
ee890f
-  // write-locked.  This allows us to use htm_fastpath and the serial lock's
ee890f
-  // writer flag to reliable determine whether the current thread runs a HW
ee890f
-  // transaction, and thus we do not need to maintain this information in
ee890f
+  // write-locked.  Also, HW transactions monitor the fastpath control
ee890f
+  // variable, so that they will only execute if dispatch_htm is still the
ee890f
+  // current method group.  This allows us to use htm_fastpath and the serial
ee890f
+  // lock's writers flag to reliable determine whether the current thread runs
ee890f
+  // a HW transaction, and thus we do not need to maintain this information in
ee890f
   // per-thread state.
ee890f
   // If an uninstrumented code path is not available, we can still run
ee890f
   // instrumented code from a HW transaction because the HTM fastpath kicks
ee890f
@@ -187,9 +190,14 @@ GTM::gtm_thread::begin_transaction (uint32_t prop, const gtm_jmpbuf *jb)
ee890f
   // indeed in serial mode, and HW transactions should never need serial mode
ee890f
   // for any internal changes (e.g., they never abort visibly to the STM code
ee890f
   // and thus do not trigger the standard retry handling).
ee890f
-  if (likely(htm_fastpath && (prop & pr_hasNoAbort)))
ee890f
+  if (likely(serial_lock.get_htm_fastpath() && (prop & pr_hasNoAbort)))
ee890f
     {
ee890f
-      for (uint32_t t = htm_fastpath; t; t--)
ee890f
+      // Note that the snapshot of htm_fastpath that we take here could be
ee890f
+      // outdated, and a different method group than dispatch_htm may have
ee890f
+      // been chosen in the meantime.  Therefore, take care not not touch
ee890f
+      // anything besides the serial lock, which is independent of method
ee890f
+      // groups.
ee890f
+      for (uint32_t t = serial_lock.get_htm_fastpath(); t; t--)
ee890f
 	{
ee890f
 	  uint32_t ret = htm_begin();
ee890f
 	  if (htm_begin_success(ret))
ee890f
@@ -197,9 +205,11 @@ GTM::gtm_thread::begin_transaction (uint32_t prop, const gtm_jmpbuf *jb)
ee890f
 	      // We are executing a transaction now.
ee890f
 	      // Monitor the writer flag in the serial-mode lock, and abort
ee890f
 	      // if there is an active or waiting serial-mode transaction.
ee890f
+	      // Also checks that htm_fastpath is still nonzero and thus
ee890f
+	      // HW transactions are allowed to run.
ee890f
 	      // Note that this can also happen due to an enclosing
ee890f
 	      // serial-mode transaction; we handle this case below.
ee890f
-	      if (unlikely(serial_lock.is_write_locked()))
ee890f
+	      if (unlikely(serial_lock.htm_fastpath_disabled()))
ee890f
 		htm_abort();
ee890f
 	      else
ee890f
 		// We do not need to set a_saveLiveVariables because of HTM.
ee890f
@@ -210,9 +220,12 @@ GTM::gtm_thread::begin_transaction (uint32_t prop, const gtm_jmpbuf *jb)
ee890f
 	  // retrying the transaction will be successful.
ee890f
 	  if (!htm_abort_should_retry(ret))
ee890f
 	    break;
ee890f
+	  // Check whether the HTM fastpath has been disabled.
ee890f
+	  if (!serial_lock.get_htm_fastpath())
ee890f
+	    break;
ee890f
 	  // Wait until any concurrent serial-mode transactions have finished.
ee890f
 	  // This is an empty critical section, but won't be elided.
ee890f
-	  if (serial_lock.is_write_locked())
ee890f
+	  if (serial_lock.htm_fastpath_disabled())
ee890f
 	    {
ee890f
 	      tx = gtm_thr();
ee890f
 	      if (unlikely(tx == NULL))
ee890f
@@ -618,7 +631,7 @@ _ITM_commitTransaction(void)
ee890f
   // a serial-mode transaction.  If we are, then there will be no other
ee890f
   // concurrent serial-mode transaction.
ee890f
   // See gtm_thread::begin_transaction.
ee890f
-  if (likely(htm_fastpath && !gtm_thread::serial_lock.is_write_locked()))
ee890f
+  if (likely(!gtm_thread::serial_lock.htm_fastpath_disabled()))
ee890f
     {
ee890f
       htm_commit();
ee890f
       return;
ee890f
@@ -634,7 +647,7 @@ _ITM_commitTransactionEH(void *exc_ptr)
ee890f
 {
ee890f
 #if defined(USE_HTM_FASTPATH)
ee890f
   // See _ITM_commitTransaction.
ee890f
-  if (likely(htm_fastpath && !gtm_thread::serial_lock.is_write_locked()))
ee890f
+  if (likely(!gtm_thread::serial_lock.htm_fastpath_disabled()))
ee890f
     {
ee890f
       htm_commit();
ee890f
       return;
ee890f
--- libitm/config/linux/rwlock.h
ee890f
+++ libitm/config/linux/rwlock.h
ee890f
@@ -39,16 +39,29 @@ struct gtm_thread;
ee890f
 //
ee890f
 // In this implementation, writers are given highest priority access but
ee890f
 // read-to-write upgrades do not have a higher priority than writers.
ee890f
+//
ee890f
+// Do not change the layout of this class; it must remain a POD type with
ee890f
+// standard layout, and the writers field must be first (i.e., so the
ee890f
+// assembler code can assume that its address is equal to the address of the
ee890f
+// respective instance of the class), and htm_fastpath must be second.
ee890f
 
ee890f
 class gtm_rwlock
ee890f
 {
ee890f
-  // TODO Put futexes on different cachelines?
ee890f
   std::atomic<int> writers;       // Writers' futex.
ee890f
+  // We put the HTM fastpath control variable here so that HTM fastpath
ee890f
+  // transactions can check efficiently whether they are allowed to run.
ee890f
+  // This must be accessed atomically because threads can load this value
ee890f
+  // when they are neither a registered reader nor writer (i.e., when they
ee890f
+  // attempt to execute the HTM fastpath).
ee890f
+  std::atomic<uint32_t> htm_fastpath;
ee890f
+  // TODO Put these futexes on different cachelines?  (writers and htm_fastpath
ee890f
+  // should remain on the same cacheline.
ee890f
   std::atomic<int> writer_readers;// A confirmed writer waits here for readers.
ee890f
   std::atomic<int> readers;       // Readers wait here for writers (iff true).
ee890f
 
ee890f
  public:
ee890f
-  gtm_rwlock() : writers(0), writer_readers(0), readers(0) {};
ee890f
+  gtm_rwlock() : writers(0), htm_fastpath(0), writer_readers(0), readers(0)
ee890f
+  { }
ee890f
 
ee890f
   void read_lock (gtm_thread *tx);
ee890f
   void read_unlock (gtm_thread *tx);
ee890f
@@ -59,12 +72,28 @@ class gtm_rwlock
ee890f
   bool write_upgrade (gtm_thread *tx);
ee890f
   void write_upgrade_finish (gtm_thread *tx);
ee890f
 
ee890f
-  // Returns true iff there is a concurrent active or waiting writer.
ee890f
-  // This is primarily useful for simple HyTM approaches, and the value being
ee890f
-  // checked is loaded with memory_order_relaxed.
ee890f
-  bool is_write_locked()
ee890f
+  // Returns true iff there is a concurrent active or waiting writer, or
ee890f
+  // htm_fastpath is zero. This is primarily useful for simple HyTM
ee890f
+  // approaches, and the values being checked are loaded with
ee890f
+  // memory_order_relaxed.
ee890f
+  bool htm_fastpath_disabled ()
ee890f
+  {
ee890f
+    return writers.load (memory_order_relaxed) != 0
ee890f
+	|| htm_fastpath.load (memory_order_relaxed) == 0;
ee890f
+  }
ee890f
+
ee890f
+  // This does not need to return an exact value, hence relaxed MO is
ee890f
+  // sufficient.
ee890f
+  uint32_t get_htm_fastpath ()
ee890f
+  {
ee890f
+    return htm_fastpath.load (memory_order_relaxed);
ee890f
+  }
ee890f
+  // This must only be called while having acquired the write lock, and other
ee890f
+  // threads do not need to load an exact value; hence relaxed MO is
ee890f
+  // sufficient.
ee890f
+  void set_htm_fastpath (uint32_t val)
ee890f
   {
ee890f
-    return writers.load (memory_order_relaxed) != 0;
ee890f
+    htm_fastpath.store (val, memory_order_relaxed);
ee890f
   }
ee890f
 
ee890f
  protected:
ee890f
--- libitm/config/posix/rwlock.h
ee890f
+++ libitm/config/posix/rwlock.h
ee890f
@@ -44,19 +44,32 @@ struct gtm_thread;
ee890f
 //
ee890f
 // In this implementation, writers are given highest priority access but
ee890f
 // read-to-write upgrades do not have a higher priority than writers.
ee890f
+//
ee890f
+// Do not change the layout of this class; it must remain a POD type with
ee890f
+// standard layout, and the summary field must be first (i.e., so the
ee890f
+// assembler code can assume that its address is equal to the address of the
ee890f
+// respective instance of the class), and htm_fastpath must be second.
ee890f
 
ee890f
 class gtm_rwlock
ee890f
 {
ee890f
-  pthread_mutex_t mutex;	        // Held if manipulating any field.
ee890f
-  pthread_cond_t c_readers;	        // Readers wait here
ee890f
-  pthread_cond_t c_writers;	        // Writers wait here for writers
ee890f
-  pthread_cond_t c_confirmed_writers;	// Writers wait here for readers
ee890f
-
ee890f
   static const unsigned a_writer  = 1;	// An active writer.
ee890f
   static const unsigned w_writer  = 2;	// The w_writers field != 0
ee890f
   static const unsigned w_reader  = 4;  // The w_readers field != 0
ee890f
 
ee890f
   std::atomic<unsigned int> summary;	// Bitmask of the above.
ee890f
+
ee890f
+  // We put the HTM fastpath control variable here so that HTM fastpath
ee890f
+  // transactions can check efficiently whether they are allowed to run.
ee890f
+  // This must be accessed atomically because threads can load this value
ee890f
+  // when they are neither a registered reader nor writer (i.e., when they
ee890f
+  // attempt to execute the HTM fastpath).
ee890f
+  std::atomic<uint32_t> htm_fastpath;
ee890f
+
ee890f
+  pthread_mutex_t mutex;	        // Held if manipulating any field.
ee890f
+  pthread_cond_t c_readers;	        // Readers wait here
ee890f
+  pthread_cond_t c_writers;	        // Writers wait here for writers
ee890f
+  pthread_cond_t c_confirmed_writers;	// Writers wait here for readers
ee890f
+
ee890f
   unsigned int a_readers;	// Nr active readers as observed by a writer
ee890f
   unsigned int w_readers;	// Nr waiting readers
ee890f
   unsigned int w_writers;	// Nr waiting writers
ee890f
@@ -74,12 +87,28 @@ class gtm_rwlock
ee890f
   bool write_upgrade (gtm_thread *tx);
ee890f
   void write_upgrade_finish (gtm_thread *tx);
ee890f
 
ee890f
-  // Returns true iff there is a concurrent active or waiting writer.
ee890f
-  // This is primarily useful for simple HyTM approaches, and the value being
ee890f
-  // checked is loaded with memory_order_relaxed.
ee890f
-  bool is_write_locked()
ee890f
+  // Returns true iff there is a concurrent active or waiting writer, or
ee890f
+  // htm_fastpath is zero. This is primarily useful for simple HyTM
ee890f
+  // approaches, and the values being checked are loaded with
ee890f
+  // memory_order_relaxed.
ee890f
+  bool htm_fastpath_disabled ()
ee890f
+  {
ee890f
+    return (summary.load (memory_order_relaxed) & (a_writer | w_writer))
ee890f
+	|| htm_fastpath.load (memory_order_relaxed) == 0;
ee890f
+  }
ee890f
+
ee890f
+  // This does not need to return an exact value, hence relaxed MO is
ee890f
+  // sufficient.
ee890f
+  uint32_t get_htm_fastpath ()
ee890f
+  {
ee890f
+    return htm_fastpath.load (memory_order_relaxed);
ee890f
+  }
ee890f
+  // This must only be called while having acquired the write lock, and other
ee890f
+  // threads do not need to load an exact value; hence relaxed MO is
ee890f
+  // sufficient.
ee890f
+  void set_htm_fastpath (uint32_t val)
ee890f
   {
ee890f
-    return summary.load (memory_order_relaxed) & (a_writer | w_writer);
ee890f
+    htm_fastpath.store (val, memory_order_relaxed);
ee890f
   }
ee890f
 
ee890f
  protected:
ee890f
--- libitm/config/posix/rwlock.cc
ee890f
+++ libitm/config/posix/rwlock.cc
ee890f
@@ -30,11 +30,12 @@ namespace GTM HIDDEN {
ee890f
 // ??? Move this back to the header file when constexpr is implemented.
ee890f
 
ee890f
 gtm_rwlock::gtm_rwlock()
ee890f
-  : mutex (PTHREAD_MUTEX_INITIALIZER),
ee890f
+  : summary (0),
ee890f
+    htm_fastpath (0),
ee890f
+    mutex (PTHREAD_MUTEX_INITIALIZER),
ee890f
     c_readers (PTHREAD_COND_INITIALIZER),
ee890f
     c_writers (PTHREAD_COND_INITIALIZER),
ee890f
     c_confirmed_writers (PTHREAD_COND_INITIALIZER),
ee890f
-    summary (0),
ee890f
     a_readers (0),
ee890f
     w_readers (0),
ee890f
     w_writers (0)
ee890f
--- libitm/libitm_i.h
ee890f
+++ libitm/libitm_i.h
ee890f
@@ -336,10 +336,6 @@ extern abi_dispatch *dispatch_htm();
ee890f
 
ee890f
 extern gtm_cacheline_mask gtm_mask_stack(gtm_cacheline *, gtm_cacheline_mask);
ee890f
 
ee890f
-// Control variable for the HTM fastpath that uses serial mode as fallback.
ee890f
-// Non-zero if the HTM fastpath is enabled. See gtm_thread::begin_transaction.
ee890f
-extern uint32_t htm_fastpath;
ee890f
-
ee890f
 } // namespace GTM
ee890f
 
ee890f
 #endif // LIBITM_I_H
ee890f
--- libitm/method-serial.cc
ee890f
+++ libitm/method-serial.cc
ee890f
@@ -222,13 +222,13 @@ struct htm_mg : public method_group
ee890f
     // Enable the HTM fastpath if the HW is available.  The fastpath is
ee890f
     // initially disabled.
ee890f
 #ifdef USE_HTM_FASTPATH
ee890f
-    htm_fastpath = htm_init();
ee890f
+    gtm_thread::serial_lock.set_htm_fastpath(htm_init());
ee890f
 #endif
ee890f
   }
ee890f
   virtual void fini()
ee890f
   {
ee890f
     // Disable the HTM fastpath.
ee890f
-    htm_fastpath = 0;
ee890f
+    gtm_thread::serial_lock.set_htm_fastpath(0);
ee890f
   }
ee890f
 };
ee890f
 
ee890f
@@ -288,7 +288,7 @@ GTM::gtm_thread::serialirr_mode ()
ee890f
 #if defined(USE_HTM_FASTPATH)
ee890f
   // HTM fastpath.  If we are executing a HW transaction, don't go serial but
ee890f
   // continue.  See gtm_thread::begin_transaction.
ee890f
-  if (likely(htm_fastpath && !gtm_thread::serial_lock.is_write_locked()))
ee890f
+  if (likely(!gtm_thread::serial_lock.htm_fastpath_disabled()))
ee890f
     return;
ee890f
 #endif
ee890f
 
ee890f
--- libitm/query.cc
ee890f
+++ libitm/query.cc
ee890f
@@ -49,7 +49,7 @@ _ITM_inTransaction (void)
ee890f
   // a transaction and thus we can't deduce this by looking at just the serial
ee890f
   // lock.  This function isn't used in practice currently, so the easiest
ee890f
   // way to handle it is to just abort.
ee890f
-  if (htm_fastpath && htm_transaction_active())
ee890f
+  if (gtm_thread::serial_lock.get_htm_fastpath() && htm_transaction_active())
ee890f
     htm_abort();
ee890f
 #endif
ee890f
   struct gtm_thread *tx = gtm_thr();
ee890f
@@ -69,7 +69,7 @@ _ITM_getTransactionId (void)
ee890f
 {
ee890f
 #if defined(USE_HTM_FASTPATH)
ee890f
   // See ITM_inTransaction.
ee890f
-  if (htm_fastpath && htm_transaction_active())
ee890f
+  if (gtm_thread::serial_lock.get_htm_fastpath() && htm_transaction_active())
ee890f
     htm_abort();
ee890f
 #endif
ee890f
   struct gtm_thread *tx = gtm_thr();