From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Avoid using potentially-under-aligned page buffers.
Date: Sat, 01 Sep 2018 19:27:44 +0000
Message-ID: <E1fwBYe-0006Sf-72@gemulon.postgresql.org> (raw)
Avoid using potentially-under-aligned page buffers.
There's a project policy against using plain "char buf[BLCKSZ]" local
or static variables as page buffers; preferred style is to palloc or
malloc each buffer to ensure it is MAXALIGN'd. However, that policy's
been ignored in an increasing number of places. We've apparently got
away with it so far, probably because (a) relatively few people use
platforms on which misalignment causes core dumps and/or (b) the
variables chance to be sufficiently aligned anyway. But this is not
something to rely on. Moreover, even if we don't get a core dump,
we might be paying a lot of cycles for misaligned accesses.
To fix, invent new union types PGAlignedBlock and PGAlignedXLogBlock
that the compiler must allocate with sufficient alignment, and use
those in place of plain char arrays.
I used these types even for variables where there's no risk of a
misaligned access, since ensuring proper alignment should make
kernel data transfers faster. I also changed some places where
we had been palloc'ing short-lived buffers, for coding style
uniformity and to save palloc/pfree overhead.
Since this seems to be a live portability hazard (despite the lack
of field reports), back-patch to all supported versions.
Patch by me; thanks to Michael Paquier for review.
Discussion: https://postgr.es/m/1535618100.1286.3.camel@credativ.de
Branch
------
REL9_5_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/03ffe5553c1d6238dc235e8d5e8f1295734e5c79
Modified Files
--------------
contrib/pg_prewarm/pg_prewarm.c | 4 ++--
src/backend/access/gin/ginentrypage.c | 6 +++---
src/backend/access/gin/ginfast.c | 11 +++--------
src/backend/access/hash/hashpage.c | 6 +++---
src/backend/access/heap/heapam.c | 16 ++++------------
src/backend/access/heap/visibilitymap.c | 11 ++++-------
src/backend/access/transam/xlog.c | 19 +++++++------------
src/backend/access/transam/xloginsert.c | 14 +++++++-------
src/backend/access/transam/xlogreader.c | 6 +++---
src/backend/commands/tablecmds.c | 17 ++++-------------
src/backend/replication/walsender.c | 6 +++---
src/backend/storage/file/buffile.c | 10 +++++-----
src/backend/storage/freespace/freespace.c | 11 ++++-------
src/bin/pg_basebackup/receivelog.c | 8 +++-----
src/bin/pg_resetxlog/pg_resetxlog.c | 14 ++++++--------
src/bin/pg_rewind/copy_fetch.c | 6 +++---
src/include/c.h | 26 ++++++++++++++++++++++++++
17 files changed, 90 insertions(+), 101 deletions(-)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: tgl@sss.pgh.pa.us
Subject: Re: pgsql: Avoid using potentially-under-aligned page buffers.
In-Reply-To: <E1fwBYe-0006Sf-72@gemulon.postgresql.org>
References: <E1fwBYe-0006Sf-72@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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