From 40044981eb5f9424efab47461a8b7f3e626d937d Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Sat, 10 Dec 2016 22:58:08 +0100 Subject: [PATCH] FreeBSD compat: Align allocations according to FreeBSD man page. According to the FreeBSD kernel malloc man page the allocator is expected to return power of two aligned addresses for allocations up to one page size. While it also states that this shouldn't be relied upon, at least our (directly copied) bus_dmamem_alloc expects it and drivers may depend on it as well. Looking through the FreeBSD commit logs, this expectation seems to be rooted quite deeply. This fixes watchdog timeouts in the ipro1000 driver under KVM and may help with #11953. It might also be related to #9099 and #9601 as those seem memory allocation related as well. --- src/libs/compat/freebsd_network/compat.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/libs/compat/freebsd_network/compat.c b/src/libs/compat/freebsd_network/compat.c index 33957800b1..f745e11488 100644 --- a/src/libs/compat/freebsd_network/compat.c +++ b/src/libs/compat/freebsd_network/compat.c @@ -14,6 +14,8 @@ #include #include +#include + #include #include #include @@ -602,7 +604,12 @@ _kernel_malloc(size_t size, int flags) { // our kernel malloc() is insufficient, must handle M_WAIT - void *ptr = malloc(size); + // According to the FreeBSD kernel malloc man page the allocator is expected + // to return power of two aligned addresses for allocations up to one page + // size. While it also states that this shouldn't be relied upon, at least + // bus_dmamem_alloc expects it and drivers may depend on it as well. + void *ptr + = memalign(size >= PAGE_SIZE ? PAGE_SIZE : next_power_of_2(size), size); if (ptr == NULL) return NULL;