Blame SOURCES/gcc48-rh1180633.patch

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