From owner-svn-src-all@FreeBSD.ORG Thu Jul 26 17:59:58 2012 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 21222106566C; Thu, 26 Jul 2012 17:59:58 +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 7CF858FC16; Thu, 26 Jul 2012 17:59:57 +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 q6QI00Q1078733; Thu, 26 Jul 2012 21:00:00 +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 q6QHxlUd098685; Thu, 26 Jul 2012 20:59:47 +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 q6QHxlOB098684; Thu, 26 Jul 2012 20:59:47 +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: Thu, 26 Jul 2012 20:59:47 +0300 From: Konstantin Belousov To: Bruce Evans Message-ID: <20120726175947.GZ2676@deviant.kiev.zoral.com.ua> References: <500F9E22.4080608@FreeBSD.org> <20120725102130.GH2676@deviant.kiev.zoral.com.ua> <500FE6AE.8070706@FreeBSD.org> <20120726001659.M5406@besplex.bde.org> <50102C94.9030706@FreeBSD.org> <20120725180537.GO2676@deviant.kiev.zoral.com.ua> <50103C61.8040904@FreeBSD.org> <20120726170837.Q2536@besplex.bde.org> <20120726104918.GW2676@deviant.kiev.zoral.com.ua> <20120726213001.K3621@besplex.bde.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="A5JfamB56Xzo2P18" Content-Disposition: inline In-Reply-To: <20120726213001.K3621@besplex.bde.org> User-Agent: Mutt/1.4.2.3i 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: Jim Harris , src-committers@freebsd.org, svn-src-all@freebsd.org, Andriy Gapon , svn-src-head@freebsd.org, Jung-uk Kim Subject: Re: svn commit: r238755 - head/sys/x86/x86 X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 26 Jul 2012 17:59:58 -0000 --A5JfamB56Xzo2P18 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Jul 26, 2012 at 10:30:51PM +1000, Bruce Evans wrote: > On Thu, 26 Jul 2012, Konstantin Belousov wrote: >=20 > >On Thu, Jul 26, 2012 at 05:35:23PM +1000, Bruce Evans wrote: > >>In fact, there is always a full documented serialization instruction > >>for syscalls, except maybe in FreeBSD-1 compat code on i386, at > >>least on Athlon64. i386 syscalls use int 0x80 (except in FreeBSD-1 > >>compat code they use lcalls, and the iret necessary to return from > >>this is serializing on at least Athlon64. amd64 syscalls use > >>sysenter/sysret. sysret isn't serializing (like far returns), at least > >>on Athlon64, but at least in FreeBSD, the syscall implementation uses > >>at least 2 swapgs's (one on entry and one just before the sysret), and > >>swapgs is serializing, at least on Athlon64. > >Yes, SWAPGS is not documented as serializing on Intels. I reviewed >=20 > Isn't that too incompatible? After the SYSRETQ story, we should not be surprised. I believe I saw the difference between SWAPGS specifications earlier. >=20 > >the whole syscall sequence for e.g. gettimeofday(2), and there is no > >serialization point for fast path. E.g. ast would add locking and thus > >serialization, as well as return by IRET, but fast path on amd64 has > >no such things. >=20 > >>>This function was moved around from time to time and now it sits here: > >>> > >>>http://git.kernel.org/?p=3Dlinux/kernel/git/torvalds/linux.git;a=3Dblo= b_plain;f=3Darch/x86/vdso/vclock_gettime.c > >>> > >>>It still carries one barrier before rdtsc. Please see the comments. > >> > >>For safety, you probably need to use the slowest (cpuid) method. Linux > >>seems to be just using fences that are observed to work. > >No, there is explicit mention of the recommended barriers in the vendor > >documentation, which is LFENCE for Intels, and MFENCE for AMDs. My patch > >just follows what is suggested in documentation. >=20 > But you say later theat CPUID is needed (instead of just lock?). The > original Athlon64 manual doesn't seem to mention MFENCE for RTDSC. > Maybe later manuals clarify that MFENCE works on old CPUs too. >=20 > >[Replying to other mail in-place, the thread goes wild] >=20 > Too much quoting :-). >=20 > >On Thu, Jul 26, 2012 at 04:25:01PM +1000, Bruce Evans wrote: > >>... > >>For the threaded case, there has to something for the accesses to be > >>provably ordered. It is hard to see how the something can be strong > >>enough unless it serializes all thread state in A and B. The rdtsc > >>state is not part of the thread state as know to APIs, but it is hard > >>to see how threads can serialize themselves without also serializing > >>the TSC. > >TSC timer read is not synchronized, and I found the Linux test for the > >thing I described above. Adopted version is available at > >http://people.freebsd.org/~kib/misc/time-warp-test.c. > >It shall be compiled in 32bit mode only. >=20 > My point is that it will normally be synchronized by whatever the threads > do to provide synchronization for themself. Only the case of a single > thread doing sequential timer reads should expect the reads to be > monotonic without any explicit synchronization. I hope this case doesn't > require stalling everything in low-level code. >=20 > >On my Nehalem workstation, I get enormous amount of wraps reported for > >RDTSC without CPUID. Adding CPUID back fixes the issue. So at least on > >Nehalems (and probably Westmere, I will test later today) RDTSC can even > >pass LOCKed instructions. >=20 > Oh, you mean with the test program, that it needs CPUID because it only > has locks and no fences and its CPUID is commented out. Yes, CPUID or LFENCE is enough to fix the failure. >=20 > >Curiously enough, SandyBridge is sane and reports zero wraps, it seems > >Intel fixed the bug. >=20 > The original Athlon64 manual doesn't seem to mention locks being sufficie= nt > any more than it mentions fences. >=20 > >>I care about timestamps being ordered more than most people, and tried > >>to kill the get*time() APIs because they are weakly ordered relative > >>to the non-get variants (they return times in the past, and there is > >>no way to round down to get consistent times). I tried to fix them > >>by adding locking and updating them to the latest time whenever a > >>non-get variant gives a later time (by being used). This was too slow, > >>and breaks the design criteria that timecounter calls should not use > >>any explicit locking. However, if you want slowness, then you can get > >>it similarly by fixing the monotonicity of rdtsc in software. I think > >>I just figured out how to do this with the same slowness as serializati= on, > >>if a locked instruction serialzes; maybe less otherwise: > >> > >>spin: > >> ptsc =3D prev_tsc; /* memory -> local (intentionally !atomic) */ > >> tsc =3D rdtsc(); /* only 32 bits for timecounters */ > >> if (tsc <=3D ptsc) { /* I forgot about wrap at first -- see below > >> */ > >> /* > >> * It went backwards, or stopped. Could handle more > >> * completely, starting with panic() to see if this > >> * happens at all. > >> */ > >> return (ptsc); /* stopped is better than backwards */ > >> } > >> /* Usual case; update (32 bits). */ > >> if (atomic_cmpset_int(&prev_tsc, ptsc, tsc)) > >> return (tsc); > >> goto spin; > >I do not understand this. Algorithm is clear, but what you propose is > >very heavy-weight comparing with adding just LFENCE or MFENCE before rdt= sc. > >First, the cache-line for prev_tsc becomes heavy-contended. Second, CAS > >is expensive. LFENCE is fully local to the core it executes on. >=20 > I expect the contention to be rare, but then optimization isn't important > either. >=20 > But if the problem is fully local, as it apparently is for fences to > fix it, then prev_tsc can be per-CPU with a non-atomic cmpset to access > it. We don't care if rdtsc gives an old value due to some delay in > copying the result to EDX:EAX any more than we care about an old value > due to being interrupted. The case where we are interrupted, and > context-switched, and come back on a different CPU, is especially > interesting. Then we may have an old tsc value from another CPU. > Sometimes we detect that it is old for the new CPU, sometimes not. > There is a problem in theory but I think none in practice. The switch > could set a flag to tell us to loop (set prev_tsc to a sentinel value), > and it accidentally already does, except with serious our of orderness: > switches happen to always call the TSC if the TSC is usable, to maintain > the the pcpu switchtime variable. The call for this will give a tsc > value for the new CPU that is in advance of the old one, provided there > all the TSC are in sync and there is sufficient monotonicity across > CPUs. Then the loop will see that its tsc is old, and repeat. >=20 > I am only half serious in proposing the above. If you want to be slow > then you can do useful work like ensuring monotonicity across all CPUs > in much the same time that is wasted by stalling the hardware until > everything is serialized. But monotonicity across all cores (at least for single-package systems) is exactly what the patch ensures. --A5JfamB56Xzo2P18 Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (FreeBSD) iEYEARECAAYFAlARhZMACgkQC3+MBN1Mb4jkIgCgsBCOqyfjUOHE+c3s5tSlUOOs pykAn3RM8kQDvBuxCSMaRqMS0fzL6dq2 =2hGT -----END PGP SIGNATURE----- --A5JfamB56Xzo2P18--