Blame SOURCES/gcc48-rh1180633.patch

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