git: ae19883a1695 - stable/12 - Implement malloc_domainset_aligned(9).

Konstantin Belousov kib at FreeBSD.org
Tue Feb 9 08:36:55 UTC 2021


The branch stable/12 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=ae19883a16950043127aae0b65cf66be37bbc8b9

commit ae19883a16950043127aae0b65cf66be37bbc8b9
Author:     Konstantin Belousov <kib at FreeBSD.org>
AuthorDate: 2021-01-14 03:59:34 +0000
Commit:     Konstantin Belousov <kib at FreeBSD.org>
CommitDate: 2021-02-09 08:35:56 +0000

    Implement malloc_domainset_aligned(9).
    
    Tested by:      pho
    
    (cherry picked from commit 3b15beb30b3b4ba17bae3d1d43c8c04ff862bb57)
    (cherry picked from commit 0781c79d4872a84a8ebeee3b5eb5520a682b8e7b)
    (cherry picked from commit 1ac7c34486ab9177c2472278739568d4607e1acc)
---
 sys/kern/kern_malloc.c | 39 +++++++++++++++++++++++++++++++++++++--
 sys/sys/malloc.h       |  3 +++
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c
index 506da31ab12f..5e8af96cb1eb 100644
--- a/sys/kern/kern_malloc.c
+++ b/sys/kern/kern_malloc.c
@@ -658,6 +658,37 @@ malloc_domainset(size_t size, struct malloc_type *mtp, struct domainset *ds,
 	return (ret);
 }
 
+void *
+malloc_domainset_aligned(size_t size, size_t align,
+    struct malloc_type *mtp, struct domainset *ds, int flags)
+{
+	void *res;
+	size_t asize;
+
+	KASSERT(align != 0 && powerof2(align),
+	    ("malloc_domainset_aligned: wrong align %#zx size %#zx",
+	    align, size));
+	KASSERT(align <= PAGE_SIZE,
+	    ("malloc_domainset_aligned: align %#zx (size %#zx) too large",
+	    align, size));
+
+	/*
+	 * Round the allocation size up to the next power of 2,
+	 * because we can only guarantee alignment for
+	 * power-of-2-sized allocations.  Further increase the
+	 * allocation size to align if the rounded size is less than
+	 * align, since malloc zones provide alignment equal to their
+	 * size.
+	 */
+	asize = size <= align ? align : 1UL << flsl(size - 1);
+
+	res = malloc_domainset(asize, mtp, ds, flags);
+	KASSERT(res == NULL || ((uintptr_t)res & (align - 1)) == 0,
+	    ("malloc_domainset_aligned: result not aligned %p size %#zx "
+	    "allocsize %#zx align %#zx", res, size, asize, align));
+	return (res);
+}
+
 void *
 mallocarray(size_t nmemb, size_t size, struct malloc_type *type, int flags)
 {
@@ -1024,8 +1055,12 @@ mallocinit(void *dummy)
 	for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) {
 		int size = kmemzones[indx].kz_size;
 		char *name = kmemzones[indx].kz_name;
+		size_t align;
 		int subzone;
 
+		align = UMA_ALIGN_PTR;
+		if (powerof2(size) && size > sizeof(void *))
+			align = MIN(size, PAGE_SIZE) - 1;
 		for (subzone = 0; subzone < numzones; subzone++) {
 			kmemzones[indx].kz_zone[subzone] =
 			    uma_zcreate(name, size,
@@ -1034,8 +1069,8 @@ mallocinit(void *dummy)
 #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
index ffbdc4befd11..fd50dbc495b4 100644
--- a/sys/sys/malloc.h
+++ b/sys/sys/malloc.h
@@ -249,6 +249,9 @@ void	*realloc(void *addr, size_t size, struct malloc_type *type, int flags)
 	    __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);
 


More information about the dev-commits-src-all mailing list