Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 15 Dec 2000 14:26:10 -0800 (PST)
From:      John Baldwin <jhb@FreeBSD.org>
To:        =?ISO-8859-1?Q?G=E9rard_Roudier?= <groudier@club-internet.fr>
Cc:        freebsd-alpha@FreeBSD.org, Bernd Walter <ticso@cicely5.cicely.de>
Subject:   RE: mb and wmb in atomic_
Message-ID:  <XFMail.001215142610.jhb@FreeBSD.org>
In-Reply-To: <Pine.LNX.4.10.10012152205330.281-100000@linux.local>

next in thread | previous in thread | raw e-mail | index | archive | help

On 15-Dec-00 Gérard Roudier wrote:
> 
> 
> On Fri, 15 Dec 2000, John Baldwin wrote:
> 
>> 
>> On 15-Dec-00 Bernd Walter wrote:
>> > Why are the mb and wmb operations needed in the atomic_ functions?
>> > If I understood it correctly the locked operations are in synced
>> > with others CPUs and there is no memory operation beside the variable
>> > itself.
>> 
>> They should probably only be used with the 'acq' and 'rel' variants.  Hmm,
>> btw,
>> it looks like I have the order of the barriers in the 'acq' and 'rel'
>> variants
>> wrong.  The barriers should be on the inside, not the outside.  Anyone
>> disagree?
> 
> The mb() inside the atomic_ operations are obviously seriously
> miscommented:
> 
> For example:
> 
> static __inline void atomic_set_32(volatile u_int32_t *p, u_int32_t v)
> {
>       u_int32_t temp;
> 
>       __asm __volatile (
>               "1:\tldl_l %0, %2\n\t"          /* load old value */
>               "bis %0, %3, %0\n\t"            /* calculate new value */
>               "stl_c %0, %1\n\t"              /* attempt to store */
>               "beq %0, 2f\n\t"                /* spin if failed */
>               "mb\n\t"                        /* drain to memory */
>               ".section .text3,\"ax\"\n"      /* improve branch prediction */
>               "2:\tbr 1b\n"                   /* try again */
>               ".previous\n"
>               : "=&r" (temp), "=m" (*p)
>               : "m" (*p), "r" (v)
>               : "memory");
> }
> 
> There is obviously no relevance here in trying to drain something to
> memory. Note that thinking about a mb() as something that drains to memory
> is a serious mistake in my opinion. We want to order there, not to drain
> anything anywhere.
> 
> However, the mb() above does have the effect of throwing away any read 
> that may have been prefetched by the CPU. If we need that here, we must
> not remove the mb(), otherwise the mb() can be removed.

What does NetBSD use for their atomic operations?  Also, note that in FreeBSD,
we have specific variants of atomic operations that include memory barriers
(see the atomic(9) manpage in -current).  If the actual atomicity of the
operation doesn't need the mb, they should be removed.

>   Gérard.

-- 

John Baldwin <jhb@FreeBSD.org> -- http://www.FreeBSD.org/~jhb/
PGP Key: http://www.baldwin.cx/~john/pgpkey.asc
"Power Users Use the Power to Serve!"  -  http://www.FreeBSD.org/


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-alpha" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?XFMail.001215142610.jhb>