From owner-dev-commits-src-branches@freebsd.org Tue Feb 9 08:36:54 2021 Return-Path: Delivered-To: dev-commits-src-branches@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id C5B4A536355; Tue, 9 Feb 2021 08:36:54 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4DZbps6Q2Sz4gMP; Tue, 9 Feb 2021 08:36:53 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 15CE5276D0; Tue, 9 Feb 2021 08:36:53 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1198aqiU013903; Tue, 9 Feb 2021 08:36:52 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1198aqM0013902; Tue, 9 Feb 2021 08:36:52 GMT (envelope-from git) Date: Tue, 9 Feb 2021 08:36:52 GMT Message-Id: <202102090836.1198aqM0013902@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: ae19883a1695 - stable/12 - Implement malloc_domainset_aligned(9). MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/12 X-Git-Reftype: branch X-Git-Commit: ae19883a16950043127aae0b65cf66be37bbc8b9 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-branches@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commits to the stable branches of the FreeBSD src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Feb 2021 08:36:55 -0000 The branch stable/12 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=ae19883a16950043127aae0b65cf66be37bbc8b9 commit ae19883a16950043127aae0b65cf66be37bbc8b9 Author: Konstantin Belousov AuthorDate: 2021-01-14 03:59:34 +0000 Commit: Konstantin Belousov 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);