NetBSD Problem Report #57751

From www@netbsd.org  Mon Dec  4 17:42:39 2023
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 68FD71A9238
	for <gnats-bugs@gnats.NetBSD.org>; Mon,  4 Dec 2023 17:42:39 +0000 (UTC)
Message-Id: <20231204174237.CE5861A9239@mollari.NetBSD.org>
Date: Mon,  4 Dec 2023 17:42:37 +0000 (UTC)
From: nruslan_devel@yahoo.com
Reply-To: nruslan_devel@yahoo.com
To: gnats-bugs@NetBSD.org
Subject: cv_signal and cv_broadcast without mutex
X-Send-Pr-Version: www-1.0

>Number:         57751
>Category:       kern
>Synopsis:       cv_signal and cv_broadcast without mutex
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    kern-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Dec 04 17:45:00 +0000 2023
>Last-Modified:  Tue Dec 12 09:55:01 +0000 2023
>Originator:     Ruslan Nikolaev
>Release:        10.0
>Organization:
Penn State University
>Environment:
>Description:
It seems that recently the semantics of cv_signal and cv_broadcast was changed: https://www.mail-archive.com/source-changes-d@netbsd.org/msg59675.html

 The mutex passed to the wait function
 .Po Fa mtx Pc
-must also be held when calling
-.Fn cv_signal .
+should be held or have been released immediately before
+.Fn cv_signal
+is called.

Is there any good reason for this change and allowing to NOT hold the mutex? In my opinion, requiring the mutex to be always locked is a very good idea since it typically leads to better and more predictable performance (e.g., the condition is less likely to change by the time cv_wait wakes up, i.e., less likely to have spurious wake ups if someone is able to grab the mutex before cv_signal is sent and changes the condition). In addition, POSIX recommends holding the mutex for more predictable scheduling: https://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_cond_signal.html

Also, it forces the programmer to think carefully what mutexes are being used in cv_wait, etc.

I actually prefer *requiring* rather than just recommending to always hold the mutex because it can enable additional optimizations. For example, in rumprun-smp, we were able to piggyback on the (implicit) mutex (assuming that it is still locked when cv_signal is called) to manipulate a blocking queue without having another lock:
https://github.com/ssrg-vt/rumprun-smp/blob/master/lib/libbmk_rumpuser/rumpuser_synch.c
>How-To-Repeat:

>Fix:

>Audit-Trail:
From: "Jonathan A. Kollasch" <jakllsch@netbsd.org>
To: gnats-bugs@netbsd.org
Cc: 
Subject: Re: kern/57751: cv_signal and cv_broadcast without mutex
Date: Mon, 4 Dec 2023 16:09:52 -0600

 For archival posterity,
 https://www.mail-archive.com/source-changes-d@netbsd.org/msg59675.html
 is the same as:

 https://mail-index.netbsd.org/source-changes/2023/09/07/msg147474.html

 , and contains:

 """
 To generate a diff of this commit:
 cvs rdiff -u -r1.30 -r1.31 src/share/man/man9/condvar.9
 """

From: Ruslan Nikolaev <nruslan_devel@yahoo.com>
To: "kern-bug-people@netbsd.org" <kern-bug-people@netbsd.org>, 
	"gnats-admin@netbsd.org" <gnats-admin@netbsd.org>, 
	"netbsd-bugs@netbsd.org" <netbsd-bugs@netbsd.org>, 
	"gnats-bugs@netbsd.org" <gnats-bugs@netbsd.org>
