From owner-freebsd-hackers@FreeBSD.ORG Wed Sep 26 09:11:12 2012 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id ECCF61065673; Wed, 26 Sep 2012 09:11:11 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from mail.zoral.com.ua (mx0.zoral.com.ua [91.193.166.200]) by mx1.freebsd.org (Postfix) with ESMTP id 6D2F78FC18; Wed, 26 Sep 2012 09:11:11 +0000 (UTC) Received: from skuns.kiev.zoral.com.ua (localhost [127.0.0.1]) by mail.zoral.com.ua (8.14.2/8.14.2) with ESMTP id q8Q9B9tb070664; Wed, 26 Sep 2012 12:11:09 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: from deviant.kiev.zoral.com.ua (kostik@localhost [127.0.0.1]) by deviant.kiev.zoral.com.ua (8.14.5/8.14.5) with ESMTP id q8Q9AvP9084337; Wed, 26 Sep 2012 12:10:57 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: (from kostik@localhost) by deviant.kiev.zoral.com.ua (8.14.5/8.14.5/Submit) id q8Q9AvIg084336; Wed, 26 Sep 2012 12:10:57 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: deviant.kiev.zoral.com.ua: kostik set sender to kostikbel@gmail.com using -f Date: Wed, 26 Sep 2012 12:10:57 +0300 From: Konstantin Belousov To: Andriy Gapon Message-ID: <20120926091057.GH35915@deviant.kiev.zoral.com.ua> References: <5062B971.30203@FreeBSD.org> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="a8sldprk+5E/pDEv" Content-Disposition: inline In-Reply-To: <5062B971.30203@FreeBSD.org> User-Agent: Mutt/1.5.21 (2010-09-15) X-Virus-Scanned: clamav-milter 0.95.2 at skuns.kiev.zoral.com.ua X-Virus-Status: Clean X-Spam-Status: No, score=-4.0 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on skuns.kiev.zoral.com.ua Cc: freebsd-hackers Subject: Re: monitor+mwait and volatile-ish X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 26 Sep 2012 09:11:12 -0000 --a8sldprk+5E/pDEv Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Sep 26, 2012 at 11:14:41AM +0300, Andriy Gapon wrote: >=20 > Typical x86 MONITOR+MWAIT is like this (taken from Intel manual): >=20 > EAX =3D Logical Address(Trigger) > ECX =3D 0 (*Hints *) > EDX =3D 0 (* Hints *) > IF ( !trigger_store_happened) { > MONITOR EAX, ECX, EDX > IF ( !trigger_store_happened ) { > MWAIT EAX, ECX > } > } >=20 > In FreeBSD we have this helper function for MONITOR: > static __inline void > cpu_monitor(const void *addr, u_long extensions, u_int hints) > { >=20 > __asm __volatile("monitor" > : : "a" (addr), "c" (extensions), "d" (hints)); > } >=20 > Now, let's assume that 'Trigger' is a global variable and > 'trigger_store_happened' is a check for a particular value in this variab= le. >=20 > Then, with current state of matters, we must either declare the global va= riable > to be volatile or use a volatile cast in the second IF. Otherwise, a com= piler > is free to assume that the variable doesn't change between the first and = the > second IF and drop the second IF altogether. And that would make MONITOR= +MWAIT > to miss an event if it happens "between" the first check and MONITOR. >=20 > So what's my point. > - using volatile variable with cpu_monitor requires DEVOLATILE to silence > compiler warning about discarding volatile; this is unnecessary code bloat > - adding volatile cast in the checks is easy to forget and adds code bloat >=20 > Possible improvements: > - make the argument of cpu_monitor be 'const volatile void *', the most > permissive type; this would also be a hint that variable should be volati= le > - add some magic dust to cpu_monitor that would tell compiler to not cache > the variable; right now I can only think of the "memory" constraint, bu= t it > seems to be too big of a hummer >=20 > What do you think about this? You might claim that the asm writes to *addr by specifying it in the output constraint. This should fool the compiler into reload *addr after the monitor execution. --a8sldprk+5E/pDEv Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (FreeBSD) iEYEARECAAYFAlBixqEACgkQC3+MBN1Mb4g6hwCdHLujvZ0OezxypmrLw8izSFsA 3WsAn1NLKzqqdKnwjmqxY67Sx+HuLx49 =dBY0 -----END PGP SIGNATURE----- --a8sldprk+5E/pDEv--