From owner-dev-commits-src-all@freebsd.org Sun Jan 17 17:29:54 2021 Return-Path: Delivered-To: dev-commits-src-all@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 5E7964D0E05; Sun, 17 Jan 2021 17:29: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 4DJhkV2Bmlz3kP6; Sun, 17 Jan 2021 17:29:54 +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 2F91D252BE; Sun, 17 Jan 2021 17:29:54 +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 10HHTs6C099909; Sun, 17 Jan 2021 17:29:54 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 10HHTsHk099908; Sun, 17 Jan 2021 17:29:54 GMT (envelope-from git) Date: Sun, 17 Jan 2021 17:29:54 GMT Message-Id: <202101171729.10HHTsHk099908@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 3b15beb30b3b - main - 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/main X-Git-Reftype: branch X-Git-Commit: 3b15beb30b3b4ba17bae3d1d43c8c04ff862bb57 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 17 Jan 2021 17:29:54 -0000 The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=3b15beb30b3b4ba17bae3d1d43c8c04ff862bb57 commit 3b15beb30b3b4ba17bae3d1d43c8c04ff862bb57 Author: Konstantin Belousov AuthorDate: 2021-01-14 03:59:34 +0000 Commit: Konstantin Belousov CommitDate: 2021-01-17 17:29:05 +0000 Implement malloc_domainset_aligned(9). Change the power-of-two malloc zones to require alignment equal to the size [*]. Current uma allocator already provides such alignment, so in fact this change does not change anything except providing future-proof setup. Suggested by: markj [*] Reviewed by: andrew, jah, markj Tested by: pho MFC after: 1 week Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D28147 --- sys/kern/kern_malloc.c | 28 +++++++++++++++++++++++++++- sys/sys/malloc.h | 3 +++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c index f79352f2fbfd..232472708b9b 100644 --- a/sys/kern/kern_malloc.c +++ b/sys/kern/kern_malloc.c @@ -763,6 +763,28 @@ malloc_domainset_exec(size_t size, struct malloc_type *mtp, struct domainset *ds 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) +{ + void *res; + + KASSERT(align != 0 && powerof2(align), + ("malloc_domainset_aligned: wrong align %#zx size %#zx", + align, size)); + KASSERT(align <= kmemzones[nitems(kmemzones) - 2].kz_size, + ("malloc_domainset_aligned: align %#zx (size %#zx) too large", + align, size)); + + if (size < align) + size = align; + res = malloc_domainset(size, mtp, ds, flags); + KASSERT(res == NULL || ((uintptr_t)res & (align - 1)) == 0, + ("malloc_domainset_aligned: result not aligned %p size %#zx " + "align %#zx", res, size, align)); + return (res); +} + void * mallocarray(size_t nmemb, size_t size, struct malloc_type *type, int flags) { @@ -1146,8 +1168,12 @@ mallocinit(void *dummy) 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 +1182,7 @@ 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 bf87e3168e01..a11dd767efc5 100644 --- a/sys/sys/malloc.h +++ b/sys/sys/malloc.h @@ -261,6 +261,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);