Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Fri, 24 May 2024 15:32:47 +0800
From: "AK47" <250200715@...com>
To: "musl" <musl@...ts.openwall.com>
Subject: Pthread robust_list for non-pshared mutexes

Hi all,

Now for a normal non-Robust mutex, like a normal mutex with type PTHREAD_MUTEX_RECURSIVE. I found it also will be add to robust_list in pthread in&nbsp;&nbsp;__pthread_mutex_trylock_owner:
      volatile void *next = self-&gt;robust_list.head;      m-&gt;_m_next = next;
      m-&gt;_m_prev = &amp;self-&gt;robust_list.head;
      if (next != &amp;self-&gt;robust_list.head) *(volatile void *volatile *)
            ((char *)next - sizeof(void *)) = &amp;m-&gt;_m_next;
      self-&gt;robust_list.head = &amp;m-&gt;_m_next;
      self-&gt;robust_list.pending = 0;



However, it seems that for non-Robust mutexes added to the pthread robust_list, only when the pthread exits we will do&nbsp;a traversal wake-up operation in __pthread_exit:

      volatile void *volatile *rp;      while ((rp=self-&gt;robust_list.head) &amp;&amp; rp != &amp;self-&gt;robust_list.head) {
            pthread_mutex_t *m = (void *)((char *)rp
                  - offsetof(pthread_mutex_t, _m_next));
            int waiters = m-&gt;_m_waiters;
            int priv = (m-&gt;_m_type &amp; 128) ^ 128;
            self-&gt;robust_list.pending = rp;
            self-&gt;robust_list.head = *rp;
            int cont = a_swap(&amp;m-&gt;_m_lock, 0x40000000);
            self-&gt;robust_list.pending = 0;
            if (cont < 0 || waiters)
                  __wake(&amp;m-&gt;_m_lock, 1, priv);
      }





And taking a regular recursive mutex as an example, it will be added to the robust_list, if the thread ends but it is still not fully released, The robust_list still contains this mutex. We will swap the _m​_lock to owner died (0x40000000) and wake up one of the waiting thread. But due to this code in&nbsp;__pthread_mutex_trylock_owner:



      if (own == 0x3fffffff) return ENOTRECOVERABLE;
      if (own || (old &amp;&amp; !(type &amp; 4))) return EBUSY;



The waiting thread which has been waked up will nerver get the&nbsp;recursive mutex, the deadlock still occurred. So my question is, why do we need to add some non-Roubus mutexes, such as PTHREAD-MUTEX-RECURSIVE, to the robust list&nbsp;and try to do wake up in pthread​​_exit when the mutex isn't released by this thread.




I hope to receive your answer, thank you.

JinCheng Li






PS: This is a simple case for deadlock but musl still do something with robust_list such as waking up the waiters:
&nbsp;     pthread_mutex_t mutex;
void *dl_test1()
{
      pthread_mutex_lock(&amp;mutex);

      sleep(10);
      return 0;
}
      void *dl_test2()

{
      pthread_mutex_lock(&amp;mutex);

      return 0;
}
int main()
{
      void *res;

      pthread_mutexattr_t attr;

      pthread_mutexattr_init(&amp;attr);

      pthread_mutexattr_settype(&amp;attr, PTHREAD_MUTEX_RECURSIVE);

      pthread_mutex_init(&amp;mutex,&nbsp;&amp;attr);

      pthread_t thd1;

      pthread_t thd2;

      pthread_create(&amp;thd1, NULL, dl_test1, NULL);

      pthread_create(&amp;thd2, NULL, dl_test2, NULL);

      pthread_join(thd1, &amp;res);

      pthread_join(thd2, &amp;res);

      return 0;
}
Content of type "text/html" skipped

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.