pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
[PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
17+ messages / 6 participants
[nested] [flat]

* [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
@ 2011-12-18 22:42  Martin Pitt <mpitt@debian.org>
  0 siblings, 1 reply; 17+ messages in thread

From: Martin Pitt @ 2011-12-18 22:42 UTC (permalink / raw)
  To: pgsql-bugs

Hello all,

Heikki Linnakangas wrote:
> An even better approach would be to have a configure test for
> __sync_lock_test_and_set. A quick google search suggests that Intel
> C Compiler version >= 11.0 also supports __sync_lock_test_and_set,
> for example.

Right,
http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html
explicitly refers to intel CC as well.

> It probably makes sense to use it on any platform where it's
> defined. Presumably an implementation provided by the compiler is
> always going to be at least as good as any magic assembler
> incantations we can come up with.

I agree. How about a patch like this? It uses builtin atomics if
available, and falls back to the custom implementations if not.

Note that a simple AC_CHECK_FUNC(__sync_lock_test_and_set) does not
work, so I used a slightly more complicated AC_TRY_LINK() which also
verifies that __sync_lock_release() is available.

The patch is against 9.1.2, but I suppose it also applies to trunk
(except perhaps the autogenerated configure part, this needs an
autoheader/autoconf run).

Thanks for considering,

Martin

-- 
Martin Pitt                        | http://www.piware.de
Ubuntu Developer (www.ubuntu.com)  | Debian Developer  (www.debian.org)

Attachments:

01-atomic-builtins.patch (text/x-diff, 5.0K) download Description: Use gcc/intel cc builtin atomic operations for test-and-set, if available (http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html) Author: Martin Pitt <mpitt@debian.org> Index: postgresql-9.1-9.1.2/configure.in =================================================================== --- postgresql-9.1-9.1.2.orig/configure.in 2011-12-01 22:47:20.000000000 +0100 +++ postgresql-9.1-9.1.2/configure.in 2011-12-18 23:30:47.940475128 +0100 @@ -1522,6 +1522,18 @@ AC_SUBST(LDAP_LIBS_FE) AC_SUBST(LDAP_LIBS_BE) +# gcc and intel compiler provide builtin functions for atomic test-and-set +AC_MSG_CHECKING([whether the C compiler provides atomic builtins]) +AC_TRY_LINK([], [int lock = 0; __sync_lock_test_and_set(&lock, 1); __sync_lock_release(&lock);], + [have_cc_atomics="yes"], + [have_cc_atomics="no"] +) +if test "$have_cc_atomics" = yes; then + AC_MSG_RESULT(yes) + AC_DEFINE(HAVE_CC_ATOMICS, 1, [Define to 1 if compiler provides atomic builtins]) +else + AC_MSG_RESULT(no) +fi # This test makes sure that run tests work at all. Sometimes a shared # library is found by the linker, but the runtime linker can't find it. Index: postgresql-9.1-9.1.2/configure =================================================================== --- postgresql-9.1-9.1.2.orig/configure 2011-12-01 22:47:20.000000000 +0100 +++ postgresql-9.1-9.1.2/configure 2011-12-18 23:31:00.304475596 +0100 @@ -23602,6 +23602,69 @@ +# gcc and intel compiler provide builtin functions for atomic test-and-set +{ $as_echo "$as_me:$LINENO: checking whether the C compiler provides atomic builtins" >&5 +$as_echo_n "checking whether the C compiler provides atomic builtins... " >&6; } +cat >conftest.$ac_ext <<_ACEOF +/* confdefs.h. */ +_ACEOF +cat confdefs.h >>conftest.$ac_ext +cat >>conftest.$ac_ext <<_ACEOF +/* end confdefs.h. */ + +int +main () +{ +int lock = 0; __sync_lock_test_and_set(&lock, 1); __sync_lock_release(&lock); + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext conftest$ac_exeext +if { (ac_try="$ac_link" +case "(($ac_try" in + *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; + *) ac_try_echo=$ac_try;; +esac +eval ac_try_echo="\"\$as_me:$LINENO: $ac_try_echo\"" +$as_echo "$ac_try_echo") >&5 + (eval "$ac_link") 2>conftest.er1 + ac_status=$? + grep -v '^ *+' conftest.er1 >conftest.err + rm -f conftest.er1 + cat conftest.err >&5 + $as_echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); } && { + test -z "$ac_c_werror_flag" || + test ! -s conftest.err + } && test -s conftest$ac_exeext && { + test "$cross_compiling" = yes || + $as_test_x conftest$ac_exeext + }; then + have_cc_atomics="yes" +else + $as_echo "$as_me: failed program was:" >&5 +sed 's/^/| /' conftest.$ac_ext >&5 + + have_cc_atomics="no" + +fi + +rm -rf conftest.dSYM +rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \ + conftest$ac_exeext conftest.$ac_ext +if test "$have_cc_atomics" = yes; then + { $as_echo "$as_me:$LINENO: result: yes" >&5 +$as_echo "yes" >&6; } + +cat >>confdefs.h <<\_ACEOF +#define HAVE_CC_ATOMICS 1 +_ACEOF + +else + { $as_echo "$as_me:$LINENO: result: no" >&5 +$as_echo "no" >&6; } +fi # This test makes sure that run tests work at all. Sometimes a shared # library is found by the linker, but the runtime linker can't find it. Index: postgresql-9.1-9.1.2/src/include/pg_config.h.in =================================================================== --- postgresql-9.1-9.1.2.orig/src/include/pg_config.h.in 2011-12-01 22:47:20.000000000 +0100 +++ postgresql-9.1-9.1.2/src/include/pg_config.h.in 2011-12-18 23:30:34.752474625 +0100 @@ -87,6 +87,9 @@ /* Define to 1 if you have the `cbrt' function. */ #undef HAVE_CBRT +/* Define to 1 if compiler provides atomic builtins */ +#undef HAVE_CC_ATOMICS + /* Define to 1 if you have the `class' function. */ #undef HAVE_CLASS Index: postgresql-9.1-9.1.2/src/include/storage/s_lock.h =================================================================== --- postgresql-9.1-9.1.2.orig/src/include/storage/s_lock.h 2011-12-01 22:47:20.000000000 +0100 +++ postgresql-9.1-9.1.2/src/include/storage/s_lock.h 2011-12-18 23:30:35.296474646 +0100 @@ -77,8 +77,26 @@ #ifdef HAVE_SPINLOCKS /* skip spinlocks if requested */ +/************************************************************************* + * Use compiler provided atomic builtins if available (in particular, gcc and + * Intel provide them for many platforms), as we are unlikely to come up with + * better ones. + */ +#ifdef HAVE_CC_ATOMICS +#define HAS_TEST_AND_SET +typedef int slock_t; + +#define TAS(lock) tas(lock) +#define S_UNLOCK(lock) __sync_lock_release(lock) + +static __inline__ int +tas(volatile slock_t *lock) +{ + return __sync_lock_test_and_set (lock, 1); +} +#endif -#if defined(__GNUC__) || defined(__INTEL_COMPILER) +#if !defined(HAS_TEST_AND_SET) && (defined(__GNUC__) || defined(__INTEL_COMPILER)) /************************************************************************* * All the gcc inlines * Gcc consistently defines the CPU as __cpu__.
signature.asc (application/pgp-signature, 835B) download ^ permalink raw reply [nested|flat] 17+ messages in thread

* Re: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
@ 2011-12-19 14:31  Robert Haas <robertmhaas@gmail.com>
  parent: Martin Pitt <mpitt@debian.org>
  0 siblings, 2 replies; 17+ messages in thread

From: Robert Haas @ 2011-12-19 14:31 UTC (permalink / raw)
  To: pgsql-bugs

On Sun, Dec 18, 2011 at 5:42 PM, Martin Pitt <mpitt@debian.org> wrote:
>> It probably makes sense to use it on any platform where it's
>> defined. Presumably an implementation provided by the compiler is
>> always going to be at least as good as any magic assembler
>> incantations we can come up with.
>
> I agree. How about a patch like this? It uses builtin atomics if
> available, and falls back to the custom implementations if not.

-1.  Absent some evidence that gcc's implementations are superior to
ours, I think we should not change stuff that works now.  That's
likely to lead to subtle bugs that are hard to find and perhaps
dependent on the exact compiler version used.

But I'm completely cool with doing this for platforms where we haven't
otherwise got an implementation.  Any port in a storm.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company



^ permalink  raw  reply  [nested|flat] 17+ messages in thread

* Re: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
@ 2011-12-19 15:09  Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  parent: Robert Haas <robertmhaas@gmail.com>
  1 sibling, 3 replies; 17+ messages in thread

From: Heikki Linnakangas @ 2011-12-19 15:09 UTC (permalink / raw)
  To: Robert Haas <robertmhaas@gmail.com>; +Cc: pgsql-bugs

On 19.12.2011 16:31, Robert Haas wrote:
> On Sun, Dec 18, 2011 at 5:42 PM, Martin Pitt<mpitt@debian.org>  wrote:
>>> It probably makes sense to use it on any platform where it's
>>> defined. Presumably an implementation provided by the compiler is
>>> always going to be at least as good as any magic assembler
>>> incantations we can come up with.
>>
>> I agree. How about a patch like this? It uses builtin atomics if
>> available, and falls back to the custom implementations if not.
>
> -1.  Absent some evidence that gcc's implementations are superior to
> ours, I think we should not change stuff that works now.  That's
> likely to lead to subtle bugs that are hard to find and perhaps
> dependent on the exact compiler version used.

Ok, we're in disagreement on that then. I don't feel very strongly about 
it, let's see what others think.

One thing that caught my eye: if you use __sync_lock_and_test() to 
implement S_LOCK(), you really should be using __sync_lock_release() for 
S_UNLOCK().

Actually, I believe our Itanium (and possibly ARM, too) implementation 
of S_UNLOCK() is wrong as it is. There is no platform-specific 
S_UNLOCK() defined for Itanium, so we're using the generic implementation:

#if !defined(S_UNLOCK)
#define S_UNLOCK(lock)		(*((volatile slock_t *) (lock)) = 0)
#endif	 /* S_UNLOCK */

That is not sufficient on platforms with a weak memory model, like Itanium.

-- 
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com



^ permalink  raw  reply  [nested|flat] 17+ messages in thread

* Re: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
@ 2011-12-19 15:25  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  2 siblings, 2 replies; 17+ messages in thread

From: Tom Lane @ 2011-12-19 15:25 UTC (permalink / raw)
  To: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>; +Cc: Robert Haas <robertmhaas@gmail.com>; pgsql-bugs

Heikki Linnakangas <heikki.linnakangas@enterprisedb.com> writes:
> On 19.12.2011 16:31, Robert Haas wrote:
>> On Sun, Dec 18, 2011 at 5:42 PM, Martin Pitt<mpitt@debian.org>  wrote:
>>> I agree. How about a patch like this? It uses builtin atomics if
>>> available, and falls back to the custom implementations if not.

>> -1.  Absent some evidence that gcc's implementations are superior to
>> ours, I think we should not change stuff that works now.  That's
>> likely to lead to subtle bugs that are hard to find and perhaps
>> dependent on the exact compiler version used.

> Ok, we're in disagreement on that then. I don't feel very strongly about 
> it, let's see what others think.

I agree with Robert.  There is no evidence whatsoever that this would
be an improvement, and unless somebody cares to provide such evidence,
we shouldn't risk changing code that's so full of portability hazards.

> Actually, I believe our Itanium (and possibly ARM, too) implementation 
> of S_UNLOCK() is wrong as it is.

Hmm.  Anybody got a large itanium box we could play with?  If it is
wrong, I'd expect it would show up pretty quickly under pgbench or
similar.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 17+ messages in thread

* Re: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
@ 2011-12-19 15:27  Martin Pitt <mpitt@debian.org>
  parent: Robert Haas <robertmhaas@gmail.com>
  1 sibling, 0 replies; 17+ messages in thread

From: Martin Pitt @ 2011-12-19 15:27 UTC (permalink / raw)
  To: pgsql-bugs

Robert Haas [2011-12-19  9:31 -0500]:
> On Sun, Dec 18, 2011 at 5:42 PM, Martin Pitt <mpitt@debian.org> wrote:
> >> It probably makes sense to use it on any platform where it's
> >> defined. Presumably an implementation provided by the compiler is
> >> always going to be at least as good as any magic assembler
> >> incantations we can come up with.
> >
> > I agree. How about a patch like this? It uses builtin atomics if
> > available, and falls back to the custom implementations if not.
> 
> -1.  Absent some evidence that gcc's implementations are superior to
> ours, I think we should not change stuff that works now.  That's
> likely to lead to subtle bugs that are hard to find and perhaps
> dependent on the exact compiler version used.
> 
> But I'm completely cool with doing this for platforms where we haven't
> otherwise got an implementation.  Any port in a storm.

Sure, then the other option is to stuff this at the end of s_lock.h if
we don't already have HAS_TEST_AND_SET. This would then mean that we
need to remove the armel implementation, as it doesn't really work on
anything non-ancient, and the gcc one got some fairly good test
coverage by now.

I'm happy to work out the patch for this. I'll just wait a bit if
there are more comments on this.

Thanks,

Martin
-- 
Martin Pitt                        | http://www.piware.de
Ubuntu Developer (www.ubuntu.com)  | Debian Developer  (www.debian.org)



^ permalink  raw  reply  [nested|flat] 17+ messages in thread

* Re: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
@ 2011-12-19 15:39  Martin Pitt <mpitt@debian.org>
  parent: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  2 siblings, 1 reply; 17+ messages in thread

From: Martin Pitt @ 2011-12-19 15:39 UTC (permalink / raw)
  To: pgsql-bugs

Heikki Linnakangas [2011-12-19 17:09 +0200]:
> One thing that caught my eye: if you use __sync_lock_and_test() to
> implement S_LOCK(), you really should be using __sync_lock_release()
> for S_UNLOCK().

Right, the patch I send does that:

 #define S_UNLOCK(lock) __sync_lock_release(lock)

Martin
-- 
Martin Pitt                        | http://www.piware.de
Ubuntu Developer (www.ubuntu.com)  | Debian Developer  (www.debian.org)



^ permalink  raw  reply  [nested|flat] 17+ messages in thread

* Re: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
@ 2011-12-19 15:41  Martin Pitt <mpitt@debian.org>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 17+ messages in thread

From: Martin Pitt @ 2011-12-19 15:41 UTC (permalink / raw)
  To: pgsql-bugs

Tom Lane [2011-12-19 10:25 -0500]:
> Heikki Linnakangas <heikki.linnakangas@enterprisedb.com> writes:
> > On 19.12.2011 16:31, Robert Haas wrote:
> >> On Sun, Dec 18, 2011 at 5:42 PM, Martin Pitt<mpitt@debian.org>  wrote:
> >>> I agree. How about a patch like this? It uses builtin atomics if
> >>> available, and falls back to the custom implementations if not.
> 
> >> -1.  Absent some evidence that gcc's implementations are superior to
> >> ours, I think we should not change stuff that works now.  That's
> >> likely to lead to subtle bugs that are hard to find and perhaps
> >> dependent on the exact compiler version used.
> 
> > Ok, we're in disagreement on that then. I don't feel very strongly about 
> > it, let's see what others think.
> 
> I agree with Robert.  There is no evidence whatsoever that this would
> be an improvement, and unless somebody cares to provide such evidence,
> we shouldn't risk changing code that's so full of portability hazards.

OK, with you and Robert preferring this as a fallback instead of a
preferred way, and with Heikki's "I don't care much", I'll rework the
patch.

Thanks,

Martin
-- 
Martin Pitt                        | http://www.piware.de
Ubuntu Developer (www.ubuntu.com)  | Debian Developer  (www.debian.org)



^ permalink  raw  reply  [nested|flat] 17+ messages in thread

* Re: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
@ 2011-12-19 16:09  Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  parent: Martin Pitt <mpitt@debian.org>
  0 siblings, 0 replies; 17+ messages in thread

From: Heikki Linnakangas @ 2011-12-19 16:09 UTC (permalink / raw)
  To: pgsql-bugs

On 19.12.2011 17:39, Martin Pitt wrote:
> Heikki Linnakangas [2011-12-19 17:09 +0200]:
>> One thing that caught my eye: if you use __sync_lock_and_test() to
>> implement S_LOCK(), you really should be using __sync_lock_release()
>> for S_UNLOCK().
>
> Right, the patch I send does that:
>
>   #define S_UNLOCK(lock) __sync_lock_release(lock)

Oh, I'm blind...

Can you comment on or test whether the existing TAS implementation is 
broken on ARM, because the existing S_UNLOCK() on ARM doesn't act as a 
memory barrier? If we're going to keep the existing snippet of inline 
assembly for those variants of ARM where it works, we need to either fix 
S_UNLOCK or convince ourselves that it's not broken.

I'll take a closer look at the same on Itanium.

-- 
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com



^ permalink  raw  reply  [nested|flat] 17+ messages in thread

* Re: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
@ 2011-12-19 16:18  Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 17+ messages in thread

From: Heikki Linnakangas @ 2011-12-19 16:18 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Robert Haas <robertmhaas@gmail.com>; pgsql-bugs

On 19.12.2011 17:25, Tom Lane wrote:
> Heikki Linnakangas<heikki.linnakangas@enterprisedb.com>  writes:
>> On 19.12.2011 16:31, Robert Haas wrote:
>> Actually, I believe our Itanium (and possibly ARM, too) implementation
>> of S_UNLOCK() is wrong as it is.
>
> Hmm.  Anybody got a large itanium box we could play with?  If it is
> wrong, I'd expect it would show up pretty quickly under pgbench or
> similar.

We've been running pgbench heavily recently in the company on large HP 
Itanium boxes with 32 and 64 cores, and haven't seen any issues. I'm not 
sure how it would manifest itself, though. It's also possible that while 
it's a genuine bug at the source level, it happens to be masked by some 
compiler decisions. It sure looks wrong.

I'll try to construct a self-contained test program to exercise that.

-- 
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com



^ permalink  raw  reply  [nested|flat] 17+ messages in thread

* Re: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
@ 2011-12-19 20:03  Noah Misch <noah@leadboat.com>
  parent: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  2 siblings, 2 replies; 17+ messages in thread

From: Noah Misch @ 2011-12-19 20:03 UTC (permalink / raw)
  To: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>; +Cc: Robert Haas <robertmhaas@gmail.com>; pgsql-bugs

On Mon, Dec 19, 2011 at 05:09:11PM +0200, Heikki Linnakangas wrote:
> Actually, I believe our Itanium (and possibly ARM, too) implementation  
> of S_UNLOCK() is wrong as it is. There is no platform-specific  
> S_UNLOCK() defined for Itanium, so we're using the generic 
> implementation:
>
> #if !defined(S_UNLOCK)
> #define S_UNLOCK(lock)		(*((volatile slock_t *) (lock)) = 0)
> #endif	 /* S_UNLOCK */
>
> That is not sufficient on platforms with a weak memory model, like Itanium.

Other processors may observe the lock as held after its release, but there's no
correctness problem.



^ permalink  raw  reply  [nested|flat] 17+ messages in thread

* Re: Re: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
@ 2011-12-19 20:12  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Noah Misch <noah@leadboat.com>
  1 sibling, 1 reply; 17+ messages in thread

From: Tom Lane @ 2011-12-19 20:12 UTC (permalink / raw)
  To: Noah Misch <noah@leadboat.com>; +Cc: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-bugs

Noah Misch <noah@leadboat.com> writes:
> On Mon, Dec 19, 2011 at 05:09:11PM +0200, Heikki Linnakangas wrote:
>> That is not sufficient on platforms with a weak memory model, like Itanium.

> Other processors may observe the lock as held after its release, but there's no
> correctness problem.

How weak is the memory model, exactly?

A correctness problem would ensue if out-of-order stores are possible,
ie other processors could observe the lock being freed (and then acquire
it) before seeing changes to shared variables that had been made while
holding the lock.

I'm a little dubious that this applies to Itanium, because I don't see
any memory fence instruction in the TAS macro.  If we needed one in
S_UNLOCK I'd rather expect there to be one in TAS as well.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 17+ messages in thread

* Re: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
@ 2011-12-19 20:21  Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  parent: Noah Misch <noah@leadboat.com>
  1 sibling, 0 replies; 17+ messages in thread

From: Heikki Linnakangas @ 2011-12-19 20:21 UTC (permalink / raw)
  To: Noah Misch <noah@leadboat.com>; +Cc: Robert Haas <robertmhaas@gmail.com>; pgsql-bugs

On 19.12.2011 22:03, Noah Misch wrote:
> On Mon, Dec 19, 2011 at 05:09:11PM +0200, Heikki Linnakangas wrote:
>> Actually, I believe our Itanium (and possibly ARM, too) implementation
>> of S_UNLOCK() is wrong as it is. There is no platform-specific
>> S_UNLOCK() defined for Itanium, so we're using the generic
>> implementation:
>>
>> #if !defined(S_UNLOCK)
>> #define S_UNLOCK(lock)		(*((volatile slock_t *) (lock)) = 0)
>> #endif	 /* S_UNLOCK */
>>
>> That is not sufficient on platforms with a weak memory model, like Itanium.
>
> Other processors may observe the lock as held after its release, but there's no
> correctness problem.

I thought it would also be legal for a store to become visible to other 
processors, *after* the releasing of the lock. Which would be bad. For 
example, if you have:

volatile bool *shared = ...
SpinLockAcquire(lock);
shared->variable = true;
SpinLockRelease(lock);
more code

The macro-expanded code would look like:

<test and set> lock
shared->variable = true;
(*((volatile slock_t *) (lock)) = 0;
more code

I believe on an architecture with a weak memory model, like Itanium, 
there's no guarantee that the assignments will happen in that order. The 
lock might appear as released *before* the variable is set.

-- 
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com



^ permalink  raw  reply  [nested|flat] 17+ messages in thread

* Re: Re: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
@ 2011-12-19 21:25  Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 17+ messages in thread

From: Heikki Linnakangas @ 2011-12-19 21:25 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Noah Misch <noah@leadboat.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-bugs

On 19.12.2011 22:12, Tom Lane wrote:
> Noah Misch<noah@leadboat.com>  writes:
>> On Mon, Dec 19, 2011 at 05:09:11PM +0200, Heikki Linnakangas wrote:
>>> That is not sufficient on platforms with a weak memory model, like Itanium.
>
>> Other processors may observe the lock as held after its release, but there's no
>> correctness problem.
>
> How weak is the memory model, exactly?
>
> A correctness problem would ensue if out-of-order stores are possible,
> ie other processors could observe the lock being freed (and then acquire
> it) before seeing changes to shared variables that had been made while
> holding the lock.
>
> I'm a little dubious that this applies to Itanium, because I don't see
> any memory fence instruction in the TAS macro.  If we needed one in
> S_UNLOCK I'd rather expect there to be one in TAS as well.

There's a pretty good manual called "Implementing Spinlocks on Itanium 
and PA-RISC" from HP at: 
http://h21007.www2.hp.com/portal/download/files/unprot/itanium/spinlocks.pdf. 
It says, in section "3.2.1 Try to get a spinlock":

 > Note also, that the ‘xchg’ instruction always
 > has the ‘acquire’ semantics, so it enforces the correct memory ordering.

But the current implementation seems to be safe, anyway.  In section 
"3.2.3 Freeing a spinlock", that manual says:

 > Note, that a simple assignment statement into a volatile variable 
will not work, as we are
 > assuming that the +Ovolatile=__unordered compile option is being used.

So +Ovolatile=__unordered would allow the kind of optimization that I 
thought was possible, and we would have a problem if we used it. But the 
default is more conservative, and a simple assignment does work.

I compiled the attached test program on an HP Itanium box, using the 
same flags you get from PostgreSQL's configure on that box. The relevant 
assembler output is:

         xchg4           r14 = [r15], r14           // M [slocktest.c: 66/3]
//file/line/col slocktest.c/67/3
         ld1.acq         r16 = [r11]                // M [slocktest.c: 67/3]
         nop.i           0                          // I
//file/line/col slocktest.c/68/3
         st1.rel         [r11] = r10             ;; // M [slocktest.c: 68/3]
//file/line/col slocktest.c/69/3
         st4.rel         [r15] = r0                 // M [slocktest.c: 69/3]
//file/line/col slocktest.c/70/1


The trick I missed is that the compiler attaches .rel to all the stores 
and .acq to all the loads through a volatile pointer. gcc seems to do 
the same. So we're safe.


It would be interesting to test whether using +Ovolatile=__unordered 
would make a difference to performance. Tacking those memory fence 
modifiers to all the volatile loads and stores might be expensive.

-- 
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com

Attachments:

slocktest.c (text/x-csrc, 1.4K) download #include <stdio.h> #if defined(__GNUC__) || defined(__INTEL_COMPILER) #if defined(__ia64__) || defined(__ia64) /* Intel Itanium */ #define HAS_TEST_AND_SET typedef unsigned int slock_t; #define TAS(lock) tas(lock) /* On IA64, it's a win to use a non-locking test before the xchg proper */ #define TAS_SPIN(lock) (*(lock) ? 1 : TAS(lock)) #ifndef __INTEL_COMPILER static __inline__ int tas(volatile slock_t *lock) { long int ret; __asm__ __volatile__( " xchg4 %0=%1,%2 \n" : "=r"(ret), "+m"(*lock) : "r"(1) : "memory"); return (int) ret; } #else /* __INTEL_COMPILER */ static __inline__ int tas(volatile slock_t *lock) { int ret; ret = _InterlockedExchange(lock,1); /* this is a xchg asm macro */ return ret; } #endif /* __INTEL_COMPILER */ #endif /* __ia64__ || __ia64 */ #endif /* defined(__GNUC__) || defined(__INTEL_COMPILER) */ #if defined(__hpux) && defined(__ia64) && !defined(__GNUC__) #define HAS_TEST_AND_SET typedef unsigned int slock_t; #include <ia64/sys/inline.h> #define TAS(lock) _Asm_xchg(_SZ_W, lock, 1, _LDHINT_NONE) /* On IA64, it's a win to use a non-locking test before the xchg proper */ #define TAS_SPIN(lock) (*(lock) ? 1 : TAS(lock)) #endif /* HPUX on IA64, non gcc */ slock_t lock; char shared2; int main(int argc, char **argv) { volatile char *p = &shared2; char local; TAS(&lock); local = *p; *p = 123; *((volatile slock_t *) &lock) = 0; }
^ permalink raw reply [nested|flat] 17+ messages in thread

* Re: Re: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
@ 2011-12-19 21:53  Noah Misch <noah@leadboat.com>
  parent: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  1 sibling, 0 replies; 17+ messages in thread

From: Noah Misch @ 2011-12-19 21:53 UTC (permalink / raw)
  To: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Robert Haas <robertmhaas@gmail.com>; pgsql-bugs

On Mon, Dec 19, 2011 at 11:25:06PM +0200, Heikki Linnakangas wrote:
> I compiled the attached test program on an HP Itanium box, using the  
> same flags you get from PostgreSQL's configure on that box. The relevant  
> assembler output is:
>
>         xchg4           r14 = [r15], r14           // M [slocktest.c: 66/3]
> //file/line/col slocktest.c/67/3
>         ld1.acq         r16 = [r11]                // M [slocktest.c: 67/3]
>         nop.i           0                          // I
> //file/line/col slocktest.c/68/3
>         st1.rel         [r11] = r10             ;; // M [slocktest.c: 68/3]
> //file/line/col slocktest.c/69/3
>         st4.rel         [r15] = r0                 // M [slocktest.c: 69/3]
> //file/line/col slocktest.c/70/1
>
>
> The trick I missed is that the compiler attaches .rel to all the stores  
> and .acq to all the loads through a volatile pointer. gcc seems to do  
> the same. So we're safe.

The Intel compiler appears not to follow this convention:
http://software.intel.com/sites/products/documentation/hpc/compilerpro/en-us/cpp/lin/compiler_c/copt...

If you have that compiler installed, could you see which opcode it generates?

Thanks,
nm



^ permalink  raw  reply  [nested|flat] 17+ messages in thread

* Re: Re: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
@ 2012-02-03 01:48  Bruce Momjian <bruce@momjian.us>
  parent: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  1 sibling, 1 reply; 17+ messages in thread

From: Bruce Momjian @ 2012-02-03 01:48 UTC (permalink / raw)
  To: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Noah Misch <noah@leadboat.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-bugs


Sorry for the late reply, but Heikki, can you get this Itanium
information into s_lock.h as a comment, particularly the information
about the +Ovolatile=__unordered flag?

---------------------------------------------------------------------------

On Mon, Dec 19, 2011 at 11:25:06PM +0200, Heikki Linnakangas wrote:
> On 19.12.2011 22:12, Tom Lane wrote:
> >Noah Misch<noah@leadboat.com>  writes:
> >>On Mon, Dec 19, 2011 at 05:09:11PM +0200, Heikki Linnakangas wrote:
> >>>That is not sufficient on platforms with a weak memory model, like Itanium.
> >
> >>Other processors may observe the lock as held after its release, but there's no
> >>correctness problem.
> >
> >How weak is the memory model, exactly?
> >
> >A correctness problem would ensue if out-of-order stores are possible,
> >ie other processors could observe the lock being freed (and then acquire
> >it) before seeing changes to shared variables that had been made while
> >holding the lock.
> >
> >I'm a little dubious that this applies to Itanium, because I don't see
> >any memory fence instruction in the TAS macro.  If we needed one in
> >S_UNLOCK I'd rather expect there to be one in TAS as well.
> 
> There's a pretty good manual called "Implementing Spinlocks on
> Itanium and PA-RISC" from HP at: http://h21007.www2.hp.com/portal/download/files/unprot/itanium/spinlocks.pdf.
> It says, in section "3.2.1 Try to get a spinlock":
> 
> > Note also, that the ‘xchg’ instruction always
> > has the ‘acquire’ semantics, so it enforces the correct memory ordering.
> 
> But the current implementation seems to be safe, anyway.  In section
> "3.2.3 Freeing a spinlock", that manual says:
> 
> > Note, that a simple assignment statement into a volatile variable
> will not work, as we are
> > assuming that the +Ovolatile=__unordered compile option is being used.
> 
> So +Ovolatile=__unordered would allow the kind of optimization that
> I thought was possible, and we would have a problem if we used it.
> But the default is more conservative, and a simple assignment does
> work.
> 
> I compiled the attached test program on an HP Itanium box, using the
> same flags you get from PostgreSQL's configure on that box. The
> relevant assembler output is:
> 
>         xchg4           r14 = [r15], r14           // M [slocktest.c: 66/3]
> //file/line/col slocktest.c/67/3
>         ld1.acq         r16 = [r11]                // M [slocktest.c: 67/3]
>         nop.i           0                          // I
> //file/line/col slocktest.c/68/3
>         st1.rel         [r11] = r10             ;; // M [slocktest.c: 68/3]
> //file/line/col slocktest.c/69/3
>         st4.rel         [r15] = r0                 // M [slocktest.c: 69/3]
> //file/line/col slocktest.c/70/1
> 
> 
> The trick I missed is that the compiler attaches .rel to all the
> stores and .acq to all the loads through a volatile pointer. gcc
> seems to do the same. So we're safe.
> 
> 
> It would be interesting to test whether using +Ovolatile=__unordered
> would make a difference to performance. Tacking those memory fence
> modifiers to all the volatile loads and stores might be expensive.
> 
> -- 
>   Heikki Linnakangas
>   EnterpriseDB   http://www.enterprisedb.com

> #include <stdio.h>
> 
> #if defined(__GNUC__) || defined(__INTEL_COMPILER)
> #if defined(__ia64__) || defined(__ia64)	/* Intel Itanium */
> #define HAS_TEST_AND_SET
> 
> typedef unsigned int slock_t;
> 
> #define TAS(lock) tas(lock)
> 
> /* On IA64, it's a win to use a non-locking test before the xchg proper */
> #define TAS_SPIN(lock)	(*(lock) ? 1 : TAS(lock))
> 
> #ifndef __INTEL_COMPILER
> 
> static __inline__ int
> tas(volatile slock_t *lock)
> {
> 	long int	ret;
> 
> 	__asm__ __volatile__(
> 		"	xchg4 	%0=%1,%2	\n"
> :		"=r"(ret), "+m"(*lock)
> :		"r"(1)
> :		"memory");
> 	return (int) ret;
> }
> 
> #else /* __INTEL_COMPILER */
> 
> static __inline__ int
> tas(volatile slock_t *lock)
> {
> 	int		ret;
> 
> 	ret = _InterlockedExchange(lock,1);	/* this is a xchg asm macro */
> 
> 	return ret;
> }
> 
> #endif /* __INTEL_COMPILER */
> #endif	 /* __ia64__ || __ia64 */
> #endif	/* defined(__GNUC__) || defined(__INTEL_COMPILER) */
> 
> #if defined(__hpux) && defined(__ia64) && !defined(__GNUC__)
> 
> #define HAS_TEST_AND_SET
> 
> typedef unsigned int slock_t;
> 
> #include <ia64/sys/inline.h>
> #define TAS(lock) _Asm_xchg(_SZ_W, lock, 1, _LDHINT_NONE)
> /* On IA64, it's a win to use a non-locking test before the xchg proper */
> #define TAS_SPIN(lock)	(*(lock) ? 1 : TAS(lock))
> 
> #endif	/* HPUX on IA64, non gcc */
> 
> slock_t lock;
> char shared2;
> 
> int main(int argc, char **argv)
> {
>   volatile char *p = &shared2;
>   char local;
> 
>   TAS(&lock);
>   local = *p;
>   *p = 123;
>   *((volatile slock_t *) &lock) = 0;
> }

> 
> -- 
> Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-bugs


-- 
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + It's impossible for everything to be true. +



^ permalink  raw  reply  [nested|flat] 17+ messages in thread

* Re: Re: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
@ 2012-02-06 11:24  Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  parent: Bruce Momjian <bruce@momjian.us>
  0 siblings, 1 reply; 17+ messages in thread

From: Heikki Linnakangas @ 2012-02-06 11:24 UTC (permalink / raw)
  To: Bruce Momjian <bruce@momjian.us>; Sergey E. Koposov <math@sai.msu.ru>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Noah Misch <noah@leadboat.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-bugs

On 03.02.2012 02:48, Bruce Momjian wrote:
> Sorry for the late reply, but Heikki, can you get this Itanium
> information into s_lock.h as a comment, particularly the information
> about the +Ovolatile=__unordered flag?

Good idea. I came up with the attached, hope that explains it.

Looking back at the discussions, we concluded that the current code is 
safe on gcc, because it implicitly adds the .rel/.acq opcodes to 
volatile accesses, and HP's compiler does the same as long as you don't 
explicitly disable it with +Ovolatile=__unordered. But what about 
Intel's icc compiler? Presumably it's also safe, but looking at Intel's 
manuals that I found, I'm not completely sure about it. There's an 
option, -m[no-]serialize-volatile, that controls it, but I couldn't 
figure out which is the default. Looking at the docs on that from Intel 
that I found [1], it seems to me that on Linux, the default is *not* 
safe, but on Windows it is.

Sergey, you have dugong in the buildfarm that uses Intel's compiler on 
Itanium. Could you verify whether the -mno-serialize-volatile is the 
default? If you could for example extract the assembler code generated 
by icc for xlog.c, and send it over. Whether it's generating the 
.rel/.acq opcodes should be easy to see in the generated code of the 
XLogGetLastRemoved() function, for example, which doesn't do much else 
than grab a spinlock. On gcc, the -s flag generates the assembly files, 
I presume it's the same on icc.

Perhaps we should set those compiler flags explicitly in configure, 
regardless of the defaults.

[1] 
http://software.intel.com/sites/products/documentation/hpc/compilerpro/en-us/cpp/lin/compiler_c/copt...

-- 
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com

Attachments:

document-itanium-volatile-behavior-1.patch (text/x-diff, 1.9K) download diff --git a/src/include/storage/s_lock.h b/src/include/storage/s_lock.h index 074838e..d55b662 100644 --- a/src/include/storage/s_lock.h +++ b/src/include/storage/s_lock.h @@ -234,7 +234,22 @@ spin_delay(void) #endif /* __x86_64__ */ -#if defined(__ia64__) || defined(__ia64) /* Intel Itanium */ +#if defined(__ia64__) || defined(__ia64) +/* + * Intel Itanium, gcc or Intel's compiler. + * + * Itanium has weak memory ordering, but we rely on the compiler to enforce + * strict ordering of accesses to volatile data. In particular, while the + * xchg instruction implicitly acts as a memory barrier with 'acquire' + * semantics, we do not have an explicit memory fence instruction in the + * S_UNLOCK macro. We use a regular assignment to clear the spinlock, and + * trust that the compiler marks the generated store instruction with the + * ".rel" opcode. + * + * Testing shows that assumption holds on gcc, although I could not find any + * official statement on that in the gcc manual. In Intel's compiler, the + * -m[no-]serialize-volatile option controls that. Keep it enabled! + */ #define HAS_TEST_AND_SET typedef unsigned int slock_t; @@ -785,7 +800,19 @@ tas(volatile slock_t *lock) #if defined(__hpux) && defined(__ia64) && !defined(__GNUC__) - +/* + * HP-UX on Itanium, non-gcc compiler + * + * We assume that the compiler enforces strict ordering of loads/stores on + * volatile data (see comments on the gcc-version earlier in this file). + * Note that this assumption does *not* hold if you use the + * +Ovolatile=__unordered option on the HP-UX compiler, so don't do that. + * + * See also Implementing Spinlocks on the Intel Itanium Architecture and + * PA-RISC, by Tor Ekqvist and David Graves, for more information. As of + * this writing, version 1.0 of the manual is available at: + * http://h21007.www2.hp.com/portal/download/files/unprot/itanium/spinlocks.pdf + */ #define HAS_TEST_AND_SET typedef unsigned int slock_t;
^ permalink raw reply [nested|flat] 17+ messages in thread

* Re: Re: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb]
@ 2012-03-16 09:45  Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  parent: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  0 siblings, 0 replies; 17+ messages in thread

From: Heikki Linnakangas @ 2012-03-16 09:45 UTC (permalink / raw)
  To: Bruce Momjian <bruce@momjian.us>; Sergey E. Koposov <math@sai.msu.ru>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Noah Misch <noah@leadboat.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-bugs

On 06.02.2012 13:24, Heikki Linnakangas wrote:
> On 03.02.2012 02:48, Bruce Momjian wrote:
>> Sorry for the late reply, but Heikki, can you get this Itanium
>> information into s_lock.h as a comment, particularly the information
>> about the +Ovolatile=__unordered flag?
>
> Good idea. I came up with the attached, hope that explains it.

Committed these additional comments.

> Looking back at the discussions, we concluded that the current code is
> safe on gcc, because it implicitly adds the .rel/.acq opcodes to
> volatile accesses, and HP's compiler does the same as long as you don't
> explicitly disable it with +Ovolatile=__unordered. But what about
> Intel's icc compiler? Presumably it's also safe, but looking at Intel's
> manuals that I found, I'm not completely sure about it. There's an
> option, -m[no-]serialize-volatile, that controls it, but I couldn't
> figure out which is the default. Looking at the docs on that from Intel
> that I found [1], it seems to me that on Linux, the default is *not*
> safe, but on Windows it is.
>
> Sergey, you have dugong in the buildfarm that uses Intel's compiler on
> Itanium. Could you verify whether the -mno-serialize-volatile is the
> default? If you could for example extract the assembler code generated
> by icc for xlog.c, and send it over. Whether it's generating the
> .rel/.acq opcodes should be easy to see in the generated code of the
> XLogGetLastRemoved() function, for example, which doesn't do much else
> than grab a spinlock. On gcc, the -s flag generates the assembly files,
> I presume it's the same on icc.

Sergey confirmed off-list that icc defaults to -mserialize-volatile, so 
we're safe.

-- 
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com



^ permalink  raw  reply  [nested|flat] 17+ messages in thread


end of thread, other threads:[~2012-03-16 09:45 UTC | newest]

Thread overview: 17+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-12-18 22:42 [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb] Martin Pitt <mpitt@debian.org>
2011-12-19 14:31 ` Robert Haas <robertmhaas@gmail.com>
2011-12-19 15:09   ` Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
2011-12-19 15:25     ` Tom Lane <tgl@sss.pgh.pa.us>
2011-12-19 15:41       ` Martin Pitt <mpitt@debian.org>
2011-12-19 16:18       ` Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
2011-12-19 15:39     ` Martin Pitt <mpitt@debian.org>
2011-12-19 16:09       ` Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
2011-12-19 20:03     ` Noah Misch <noah@leadboat.com>
2011-12-19 20:12       ` Tom Lane <tgl@sss.pgh.pa.us>
2011-12-19 21:25         ` Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
2011-12-19 21:53           ` Noah Misch <noah@leadboat.com>
2012-02-03 01:48           ` Bruce Momjian <bruce@momjian.us>
2012-02-06 11:24             ` Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
2012-03-16 09:45               ` Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
2011-12-19 20:21       ` Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
2011-12-19 15:27   ` Martin Pitt <mpitt@debian.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox