public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
* [PATCH] fix race condition in List_insert
@ 2021-08-23 14:27 Aleksandr Malikov
  2021-08-23 15:02 ` Corinna Vinschen
  0 siblings, 1 reply; 6+ messages in thread
From: Aleksandr Malikov @ 2021-08-23 14:27 UTC (permalink / raw)
  To: cygwin-patches

From: Aleksand Malikov <schn27@gmail.com>

Revert mx parameter and mutex lock while operating the list.
Mutex was removed with 94d24160 informing that:
'Use InterlockedCompareExchangePointer to ensure race safeness
without using a mutex.'

But it does not.

Calling pthread_mutex_init and pthread_mutex_destroy from two or
more threads occasionally leads to hang in pthread_mutex_destroy.

To not change the behaviour of other cases where List_insert was called,
List_insert_nolock is added.
---
 winsup/cygwin/thread.cc |  8 ++++----
 winsup/cygwin/thread.h  | 17 +++++++++++++++--
 2 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/winsup/cygwin/thread.cc b/winsup/cygwin/thread.cc
index 4d0ea27..7c6a919 100644
--- a/winsup/cygwin/thread.cc
+++ b/winsup/cygwin/thread.cc
@@ -1595,7 +1595,7 @@ pthread_rwlock::add_reader ()
 {
   RWLOCK_READER *rd = new RWLOCK_READER;
   if (rd)
-    List_insert (readers, rd);
+    List_insert_nolock (readers, rd);
   return rd;
 }
 
@@ -2165,7 +2165,7 @@ pthread::atfork (void (*prepare)(void), void (*parent)(void), void (*child)(void
   if (prepcb)
   {
     prepcb->cb = prepare;
-    List_insert (MT_INTERFACE->pthread_prepare, prepcb);
+    List_insert_nolock (MT_INTERFACE->pthread_prepare, prepcb);
   }
   if (parentcb)
   {
@@ -2174,7 +2174,7 @@ pthread::atfork (void (*prepare)(void), void (*parent)(void), void (*child)(void
     while (*t)
       t = &(*t)->next;
     /* t = pointer to last next in the list */
-    List_insert (*t, parentcb);
+    List_insert_nolock (*t, parentcb);
   }
   if (childcb)
   {
@@ -2183,7 +2183,7 @@ pthread::atfork (void (*prepare)(void), void (*parent)(void), void (*child)(void
     while (*t)
       t = &(*t)->next;
     /* t = pointer to last next in the list */
-    List_insert (*t, childcb);
+    List_insert_nolock (*t, childcb);
   }
   return 0;
 }
diff --git a/winsup/cygwin/thread.h b/winsup/cygwin/thread.h
index 6b699cc..ddb2d7d 100644
--- a/winsup/cygwin/thread.h
+++ b/winsup/cygwin/thread.h
@@ -111,7 +111,20 @@ typedef enum
 } verifyable_object_state;
 
 template <class list_node> inline void
-List_insert (list_node *&head, list_node *node)
+List_insert (fast_mutex &mx, list_node *&head, list_node *node)
+{
+  if (!node)
+    return;
+  mx.lock ();
+  do
+    node->next = head;
+  while (InterlockedCompareExchangePointer ((PVOID volatile *) &head,
+					    node, node->next) != node->next);
+  mx.unlock ();
+}
+
+template <class list_node> inline void
+List_insert_nolock (list_node *&head, list_node *node)
 {
   if (!node)
     return;
@@ -163,7 +176,7 @@ template <class list_node> class List
 
   void insert (list_node *node)
   {
-    List_insert (head, node);
+    List_insert (mx, head, node);
   }
 
   void remove (list_node *node)
-- 
2.32.0.windows.2


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fix race condition in List_insert
  2021-08-23 14:27 [PATCH] fix race condition in List_insert Aleksandr Malikov
@ 2021-08-23 15:02 ` Corinna Vinschen
  2021-08-23 15:48   ` Александр Маликов
  0 siblings, 1 reply; 6+ messages in thread
From: Corinna Vinschen @ 2021-08-23 15:02 UTC (permalink / raw)
  To: Aleksandr Malikov; +Cc: cygwin-patches

Hi Aleksandr,


thanks for the patch.

On Aug 23 17:27, Aleksandr Malikov wrote:
> From: Aleksand Malikov <schn27@gmail.com>
> 
> Revert mx parameter and mutex lock while operating the list.
> Mutex was removed with 94d24160 informing that:
> 'Use InterlockedCompareExchangePointer to ensure race safeness
> without using a mutex.'
> 
> But it does not.
> 
> Calling pthread_mutex_init and pthread_mutex_destroy from two or
> more threads occasionally leads to hang in pthread_mutex_destroy.

Do you have a simple testcase in plain C, by any chance?

> To not change the behaviour of other cases where List_insert was called,
> List_insert_nolock is added.

LGTM.  Can you please provide a copyright waiver per
https://cygwin.com/contrib.html.  See the winsup/CONTRIBUTORS file.


Thanks,
Corinna

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fix race condition in List_insert
  2021-08-23 15:02 ` Corinna Vinschen
@ 2021-08-23 15:48   ` Александр Маликов
  2021-08-23 18:08     ` Corinna Vinschen
  0 siblings, 1 reply; 6+ messages in thread
From: Александр Маликов @ 2021-08-23 15:48 UTC (permalink / raw)
  To: cygwin-patches, Aleksandr Malikov

https://gist.github.com/schn27/23b47563b429aaaad5ac315d05a43a11

The test is failed if "Thread #X timeout" is printed and -1 returned. This
happens on my laptop in about several minutes.
The test is passed if it runs infinitely.


пн, 23 авг. 2021 г. в 18:02, Corinna Vinschen <corinna-cygwin@cygwin.com>:

> Hi Aleksandr,
>
>
> thanks for the patch.
>
> On Aug 23 17:27, Aleksandr Malikov wrote:
> > From: Aleksand Malikov <schn27@gmail.com>
> >
> > Revert mx parameter and mutex lock while operating the list.
> > Mutex was removed with 94d24160 informing that:
> > 'Use InterlockedCompareExchangePointer to ensure race safeness
> > without using a mutex.'
> >
> > But it does not.
> >
> > Calling pthread_mutex_init and pthread_mutex_destroy from two or
> > more threads occasionally leads to hang in pthread_mutex_destroy.
>
> Do you have a simple testcase in plain C, by any chance?
>
> > To not change the behaviour of other cases where List_insert was called,
> > List_insert_nolock is added.
>
> LGTM.  Can you please provide a copyright waiver per
> https://cygwin.com/contrib.html.  See the winsup/CONTRIBUTORS file.
>
>
> Thanks,
> Corinna
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fix race condition in List_insert
  2021-08-23 15:48   ` Александр Маликов
@ 2021-08-23 18:08     ` Corinna Vinschen
  2021-08-23 19:05       ` Александр Маликов
  0 siblings, 1 reply; 6+ messages in thread
From: Corinna Vinschen @ 2021-08-23 18:08 UTC (permalink / raw)
  To: Александр
	Маликов
  Cc: cygwin-patches

Hi Александр,

On Aug 23 18:48, Александр Маликов wrote:
> https://gist.github.com/schn27/23b47563b429aaaad5ac315d05a43a11
> 
> The test is failed if "Thread #X timeout" is printed and -1 returned. This
> happens on my laptop in about several minutes.
> The test is passed if it runs infinitely.

Yup, that shows the problem nicely.

> > LGTM.  Can you please provide a copyright waiver per
> > https://cygwin.com/contrib.html.  See the winsup/CONTRIBUTORS file.

Just provide your BSD-2 waiver and I'll push your patch.


Thanks,
Corinna

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fix race condition in List_insert
  2021-08-23 18:08     ` Corinna Vinschen
@ 2021-08-23 19:05       ` Александр Маликов
  2021-08-24  7:57         ` Corinna Vinschen
  0 siblings, 1 reply; 6+ messages in thread
From: Александр Маликов @ 2021-08-23 19:05 UTC (permalink / raw)
  To: cygwin-patches,
	Александр
	Маликов

I'm completely new to all this BSD-2 stuff etc... What exactly should I do
to 'provide my BSD-2 waiver'?
This patch is licenced under 2-clause BSD.
Is it enough?

пн, 23 авг. 2021 г. в 21:08, Corinna Vinschen <corinna-cygwin@cygwin.com>:

> Hi Александр,
>
> On Aug 23 18:48, Александр Маликов wrote:
> > https://gist.github.com/schn27/23b47563b429aaaad5ac315d05a43a11
> >
> > The test is failed if "Thread #X timeout" is printed and -1 returned.
> This
> > happens on my laptop in about several minutes.
> > The test is passed if it runs infinitely.
>
> Yup, that shows the problem nicely.
>
> > > LGTM.  Can you please provide a copyright waiver per
> > > https://cygwin.com/contrib.html.  See the winsup/CONTRIBUTORS file.
>
> Just provide your BSD-2 waiver and I'll push your patch.
>
>
> Thanks,
> Corinna
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fix race condition in List_insert
  2021-08-23 19:05       ` Александр Маликов
@ 2021-08-24  7:57         ` Corinna Vinschen
  0 siblings, 0 replies; 6+ messages in thread
From: Corinna Vinschen @ 2021-08-24  7:57 UTC (permalink / raw)
  To: Александр
	Маликов
  Cc: cygwin-patches

Hi Александр,

On Aug 23 22:05, Александр Маликов wrote:
> I'm completely new to all this BSD-2 stuff etc... What exactly should I do
> to 'provide my BSD-2 waiver'?
> This patch is licenced under 2-clause BSD.
> Is it enough?

Yes, that's ok.  You just have to write that all your Cygwin patches
will be licensed under BSD-2 as outlined in winsup/CONTRIBUTORS.  I'll
add you to the file and that's it for the future.

I pushed your patch.


Thanks,
Corinna

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-08-24  7:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-23 14:27 [PATCH] fix race condition in List_insert Aleksandr Malikov
2021-08-23 15:02 ` Corinna Vinschen
2021-08-23 15:48   ` Александр Маликов
2021-08-23 18:08     ` Corinna Vinschen
2021-08-23 19:05       ` Александр Маликов
2021-08-24  7:57         ` Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).