bdc76f
commit 823624bdc47f1f80109c9c52dee7939b9386d708
bdc76f
Author: Stefan Liebler <stli@linux.ibm.com>
bdc76f
Date:   Thu Feb 7 15:18:36 2019 +0100
bdc76f
bdc76f
    Add compiler barriers around modifications of the robust mutex list for pthread_mutex_trylock. [BZ #24180]
bdc76f
    
bdc76f
    While debugging a kernel warning, Thomas Gleixner, Sebastian Sewior and
bdc76f
    Heiko Carstens found a bug in pthread_mutex_trylock due to misordered
bdc76f
    instructions:
bdc76f
    140:   a5 1b 00 01             oill    %r1,1
bdc76f
    144:   e5 48 a0 f0 00 00       mvghi   240(%r10),0   <--- THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
bdc76f
    14a:   e3 10 a0 e0 00 24       stg     %r1,224(%r10) <--- last THREAD_SETMEM of ENQUEUE_MUTEX_PI
bdc76f
    
bdc76f
    vs (with compiler barriers):
bdc76f
    140:   a5 1b 00 01             oill    %r1,1
bdc76f
    144:   e3 10 a0 e0 00 24       stg     %r1,224(%r10)
bdc76f
    14a:   e5 48 a0 f0 00 00       mvghi   240(%r10),0
bdc76f
    
bdc76f
    Please have a look at the discussion:
bdc76f
    "Re: WARN_ON_ONCE(!new_owner) within wake_futex_pi() triggerede"
bdc76f
    (https://lore.kernel.org/lkml/20190202112006.GB3381@osiris/)
bdc76f
    
bdc76f
    This patch is introducing the same compiler barriers and comments
bdc76f
    for pthread_mutex_trylock as introduced for pthread_mutex_lock and
bdc76f
    pthread_mutex_timedlock by commit 8f9450a0b7a9e78267e8ae1ab1000ebca08e473e
bdc76f
    "Add compiler barriers around modifications of the robust mutex list."
bdc76f
    
bdc76f
    ChangeLog:
bdc76f
    
bdc76f
            [BZ #24180]
bdc76f
            * nptl/pthread_mutex_trylock.c (__pthread_mutex_trylock):
bdc76f
bdc76f
diff --git a/nptl/pthread_mutex_trylock.c b/nptl/pthread_mutex_trylock.c
bdc76f
index 8fe43b8f0f..bf2869eca2 100644
bdc76f
--- a/nptl/pthread_mutex_trylock.c
bdc76f
+++ b/nptl/pthread_mutex_trylock.c
bdc76f
@@ -94,6 +94,9 @@ __pthread_mutex_trylock (pthread_mutex_t *mutex)
bdc76f
     case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
bdc76f
       THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
bdc76f
 		     &mutex->__data.__list.__next);
bdc76f
+      /* We need to set op_pending before starting the operation.  Also
bdc76f
+	 see comments at ENQUEUE_MUTEX.  */
bdc76f
+      __asm ("" ::: "memory");
bdc76f
 
bdc76f
       oldval = mutex->__data.__lock;
bdc76f
       do
bdc76f
@@ -119,7 +122,12 @@ __pthread_mutex_trylock (pthread_mutex_t *mutex)
bdc76f
 	      /* But it is inconsistent unless marked otherwise.  */
bdc76f
 	      mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
bdc76f
 
bdc76f
+	      /* We must not enqueue the mutex before we have acquired it.
bdc76f
+		 Also see comments at ENQUEUE_MUTEX.  */
bdc76f
+	      __asm ("" ::: "memory");
bdc76f
 	      ENQUEUE_MUTEX (mutex);
bdc76f
+	      /* We need to clear op_pending after we enqueue the mutex.  */
bdc76f
+	      __asm ("" ::: "memory");
bdc76f
 	      THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
bdc76f
 
bdc76f
 	      /* Note that we deliberately exist here.  If we fall
bdc76f
@@ -135,6 +143,8 @@ __pthread_mutex_trylock (pthread_mutex_t *mutex)
bdc76f
 	      int kind = PTHREAD_MUTEX_TYPE (mutex);
bdc76f
 	      if (kind == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP)
bdc76f
 		{
bdc76f
+		  /* We do not need to ensure ordering wrt another memory
bdc76f
+		     access.  Also see comments at ENQUEUE_MUTEX. */
bdc76f
 		  THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
bdc76f
 				 NULL);
bdc76f
 		  return EDEADLK;
bdc76f
@@ -142,6 +152,8 @@ __pthread_mutex_trylock (pthread_mutex_t *mutex)
bdc76f
 
bdc76f
 	      if (kind == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP)
bdc76f
 		{
bdc76f
+		  /* We do not need to ensure ordering wrt another memory
bdc76f
+		     access.  */
bdc76f
 		  THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
bdc76f
 				 NULL);
bdc76f
 
bdc76f
@@ -160,6 +172,9 @@ __pthread_mutex_trylock (pthread_mutex_t *mutex)
bdc76f
 							id, 0);
bdc76f
 	  if (oldval != 0 && (oldval & FUTEX_OWNER_DIED) == 0)
bdc76f
 	    {
bdc76f
+	      /* We haven't acquired the lock as it is already acquired by
bdc76f
+		 another owner.  We do not need to ensure ordering wrt another
bdc76f
+		 memory access.  */
bdc76f
 	      THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
bdc76f
 
bdc76f
 	      return EBUSY;
bdc76f
@@ -173,13 +188,20 @@ __pthread_mutex_trylock (pthread_mutex_t *mutex)
bdc76f
 	      if (oldval == id)
bdc76f
 		lll_unlock (mutex->__data.__lock,
bdc76f
 			    PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
bdc76f
+	      /* FIXME This violates the mutex destruction requirements.  See
bdc76f
+		 __pthread_mutex_unlock_full.  */
bdc76f
 	      THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
bdc76f
 	      return ENOTRECOVERABLE;
bdc76f
 	    }
bdc76f
 	}
