From owner-svn-src-head@FreeBSD.ORG Sun Jun 23 23:38:52 2013 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id 35FBF3AD; Sun, 23 Jun 2013 23:38:52 +0000 (UTC) (envelope-from brde@optusnet.com.au) Received: from mail105.syd.optusnet.com.au (mail105.syd.optusnet.com.au [211.29.132.249]) by mx1.freebsd.org (Postfix) with ESMTP id EC7311242; Sun, 23 Jun 2013 23:38:51 +0000 (UTC) Received: from c122-106-156-23.carlnfd1.nsw.optusnet.com.au (c122-106-156-23.carlnfd1.nsw.optusnet.com.au [122.106.156.23]) by mail105.syd.optusnet.com.au (Postfix) with ESMTPS id D8FE01040BCF; Mon, 24 Jun 2013 09:38:43 +1000 (EST) Date: Mon, 24 Jun 2013 09:38:42 +1000 (EST) From: Bruce Evans X-X-Sender: bde@besplex.bde.org To: Bruce Evans Subject: Re: svn commit: r252032 - head/sys/amd64/include In-Reply-To: <20130623181458.J2256@besplex.bde.org> Message-ID: <20130624085002.G874@besplex.bde.org> References: <201306201430.r5KEU4G5049115@svn.freebsd.org> <20130621065839.J916@besplex.bde.org> <20130621081116.E1151@besplex.bde.org> <20130621090207.F1318@besplex.bde.org> <20130621064901.GS1214@FreeBSD.org> <20130621184140.G848@besplex.bde.org> <20130621135427.GA1214@FreeBSD.org> <20130622110352.J2033@besplex.bde.org> <20130622124832.S2347@besplex.bde.org> <20130622174921.I3112@besplex.bde.org> <20130623073343.GY91021@kib.kiev.ua> <20130623181458.J2256@besplex.bde.org> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.0 cv=K8x6hFqI c=1 sm=1 a=0l9hOOMEwYoA:10 a=kj9zAlcOel0A:10 a=PO7r1zJSAAAA:8 a=JzwRw_2MAAAA:8 a=gvJhbHXk4isA:10 a=X7nFtJYR00Ou1QpXdpoA:9 a=CjuIK1q_8ugA:10 a=ebeQFi2P/qHVC0Yw9JDJ4g==:117 Cc: Konstantin Belousov , svn-src-head@freebsd.org, svn-src-all@freebsd.org, Gleb Smirnoff , src-committers@freebsd.org X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 23 Jun 2013 23:38:52 -0000 On Sun, 23 Jun 2013, I wrote: > I thought of lots of variations, but couldn't find one that works perfectly. > One idea (that goes with the sign check on the low 32 bits) is to use a > misaligned add to memory to copy the 31st bit as a carry bit to the the > high word. The value of the counter is supposed to be > > [unsigned value of low 32 bits] + [unsigned value of next 24 bits] << 31 > (high 8 bits not used) > > at all times, with the 31st bit zero at most times so that the the carry > operation is rarely executed. This format is only slightly confusing for > debugging (it basically has 2 31st bits, with the one in the low 32 bits > rarely used). This format can be updated using something like: > .... > The above doesn't work if it is preempted -- then it might add do the > carry operation more than once. But it is easy to lock using cmpxchg or > disabling interrupts. Disabling interrupts requires only small code: > ... > Another idea is to use the high bits of the high word to encode state. > They can be set atomically enough using addl/andl/orl/xorl since they > are per-CPU. I couldn't quite get this to work. You could increment Here is a combined version. It uses the old shift trick instead of disabling interrupts for locking. The shift trick is not often used since it doesn't work for SMP. It works here since the counters are per-CPU: addl %1,%%fs:(%0) # only small 32-bit increments supported jns 8f sar $1,%%fs:7(%0) # hi 8 bits are for lock; init to 0xfe jc 8f # already locked addl $0x80,%%fs:3(%0) # misaligned memory access movb $0xfe,%%fs:7(%0) # unlock 8: ; Since the locked case is rarely executed and the code for it is only inline for convenience, the shortest code should be preferred and that is the simpler version that disables interrupts. Unless the lock byte can be exploited in the fetching and zeroing code. I don't see how it can be, since this type of locking only works for a single CPU. Bruce