1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-23 14:01:44 +03:00

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
This commit is contained in:
Tom Lane
2018-09-01 15:27:13 -04:00
parent ee0ab27540
commit f5c93cf922
24 changed files with 139 additions and 167 deletions

View File

@ -61,14 +61,11 @@ typedef struct
/* State of generic xlog record construction */
struct GenericXLogState
{
/*
* page's images. Should be first in this struct to have MAXALIGN'ed
* images addresses, because some code working with pages directly aligns
* addresses, not offsets from beginning of page
*/
char images[MAX_GENERIC_XLOG_PAGES * BLCKSZ];
/* Info about each page, see above */
PageData pages[MAX_GENERIC_XLOG_PAGES];
bool isLogged;
/* Page images (properly aligned) */
PGAlignedBlock images[MAX_GENERIC_XLOG_PAGES];
};
static void writeFragment(PageData *pageData, OffsetNumber offset,
@ -251,12 +248,12 @@ computeDelta(PageData *pageData, Page curpage, Page targetpage)
#ifdef WAL_DEBUG
if (XLOG_DEBUG)
{
char tmp[BLCKSZ];
PGAlignedBlock tmp;
memcpy(tmp, curpage, BLCKSZ);
applyPageRedo(tmp, pageData->delta, pageData->deltaLen);
if (memcmp(tmp, targetpage, targetLower) != 0 ||
memcmp(tmp + targetUpper, targetpage + targetUpper,
memcpy(tmp.data, curpage, BLCKSZ);
applyPageRedo(tmp.data, pageData->delta, pageData->deltaLen);
if (memcmp(tmp.data, targetpage, targetLower) != 0 ||
memcmp(tmp.data + targetUpper, targetpage + targetUpper,
BLCKSZ - targetUpper) != 0)
elog(ERROR, "result of generic xlog apply does not match");
}
@ -277,7 +274,7 @@ GenericXLogStart(Relation relation)
for (i = 0; i < MAX_GENERIC_XLOG_PAGES; i++)
{
state->pages[i].image = state->images + BLCKSZ * i;
state->pages[i].image = state->images[i].data;
state->pages[i].buffer = InvalidBuffer;
}