Blame SOURCES/gcc48-rh1180633.patch

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