bdc76f
       while ((oldval & FUTEX_OWNER_DIED) != 0);
bdc76f
 
bdc76f
+      /* We must not enqueue the mutex before we have acquired it.
bdc76f
+	 Also see comments at ENQUEUE_MUTEX.  */
bdc76f
+      __asm ("" ::: "memory");
bdc76f
       ENQUEUE_MUTEX (mutex);
bdc76f
+      /* We need to clear op_pending after we enqueue the mutex.  */
bdc76f
+      __asm ("" ::: "memory");
bdc76f
       THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
bdc76f
 
bdc76f
       mutex->__data.__owner = id;
bdc76f
@@ -211,10 +233,15 @@ __pthread_mutex_trylock (pthread_mutex_t *mutex)
bdc76f
 	}
bdc76f
 
bdc76f
 	if (robust)
bdc76f
-	  /* Note: robust PI futexes are signaled by setting bit 0.  */
bdc76f
-	  THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
bdc76f
-			 (void *) (((uintptr_t) &mutex->__data.__list.__next)
bdc76f
-				   | 1));
bdc76f
+	  {
bdc76f
+	    /* Note: robust PI futexes are signaled by setting bit 0.  */
bdc76f
+	    THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
bdc76f
+			   (void *) (((uintptr_t) &mutex->__data.__list.__next)
bdc76f
+				     | 1));
bdc76f
+	    /* We need to set op_pending before starting the operation.  Also
bdc76f
+	       see comments at ENQUEUE_MUTEX.  */
bdc76f
+	    __asm ("" ::: "memory");
bdc76f
+	  }
bdc76f
 
bdc76f
 	oldval = mutex->__data.__lock;
bdc76f
 
bdc76f
@@ -223,12 +250,16 @@ __pthread_mutex_trylock (pthread_mutex_t *mutex)
bdc76f
 	  {
bdc76f
 	    if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
bdc76f
 	      {
bdc76f
+		/* We do not need to ensure ordering wrt another memory
bdc76f
+		   access.  */
bdc76f
 		THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
bdc76f
 		return EDEADLK;
bdc76f
 	      }
bdc76f
 
bdc76f
 	    if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
bdc76f
 	      {
bdc76f
+		/* We do not need to ensure ordering wrt another memory
bdc76f
+		   access.  */
bdc76f
 		THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
bdc76f
 
bdc76f
 		/* Just bump the counter.  */
bdc76f
@@ -250,6 +281,9 @@ __pthread_mutex_trylock (pthread_mutex_t *mutex)
bdc76f
 	  {
bdc76f
 	    if ((oldval & FUTEX_OWNER_DIED) == 0)
bdc76f
 	      {
bdc76f
+		/* We haven't acquired the lock as it is already acquired by
bdc76f
+		   another owner.  We do not need to ensure ordering wrt another
bdc76f
+		   memory access.  */
bdc76f
 		THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
bdc76f
 
bdc76f
 		return EBUSY;
bdc76f
@@ -270,6 +304,9 @@ __pthread_mutex_trylock (pthread_mutex_t *mutex)
bdc76f
 	    if (INTERNAL_SYSCALL_ERROR_P (e, __err)
bdc76f
 		&& INTERNAL_SYSCALL_ERRNO (e, __err) == EWOULDBLOCK)
bdc76f
 	      {
bdc76f
+		/* The kernel has not yet finished the mutex owner death.
bdc76f
+		   We do not need to ensure ordering wrt another memory
bdc76f
+		   access.  */
bdc76f
 		THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
bdc76f
 
bdc76f
 		return EBUSY;
bdc76f
@@ -287,7 +324,12 @@ __pthread_mutex_trylock (pthread_mutex_t *mutex)
bdc76f
 	    /* But it is inconsistent unless marked otherwise.  */
bdc76f
 	    mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
bdc76f
 
bdc76f
+	    /* We must not enqueue the mutex before we have acquired it.
bdc76f
+	       Also see comments at ENQUEUE_MUTEX.  */
bdc76f
+	    __asm ("" ::: "memory");
bdc76f
 	    ENQUEUE_MUTEX (mutex);
bdc76f
+	    /* We need to clear op_pending after we enqueue the mutex.  */
bdc76f
+	    __asm ("" ::: "memory");
bdc76f
 	    THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
bdc76f
 
bdc76f
 	    /* Note that we deliberately exit here.  If we fall
bdc76f
@@ -310,13 +352,20 @@ __pthread_mutex_trylock (pthread_mutex_t *mutex)
bdc76f
 						  PTHREAD_ROBUST_MUTEX_PSHARED (mutex)),
bdc76f
 			      0, 0);
bdc76f
 
bdc76f
+	    /* To the kernel, this will be visible after the kernel has
bdc76f
+	       acquired the mutex in the syscall.  */
bdc76f
 	    THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
bdc76f
 	    return ENOTRECOVERABLE;
bdc76f
 	  }
bdc76f
 
bdc76f
 	if (robust)
bdc76f
 	  {
bdc76f
+	    /* We must not enqueue the mutex before we have acquired it.
bdc76f
+	       Also see comments at ENQUEUE_MUTEX.  */
bdc76f
+	    __asm ("" ::: "memory");
bdc76f
 	    ENQUEUE_MUTEX_PI (mutex);
bdc76f
+	    /* We need to clear op_pending after we enqueue the mutex.  */
bdc76f
+	    __asm ("" ::: "memory");
bdc76f
 	    THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
bdc76f
 	  }
bdc76f