Obsolete use of volatile in walsender.c, walreceiver.c, walreceiverfuncs.c?

Started by Thomas Munroover 10 years ago10 messageshackers
Jump to latest
#1Thomas Munro
thomas.munro@gmail.com

Hi

In walsender.c, walreceiver.c, walreceiverfuncs.c there are several
places where volatile qualifiers are used apparently only to prevent
reordering around spinlock operations. My understanding is that if
potential load/store reordering around spinlock operations is the only
reason for using volatile, 0709b7ee72e4bc71ad07b7120acd117265ab51d0
made it unnecessary. For example see
6ba4ecbf477e0b25dd7bde1b0c4e07fc2da19348 which stripped some volatile
qualifiers out of xlog.c.

I did notice that sometimes walsnd->pid is read without acquiring the
spinlock. Is that actually OK anyway (taking a stale and inconsistent
view of the contents of walsnd->pid WRT to the other members that are
later accessed while holding the spinlock)?

Would it be safe to remove all those volatile qualifiers, something
like in the attached, or am I missing something?

(There is also code in syncrep.c that is reading shmem without
acquiring spinlocks using volatile qualifiers, that is a different
situation, though I don't yet see how it is ordering sensitive or
reading the same object repeatedly, but I'm not talking about that
here).

--
Thomas Munro
http://www.enterprisedb.com

Attachments:

replication-strip-volatile.patchapplication/octet-stream; name=replication-strip-volatile.patchDownload+25-52
#2Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Thomas Munro (#1)
Re: Obsolete use of volatile in walsender.c, walreceiver.c, walreceiverfuncs.c?

Thomas Munro wrote:

In walsender.c, walreceiver.c, walreceiverfuncs.c there are several
places where volatile qualifiers are used apparently only to prevent
reordering around spinlock operations.

In replication/slot.c there are a number of places (12, I think) that
introduce a block specifically to contain a volatile cast on a variable
for spinlock-protected access. We could remove the whole thing and save
at least 3 lines and one indentation level for each of them.

--
�lvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

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