Cc: 
Subject: Re: kern/57751: cv_signal and cv_broadcast without mutex
Date: Tue, 12 Dec 2023 08:02:28 +0000 (UTC)

 ------=_Part_200458_726680841.1702368148593
 Content-Type: text/plain; charset=UTF-8
 Content-Transfer-Encoding: quoted-printable

  I was wondering if anyone can follow up on this issue and who this questio=
 n should=C2=A0 be directed to?
 Ruslan
     On Monday, December 4, 2023 at 05:15:03 PM EST, Jonathan A. Kollasch <j=
 akllsch@netbsd.org> wrote: =20
 =20
  The following reply was made to PR kern/57751; it has been noted by GNATS.

 From: "Jonathan A. Kollasch" <jakllsch@netbsd.org>
 To: gnats-bugs@netbsd.org
 Cc:=20
 Subject: Re: kern/57751: cv_signal and cv_broadcast without mutex
 Date: Mon, 4 Dec 2023 16:09:52 -0600

  For archival posterity,
  https://www.mail-archive.com/source-changes-d@netbsd.org/msg59675.html
  is the same as:
 =20
  https://mail-index.netbsd.org/source-changes/2023/09/07/msg147474.html
 =20
  , and contains:
 =20
  """
  To generate a diff of this commit:
  cvs rdiff -u -r1.30 -r1.31 src/share/man/man9/condvar.9
  """
 =20
  =20
 ------=_Part_200458_726680841.1702368148593
 Content-Type: text/html; charset=UTF-8
 Content-Transfer-Encoding: quoted-printable

 <html><head></head><body><div class=3D"ydpb8aa395byahoo-style-wrap" style=
 =3D"font-family:Helvetica Neue, Helvetica, Arial, sans-serif;font-size:13px=
 ;"><div></div>
         <div dir=3D"ltr" data-setdir=3D"false">I was wondering if anyone ca=
 n follow up on this issue and who this question should&nbsp; be directed to=
 ?</div><div dir=3D"ltr" data-setdir=3D"false"><br></div><div dir=3D"ltr" da=
 ta-setdir=3D"false">Ruslan</div><div><br></div>
        =20
         </div><div id=3D"yahoo_quoted_2556859242" class=3D"yahoo_quoted">
             <div style=3D"font-family:'Helvetica Neue', Helvetica, Arial, s=
 ans-serif;font-size:13px;color:#26282a;">
                =20
                 <div>
                     On Monday, December 4, 2023 at 05:15:03 PM EST, Jonatha=
 n A. Kollasch &lt;jakllsch@netbsd.org&gt; wrote:
                 </div>
                 <div><br></div>
                 <div><br></div>
                 <div><div dir=3D"ltr">The following reply was made to PR ke=
 rn/57751; it has been noted by GNATS.<br></div><div dir=3D"ltr"><br></div><=
 div dir=3D"ltr">From: "Jonathan A. Kollasch" &lt;<a ymailto=3D"mailto:jakll=
 sch@netbsd.org" href=3D"mailto:jakllsch@netbsd.org">jakllsch@netbsd.org</a>=
 &gt;<br></div><div dir=3D"ltr">To: <a ymailto=3D"mailto:gnats-bugs@netbsd.o=
 rg" href=3D"mailto:gnats-bugs@netbsd.org">gnats-bugs@netbsd.org</a><br></di=
 v><div dir=3D"ltr">Cc: <br></div><div dir=3D"ltr">Subject: Re: kern/57751: =
 cv_signal and cv_broadcast without mutex<br></div><div dir=3D"ltr">Date: Mo=
 n, 4 Dec 2023 16:09:52 -0600<br></div><div dir=3D"ltr"><br></div><div dir=
 =3D"ltr"> For archival posterity,<br></div><div dir=3D"ltr"> <a href=3D"htt=
 ps://www.mail-archive.com/source-changes-d@netbsd.org/msg59675.html" target=
 =3D"_blank">https://www.mail-archive.com/source-changes-d@netbsd.org/msg596=
 75.html</a><br></div><div dir=3D"ltr"> is the same as:<br></div><div dir=3D=
 "ltr"> <br></div><div dir=3D"ltr"> <a href=3D"https://mail-index.netbsd.org=
 /source-changes/2023/09/07/msg147474.html" target=3D"_blank">https://mail-i=
 ndex.netbsd.org/source-changes/2023/09/07/msg147474.html</a><br></div><div =
 dir=3D"ltr"> <br></div><div dir=3D"ltr"> , and contains:<br></div><div dir=
 =3D"ltr"> <br></div><div dir=3D"ltr"> """<br></div><div dir=3D"ltr"> To gen=
 erate a diff of this commit:<br></div><div dir=3D"ltr"> cvs rdiff -u -r1.30=
  -r1.31 src/share/man/man9/condvar.9<br></div><div dir=3D"ltr"> """<br></di=
 v><div dir=3D"ltr"> <br></div></div>
             </div>
         </div></body></html>
 ------=_Part_200458_726680841.1702368148593--

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.47 2022/09/11 19:34:41 kim Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2023 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.