From owner-freebsd-current@freebsd.org Fri Sep 23 11:59:54 2016 Return-Path: Delivered-To: freebsd-current@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 4EEBABE505F for ; Fri, 23 Sep 2016 11:59:54 +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 C3E00A1F for ; Fri, 23 Sep 2016 11:59:53 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.15.2/8.15.2) with ESMTPS id u8NBxm9o065798 (version=TLSv1 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO); Fri, 23 Sep 2016 14:59:48 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua u8NBxm9o065798 Received: (from kostik@localhost) by tom.home (8.15.2/8.15.2/Submit) id u8NBxm9V065797; Fri, 23 Sep 2016 14:59:48 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Fri, 23 Sep 2016 14:59:48 +0300 From: Konstantin Belousov To: Hans Petter Selasky Cc: FreeBSD Current Subject: Re: Question about wmb() and rmb() and task switching Message-ID: <20160923115948.GX38409@kib.kiev.ua> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.6.1 (2016-04-27) 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-current@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 23 Sep 2016 11:59:54 -0000 On Fri, Sep 23, 2016 at 01:46:15PM +0200, Hans Petter Selasky wrote: > Hi, > > Does use of wmb() and rmb() for amd64 as defined in > sys/amd64/include/atomic.h required use of critical_enter()/critical_exit(). > > I was looking at the code in sys/amd64/amd64/cpu_switch.S which switches > between threads and I don't see any "sfence" instructions in there. > > Given the following piece of dummy code: > > var_a = 1; > var_b = 2; > wmb(); > > If there is a task switch between writing var_a and var_b so that the > thread in question continues executing on another core, can it happen > that the write to var_a is not flushed when wmb() is executed? > > var_a = 1; > > var_b = 2; > wmb(); cpu_switch() guarantees that the context switch behaves as a full barrier. In other words, all side-effects which are before context switch in the program order, become globally visible after it (if address spaces are switched), context switch behaves as the strongest barrier among all provided by the hardware. For amd64, there are more than one serialization instructions executed during the switch, including the access to cr3 and thread unlock for old thread. If the context switch is involuntary, then the interrupt itself also provides serialization point. Note that FreeBSD uses C11 memory model of load acquire/store release, not the linux-like rmb/wmb. atomic acq/rel ops are strongly preferred over the *mb(), which mostly exists as the initial driver porting aid.