diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c --- a/sys/kern/kern_malloc.c +++ b/sys/kern/kern_malloc.c @@ -763,6 +763,19 @@ return (malloc_large(&size, mtp, ds, flags DEBUG_REDZONE_ARG)); } +void * +malloc_domainset_aligned(size_t size, size_t align, + struct malloc_type *mtp, struct domainset *ds, int flags) +{ + MPASS(powerof2(align)); + KASSERT(align <= kmemzones[nitems(kmemzones) - 2].kz_size, + ("malloc_domainset_aligned: align %#zx too large", align)); + + if (size < align) + size = align; + return (malloc_domainset(size, mtp, ds, flags)); +} + void * mallocarray(size_t nmemb, size_t size, struct malloc_type *type, int flags) { @@ -1146,8 +1159,12 @@ for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) { int size = kmemzones[indx].kz_size; const char *name = kmemzones[indx].kz_name; + size_t align; int subzone; + align = UMA_ALIGN_PTR; + if (powerof2(size) && size > sizeof(void *)) + align = size - 1; for (subzone = 0; subzone < numzones; subzone++) { kmemzones[indx].kz_zone[subzone] = uma_zcreate(name, size, @@ -1156,7 +1173,7 @@ #else NULL, NULL, NULL, NULL, #endif - UMA_ALIGN_PTR, UMA_ZONE_MALLOC); + align, UMA_ZONE_MALLOC); } for (;i <= size; i+= KMEM_ZBASE) kmemsize[i >> KMEM_ZSHIFT] = indx; diff --git a/sys/sys/malloc.h b/sys/sys/malloc.h --- a/sys/sys/malloc.h +++ b/sys/sys/malloc.h @@ -261,6 +261,9 @@ __result_use_check __alloc_size(2); void *reallocf(void *addr, size_t size, struct malloc_type *type, int flags) __result_use_check __alloc_size(2); +void *malloc_domainset_aligned(size_t size, size_t align, + struct malloc_type *mtp, struct domainset *ds, int flags) + __malloc_like __result_use_check __alloc_size(1); struct malloc_type *malloc_desc2type(const char *desc); diff --git a/sys/x86/x86/busdma_bounce.c b/sys/x86/x86/busdma_bounce.c --- a/sys/x86/x86/busdma_bounce.c +++ b/sys/x86/x86/busdma_bounce.c @@ -445,19 +445,14 @@ * else allocate a block of contiguous pages because one or more of the * constraints is something that only the contig allocator can fulfill. * - * NOTE: The (dmat->common.alignment <= dmat->maxsize) check - * below is just a quick hack. The exact alignment guarantees - * of malloc(9) need to be nailed down, and the code below - * should be rewritten to take that into account. - * - * In the meantime warn the user if malloc gets it wrong. + * Warn the user if malloc gets it wrong. */ if (dmat->common.maxsize <= PAGE_SIZE && - dmat->common.alignment <= dmat->common.maxsize && dmat->common.lowaddr >= ptoa((vm_paddr_t)Maxmem) && attr == VM_MEMATTR_DEFAULT) { - *vaddr = malloc_domainset(dmat->common.maxsize, M_DEVBUF, - DOMAINSET_PREF(dmat->common.domain), mflags); + *vaddr = malloc_domainset_aligned(dmat->common.maxsize, + dmat->common.alignment, + M_DEVBUF, DOMAINSET_PREF(dmat->common.domain), mflags); } else if (dmat->common.nsegments >= howmany(dmat->common.maxsize, MIN(dmat->common.maxsegsz, PAGE_SIZE)) &&