From fbaf5955eee6b028e7f301dd0609a413f5aa2f3e Mon Sep 17 00:00:00 2001 From: Jonas Pfeil Date: Wed, 1 Mar 2017 18:11:10 +0100 Subject: [PATCH] ralloc: Make sure ralloc() allocations match malloc()'s alignment. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The header of ralloc needs to be aligned, because the compiler assumes that malloc returns will be aligned to 8/16 bytes depending on the platform, leading to degraded performance or alignment faults with ralloc. Fixes SIGBUS on Raspberry Pi at high optimization levels. This patch is not perfect for MSVC, as maybe in the future the alignment for the most demanding data type might change to more than 8. v2: Commit message reword/typo fix, and add a bigger explanation in the code (by anholt) Signed-off-by: Jonas Pfeil Reviewed-by: Matt Turner Reviewed-by: Marek Olšák Cc: mesa-stable@lists.freedesktop.org (cherry picked from commit cd2b55e536dc806f9358f71db438dd9c246cdb14) Squashed with ralloc: don't leave out the alignment factor Experimentation shows that without alignment factor gcc and clang choose a factor of 16 even on IA-32, which doesn't match what malloc() uses (8). The problem is it makes gcc assume the pointer is 16 byte aligned, so with -O3 it starts using aligned SSE instructions that later fault, so always specify a suitable alignment factor. Cc: Jonas Pfeil Fixes: cd2b55e5 "ralloc: Make sure ralloc() allocations match malloc()'s alignment." Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=100049 Signed-off-by: Grazvydas Ignotas Tested by: Mike Lothian Tested by: Jonas Pfeil (cherry picked from commit ff494fe999510ea40e3ed5827e7818550b6de126) --- src/util/ralloc.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/util/ralloc.c b/src/util/ralloc.c index 980e4e4f138..b08df9d552f 100644 --- a/src/util/ralloc.c +++ b/src/util/ralloc.c @@ -51,7 +51,20 @@ _CRTIMP int _vscprintf(const char *format, va_list argptr); #define CANARY 0x5A1106 -struct ralloc_header +/* Align the header's size so that ralloc() allocations will return with the + * same alignment as a libc malloc would have (8 on 32-bit GLIBC, 16 on + * 64-bit), avoiding performance penalities on x86 and alignment faults on + * ARM. + */ +struct +#ifdef _MSC_VER + __declspec(align(8)) +#elif defined(__LP64__) + __attribute__((aligned(16))) +#else + __attribute__((aligned(8))) +#endif + ralloc_header { #ifdef DEBUG /* A canary value used to determine whether a pointer is ralloc'd. */