From 44cac9346479d4b0cc9195b0267fd13eb4e7442c Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sat, 1 Sep 2018 15:27:12 -0400 Subject: 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 --- src/bin/pg_basebackup/walmethods.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) (limited to 'src/bin/pg_basebackup/walmethods.c') diff --git a/src/bin/pg_basebackup/walmethods.c b/src/bin/pg_basebackup/walmethods.c index 42e3f3023a0..68ef7fa6d77 100644 --- a/src/bin/pg_basebackup/walmethods.c +++ b/src/bin/pg_basebackup/walmethods.c @@ -116,18 +116,17 @@ dir_open_for_write(const char *pathname, const char *temp_suffix, size_t pad_to_ /* Do pre-padding on non-compressed files */ if (pad_to_size && dir_data->compression == 0) { - char *zerobuf; + PGAlignedXLogBlock zerobuf; int bytes; - zerobuf = pg_malloc0(XLOG_BLCKSZ); + memset(zerobuf.data, 0, XLOG_BLCKSZ); for (bytes = 0; bytes < pad_to_size; bytes += XLOG_BLCKSZ) { errno = 0; - if (write(fd, zerobuf, XLOG_BLCKSZ) != XLOG_BLCKSZ) + if (write(fd, zerobuf.data, XLOG_BLCKSZ) != XLOG_BLCKSZ) { int save_errno = errno; - pg_free(zerobuf); close(fd); /* @@ -137,7 +136,6 @@ dir_open_for_write(const char *pathname, const char *temp_suffix, size_t pad_to_ return NULL; } } - pg_free(zerobuf); if (lseek(fd, 0, SEEK_SET) != 0) { @@ -513,24 +511,20 @@ tar_write(Walfile f, const void *buf, size_t count) static bool tar_write_padding_data(TarMethodFile *f, size_t bytes) { - char *zerobuf = pg_malloc0(XLOG_BLCKSZ); + PGAlignedXLogBlock zerobuf; size_t bytesleft = bytes; + memset(zerobuf.data, 0, XLOG_BLCKSZ); while (bytesleft) { - size_t bytestowrite = bytesleft > XLOG_BLCKSZ ? XLOG_BLCKSZ : bytesleft; - - ssize_t r = tar_write(f, zerobuf, bytestowrite); + size_t bytestowrite = Min(bytesleft, XLOG_BLCKSZ); + ssize_t r = tar_write(f, zerobuf.data, bytestowrite); if (r < 0) - { - pg_free(zerobuf); return false; - } bytesleft -= r; } - pg_free(zerobuf); return true; } -- cgit v1.2.3