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.
This commit is contained in:
Michael Lotz
2016-12-11 01:09:17 +01:00
parent fda0bf77fb
commit 40044981eb
+8 -1
View File
@@ -14,6 +14,8 @@
#include <KernelExport.h>
#include <image.h>
#include <util/BitUtils.h>
#include <compat/machine/resource.h>
#include <compat/dev/mii/mii.h>
#include <compat/sys/bus.h>
@@ -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;