From owner-freebsd-hackers@FreeBSD.ORG Fri Sep 17 15:23:48 2010 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 792801065672 for ; Fri, 17 Sep 2010 15:23:48 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from cyrus.watson.org (cyrus.watson.org [65.122.17.42]) by mx1.freebsd.org (Postfix) with ESMTP id 48F188FC12 for ; Fri, 17 Sep 2010 15:23:48 +0000 (UTC) Received: from bigwig.baldwin.cx (66.111.2.69.static.nyinternet.net [66.111.2.69]) by cyrus.watson.org (Postfix) with ESMTPSA id F210146BA0; Fri, 17 Sep 2010 11:23:47 -0400 (EDT) Received: from jhbbsd.localnet (smtp.hudson-trading.com [209.249.190.9]) by bigwig.baldwin.cx (Postfix) with ESMTPSA id 1B4BC8A04F; Fri, 17 Sep 2010 11:23:47 -0400 (EDT) From: John Baldwin To: Benjamin Kaduk Date: Fri, 17 Sep 2010 09:02:18 -0400 User-Agent: KMail/1.13.5 (FreeBSD/7.3-CBSD-20100819; KDE/4.4.5; amd64; ; ) References: <20100915134415.GA23727@pm513-1.comsys.ntu-kpi.kiev.ua> <201009161416.05759.jhb@freebsd.org> In-Reply-To: MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201009170902.18748.jhb@freebsd.org> X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.0.1 (bigwig.baldwin.cx); Fri, 17 Sep 2010 11:23:47 -0400 (EDT) X-Virus-Scanned: clamav-milter 0.95.1 at bigwig.baldwin.cx X-Virus-Status: Clean X-Spam-Status: No, score=-2.6 required=4.2 tests=AWL,BAYES_00 autolearn=ham version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on bigwig.baldwin.cx Cc: freebsd-hackers@freebsd.org Subject: Re: Questions about mutex implementation in kern/kern_mutex.c 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: Fri, 17 Sep 2010 15:23:48 -0000 On Thursday, September 16, 2010 11:24:29 pm Benjamin Kaduk wrote: > On Thu, 16 Sep 2010, John Baldwin wrote: > > > On Thursday, September 16, 2010 1:33:07 pm Andrey Simonenko wrote: > > > >> The mtx_owned(9) macro uses this property, mtx_owned() does not use anything > >> special to compare the value of m->mtx_lock (volatile) with current thread > >> pointer, all other functions that update m->mtx_lock of unowned mutex use > >> compare-and-set instruction. Also I cannot find anything special in > >> generated Assembler code for volatile variables (except for ia64 where > >> acquire loads and release stores are used). > > > > No, mtx_owned() is just not harmed by the races it loses. You can certainly > > read a stale value of mtx_lock in mtx_owned() if some other thread owns the > > lock or has just released the lock. However, we don't care, because in both > > of those cases, mtx_owned() returns false. What does matter is that > > mtx_owned() can only return true if we currently hold the mutex. This works > > because 1) the same thread cannot call mtx_unlock() and mtx_owned() at the > > same time, and 2) even CPUs that hold writes in store buffers will snoop their > > store buffer for local reads on that CPU. That is, a given CPU will never > > read a stale value of a memory word that is "older" than a write it has > > performed to that word. > > Sorry for the naive question, but would you mind expounding a bit on what > keeps the thread from migrating to a different CPU and getting a stale > value there? (I can imagine a couple possible mechanisms, but don't know > enough to know which one(s) are the real ones.) The memory barriers in the thread_lock() / thread_unlock() pair of a context switch ensure that any writes posted by the thread before it performs a context switch will be visible on the "new" CPU before the thread resumes execution. -- John Baldwin