From owner-freebsd-hackers@FreeBSD.ORG Wed Jun 3 14:56:19 2015 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 91298CE4 for ; Wed, 3 Jun 2015 14:56:19 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 04CA71BF2 for ; Wed, 3 Jun 2015 14:56:18 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kostik@localhost [127.0.0.1]) by kib.kiev.ua (8.14.9/8.14.9) with ESMTP id t53Eu67w097596 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO); Wed, 3 Jun 2015 17:56:06 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.9.2 kib.kiev.ua t53Eu67w097596 Received: (from kostik@localhost) by tom.home (8.14.9/8.14.9/Submit) id t53Eu660097595; Wed, 3 Jun 2015 17:56:06 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Wed, 3 Jun 2015 17:56:06 +0300 From: Konstantin Belousov To: Sebastian Huber Cc: freebsd-hackers@freebsd.org, phk@phk.freebsd.dk Subject: Re: [PATCH v2] timecounters: Fix timehand generation read/write Message-ID: <20150603145606.GW2499@kib.kiev.ua> References: <1433331966-27548-1-git-send-email-sebastian.huber@embedded-brains.de> <1433332368-27645-1-git-send-email-sebastian.huber@embedded-brains.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1433332368-27645-1-git-send-email-sebastian.huber@embedded-brains.de> User-Agent: Mutt/1.5.23 (2014-03-12) X-Spam-Status: No, score=-2.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on tom.home X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 03 Jun 2015 14:56:19 -0000 On Wed, Jun 03, 2015 at 01:52:48PM +0200, Sebastian Huber wrote: > The compiler is free to re-order load/store instructions to non-volatile > variables around a load/store of a volatile variable. So the volatile > generation counter is insufficent. In addition tests on a Freescale > T4240 platform with 24 PowerPC processors showed that real memory > barriers are required. Compiler memory barriers are not enough. > > For the test the timehand count was reduced to one with 10000 > tc_windup() calls per second. The timehand memory location was adjusted > so that the th_generation field was on its own cache line. > --- > > v2: Don't use tc_getgen() in tc_windup() since in the only writer there is no > need for a read memory barrier. > > sys/kern/kern_tc.c | 100 +++++++++++++++++++++++++++++++---------------------- > 1 file changed, 59 insertions(+), 41 deletions(-) > > diff --git a/sys/kern/kern_tc.c b/sys/kern/kern_tc.c > index 9dca0e8..bb9614a 100644 > --- a/sys/kern/kern_tc.c > +++ b/sys/kern/kern_tc.c > @@ -71,7 +71,7 @@ struct timehands { > struct timeval th_microtime; > struct timespec th_nanotime; > /* Fields not to be copied in tc_windup start with th_generation. */ > - volatile u_int th_generation; > + u_int th_generation; > struct timehands *th_next; > }; > > @@ -189,6 +189,24 @@ tc_delta(struct timehands *th) > tc->tc_counter_mask); > } > > +static u_int > +tc_getgen(struct timehands *th) > +{ > + u_int gen; > + > + gen = th->th_generation; > + rmb(); > + return (gen); Why not return (atomic_load_acq_int(&th->th_generation); ? > +} > + > +static void > +tc_setgen(struct timehands *th, u_int newgen) > +{ > + > + wmb(); > + th->th_generation = newgen; And there, atomic_store_rel_int(&th->th_generation, newgen); ?