#3Thomas Munro
thomas.munro@gmail.com
In reply to: Alvaro Herrera (#2)
Re: Obsolete use of volatile in walsender.c, walreceiver.c, walreceiverfuncs.c?

On Tue, Sep 22, 2015 at 8:19 AM, Alvaro Herrera
<alvherre@2ndquadrant.com> wrote:

Thomas Munro wrote:

In walsender.c, walreceiver.c, walreceiverfuncs.c there are several
places where volatile qualifiers are used apparently only to prevent
reordering around spinlock operations.

In replication/slot.c there are a number of places (12, I think) that
introduce a block specifically to contain a volatile cast on a variable
for spinlock-protected access. We could remove the whole thing and save
at least 3 lines and one indentation level for each of them.

Right, see attached.

--
Thomas Munro
http://www.enterprisedb.com

Attachments:

replication-strip-volatile-v2.patchapplication/octet-stream; name=replication-strip-volatile-v2.patchDownload+59-118
#4Andres Freund
andres@anarazel.de
In reply to: Thomas Munro (#1)
Re: Obsolete use of volatile in walsender.c, walreceiver.c, walreceiverfuncs.c?

Hi,

On 2015-09-17 16:32:17 +1200, Thomas Munro wrote:

In walsender.c, walreceiver.c, walreceiverfuncs.c there are several
places where volatile qualifiers are used apparently only to prevent
reordering around spinlock operations. My understanding is that if
potential load/store reordering around spinlock operations is the only
reason for using volatile, 0709b7ee72e4bc71ad07b7120acd117265ab51d0
made it unnecessary. For example see
6ba4ecbf477e0b25dd7bde1b0c4e07fc2da19348 which stripped some volatile
qualifiers out of xlog.c.

Same in bufmgr.c et al. There it's actually rather annoying for new code
because volatile needs to be casted away in a bunch of places...

Greetings,

Andres Freund

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

#5Michael Paquier
michael@paquier.xyz
In reply to: Thomas Munro (#3)
Re: Obsolete use of volatile in walsender.c, walreceiver.c, walreceiverfuncs.c?

On Tue, Sep 22, 2015 at 7:25 AM, Thomas Munro
<thomas.munro@enterprisedb.com> wrote:

On Tue, Sep 22, 2015 at 8:19 AM, Alvaro Herrera
<alvherre@2ndquadrant.com> wrote:

Thomas Munro wrote:

In walsender.c, walreceiver.c, walreceiverfuncs.c there are several
places where volatile qualifiers are used apparently only to prevent
reordering around spinlock operations.

In replication/slot.c there are a number of places (12, I think) that
introduce a block specifically to contain a volatile cast on a variable
for spinlock-protected access. We could remove the whole thing and save
at least 3 lines and one indentation level for each of them.

Right, see attached.

It seems to me that we could as well simplify checkpoint.c and
logical.c. In those files volatile casts are used as well to protect
from reordering for spinlock operations. See for example 0002 on top
of 0001 that is Thomas' patch.
--
Michael

Attachments:

0001-Remove-obsolete-use-of-volatile-in-WAL-related-files.patchtext/x-patch; charset=US-ASCII; name=0001-Remove-obsolete-use-of-volatile-in-WAL-related-files.patchDownload+59-119
0002-Remove-use-of-volatile-for-spinlock-operations-in-mo.patchtext/x-patch; charset=US-ASCII; name=0002-Remove-use-of-volatile-for-spinlock-operations-in-mo.patchDownload+49-65
#6Robert Haas
robertmhaas@gmail.com
In reply to: Michael Paquier (#5)
Re: Obsolete use of volatile in walsender.c, walreceiver.c, walreceiverfuncs.c?

On Thu, Oct 1, 2015 at 11:01 PM, Michael Paquier
<michael.paquier@gmail.com> wrote:

Right, see attached.

It seems to me that we could as well simplify checkpoint.c and
logical.c. In those files volatile casts are used as well to protect
from reordering for spinlock operations. See for example 0002 on top
of 0001 that is Thomas' patch.

These patches look good to me, so I have committed them.

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

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

#7Thomas Munro
thomas.munro@gmail.com
In reply to: Robert Haas (#6)
Re: Obsolete use of volatile in walsender.c, walreceiver.c, walreceiverfuncs.c?

On Wed, Oct 7, 2015 at 8:52 AM, Robert Haas <robertmhaas@gmail.com> wrote:

On Thu, Oct 1, 2015 at 11:01 PM, Michael Paquier
<michael.paquier@gmail.com> wrote:

Right, see attached.

It seems to me that we could as well simplify checkpoint.c and
logical.c. In those files volatile casts are used as well to protect
from reordering for spinlock operations. See for example 0002 on top
of 0001 that is Thomas' patch.

These patches look good to me, so I have committed them.

Thanks. Also, spin.h's comment contains an out of date warning about
this. Here's a suggested fix for that, and a couple more volatrivia
patches.

--
Thomas Munro
http://www.enterprisedb.com

Attachments:

spin-header-comment.patchapplication/octet-stream; name=spin-header-comment.patchDownload+4-9
strip-volatile-hash.patchapplication/octet-stream; name=strip-volatile-hash.patchDownload+27-32
strip-volatile-ipc.patchapplication/octet-stream; name=strip-volatile-ipc.patchDownload+10-23
#8Michael Paquier
michael@paquier.xyz
In reply to: Thomas Munro (#7)
Re: Obsolete use of volatile in walsender.c, walreceiver.c, walreceiverfuncs.c?

On Fri, Oct 16, 2015 at 9:07 AM, Thomas Munro
<thomas.munro@enterprisedb.com> wrote:

On Wed, Oct 7, 2015 at 8:52 AM, Robert Haas <robertmhaas@gmail.com> wrote:

On Thu, Oct 1, 2015 at 11:01 PM, Michael Paquier
<michael.paquier@gmail.com> wrote:

Right, see attached.

It seems to me that we could as well simplify checkpoint.c and
logical.c. In those files volatile casts are used as well to protect
from reordering for spinlock operations. See for example 0002 on top
of 0001 that is Thomas' patch.

These patches look good to me, so I have committed them.

Thanks. Also, spin.h's comment contains an out of date warning about
this. Here's a suggested fix for that, and a couple more volatrivia
patches.

I have looked at the rest of the code, and it seems that we can get
rid of volatile in a couple of extra places like in the attached as
those are used with spin locks. This applies on top of Thomas' set.
--
Michael

Attachments:

20151016_volatile_remove.patchtext/x-diff; charset=US-ASCII; name=20151016_volatile_remove.patchDownload+19-36
#9Robert Haas
robertmhaas@gmail.com
In reply to: Michael Paquier (#8)
Re: Obsolete use of volatile in walsender.c, walreceiver.c, walreceiverfuncs.c?

On Thu, Oct 15, 2015 at 10:36 PM, Michael Paquier
<michael.paquier@gmail.com> wrote:

On Fri, Oct 16, 2015 at 9:07 AM, Thomas Munro
<thomas.munro@enterprisedb.com> wrote:

On Wed, Oct 7, 2015 at 8:52 AM, Robert Haas <robertmhaas@gmail.com> wrote:

On Thu, Oct 1, 2015 at 11:01 PM, Michael Paquier
<michael.paquier@gmail.com> wrote:

Right, see attached.

It seems to me that we could as well simplify checkpoint.c and
logical.c. In those files volatile casts are used as well to protect
from reordering for spinlock operations. See for example 0002 on top
of 0001 that is Thomas' patch.

These patches look good to me, so I have committed them.

Thanks. Also, spin.h's comment contains an out of date warning about
this. Here's a suggested fix for that, and a couple more volatrivia
patches.

I have looked at the rest of the code, and it seems that we can get
rid of volatile in a couple of extra places like in the attached as
those are used with spin locks. This applies on top of Thomas' set.

OK, committed his, and yours.

I back-patched his spin.h comment fix to 9.5 since that's a factual
error, but the rest of this seems like optimization so I committed it
only to master.

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

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

#10Michael Paquier
michael@paquier.xyz
In reply to: Robert Haas (#9)
Re: Obsolete use of volatile in walsender.c, walreceiver.c, walreceiverfuncs.c?

On Sat, Oct 17, 2015 at 3:21 AM, Robert Haas wrote:

OK, committed his, and yours.

I back-patched his spin.h comment fix to 9.5 since that's a factual
error, but the rest of this seems like optimization so I committed it
only to master.

That sounds right. Thanks!
--
Michael

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