From nobody Fri Jul 26 10:48:23 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4WVkxh1SNbz5RsKx; Fri, 26 Jul 2024 10:48:24 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4WVkxg4X7tz4VKk; Fri, 26 Jul 2024 10:48:23 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1721990903; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=gPAgVX6WBtHdQ/O9YGlMrgq7JniLVmadj/fE8JcNiAA=; b=T8ryRHIZiAe0adnDl04u0jbgntq8lE3r+na7n1ItdFC6oHukdMC7gIaW/NE6QFtV3Y8Hlg 7JQE2WUwNwz1SlkujL0JqW4sSZnfMLpTPKPb0yOlQyRIRaa94958ihiWTRsylRWBvYHlfB 6DmKLiEav2jZ8Db/z2SY6fBf04JvtmKzbYVT5mqdh4o64SOPtG7sOjtnUTeFvIOvNNjYd7 9qHrDUDpNFzCaqnmkUuGaIHWPqO41U1AcTzylKqTYGFbbRbTwPM1Ph7MO6p6FP4EdxV1y8 0NRgBeI905tdP+Q4RBxfzgTCKCkmcJ1oCrXuDQ6XgdFJt3lBpf5juQpAZ9QQVg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1721990903; a=rsa-sha256; cv=none; b=GXvJT1MlUkrg4QUr8MI29sARz4vLIHdnbsMAA+12eZp0bKKNi4rGR/QWNT3kVivjmPsMfN 0dYeAAJYkDtX8QkqRtRfemcuyaTb19htf/GAQTcO7PEifeo219To1zW/dEuP1VQ3MkMobW qsyAb9XFOO6BTEiWAtthmUMbvHyBR8Nl8eKC0x3BdJN9XYbgbZU7CrVez98nfhkysPmi/6 HOgl+V9DPTsXK8iZgeOoOVna/r9nhs+85VwgHFLR2/hZLAJL0fDJZ5iTMDCcY5hbQUWm7l Dx58Vo2B3vReAd20hZb+V9FlQsLcyjCSldmDfFo2SzWOc6QHdPLvIu5GnCVf3A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1721990903; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=gPAgVX6WBtHdQ/O9YGlMrgq7JniLVmadj/fE8JcNiAA=; b=fEhZ4owf6SRaIBz7/ESoMqsQutk5MC02TtVbU+od60uCbfefeOJUt8298BfMsnDc1e9duI CHLfhx9GcKqR/2MPgnWv9Mlg3K3H/ME8iaeZIqtPcdTMi5HSCXvw2AN30WmXMRnSguElkg 1Jkjl5lTphYyGRyZD6yiYcP/KQlXQHlHMW7jneZ9sZK6YSCiaGf9UBL7R1wyi2jj7i7wFS /DFpZe0f+cEB0Dfi6ssEpZ5SzplH+OJkofgpMd8mXijzdavy3J2ZUOQaLbyk9OMk0Wy4dV 2sm/qyUrRXtIn9UwiKVyOUDWotfvZ7CJvSWPMjzdPEXg2XcJJktBDGx8jxSAMg== 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 4WVkxg2sbFzKxf; Fri, 26 Jul 2024 10:48:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 46QAmNuT085639; Fri, 26 Jul 2024 10:48:23 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 46QAmNLD085636; Fri, 26 Jul 2024 10:48:23 GMT (envelope-from git) Date: Fri, 26 Jul 2024 10:48:23 GMT Message-Id: <202407261048.46QAmNLD085636@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Bjoern A. Zeeb" Subject: git: 4fab5f005482 - main - kern_malloc: fold free and zfree together into one __always_inline func List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bz X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 4fab5f005482aa88bc0f7d7a0a5e81b436869112 Auto-Submitted: auto-generated The branch main has been updated by bz: URL: https://cgit.FreeBSD.org/src/commit/?id=4fab5f005482aa88bc0f7d7a0a5e81b436869112 commit 4fab5f005482aa88bc0f7d7a0a5e81b436869112 Author: Bjoern A. Zeeb AuthorDate: 2024-07-24 15:56:32 +0000 Commit: Bjoern A. Zeeb CommitDate: 2024-07-26 10:46:37 +0000 kern_malloc: fold free and zfree together into one __always_inline func free() and zfree() are essentially the same copy and pasted code with the extra explicit_bzero() (and formerly kasan) calls. Add a bool to add the extra functionality and make both functions a wrapper around the common code and let the compiler do the optimization based on the bool input when inlining. No functional changes intended. Suggested by: kib (in D45812) Sponsored by: The FreeBSD Foundation MFC after: 10 days Reviewed by: kib, markj Differential Revision: https://reviews.freebsd.org/D46101 --- sys/kern/kern_malloc.c | 90 +++++++++++++++----------------------------------- 1 file changed, 27 insertions(+), 63 deletions(-) diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c index 37684e26b451..ebdd00808f22 100644 --- a/sys/kern/kern_malloc.c +++ b/sys/kern/kern_malloc.c @@ -914,15 +914,8 @@ free_dbg(void **addrp, struct malloc_type *mtp) } #endif -/* - * free: - * - * Free a block of memory allocated by malloc. - * - * This routine may not block. - */ -void -free(void *addr, struct malloc_type *mtp) +static __always_inline void +_free(void *addr, struct malloc_type *mtp, bool dozero) { uma_zone_t zone; uma_slab_t slab; @@ -938,8 +931,8 @@ free(void *addr, struct malloc_type *mtp) vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab); if (slab == NULL) - panic("free: address %p(%p) has not been allocated", - addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); + panic("%s(%d): address %p(%p) has not been allocated", __func__, + dozero, addr, (void *)((uintptr_t)addr & (~UMA_SLAB_MASK))); switch (GET_SLAB_COOKIE(slab)) { case __predict_true(SLAB_COOKIE_SLAB_PTR): @@ -947,79 +940,50 @@ free(void *addr, struct malloc_type *mtp) #if defined(INVARIANTS) && !defined(KASAN) free_save_type(addr, mtp, size); #endif + if (dozero) + explicit_bzero(addr, size); uma_zfree_arg(zone, addr, slab); break; case SLAB_COOKIE_MALLOC_LARGE: size = malloc_large_size(slab); + if (dozero) + explicit_bzero(addr, size); free_large(addr, size); break; case SLAB_COOKIE_CONTIG_MALLOC: - size = contigmalloc_size(slab); + size = round_page(contigmalloc_size(slab)); + if (dozero) + explicit_bzero(addr, size); kmem_free(addr, size); - size = round_page(size); break; default: - panic("%s: addr %p slab %p with unknown cookie %d", __func__, - addr, slab, GET_SLAB_COOKIE(slab)); + panic("%s(%d): addr %p slab %p with unknown cookie %d", + __func__, dozero, addr, slab, GET_SLAB_COOKIE(slab)); /* NOTREACHED */ } malloc_type_freed(mtp, size); } /* - * zfree: - * - * Zero then free a block of memory allocated by malloc. - * + * free: + * Free a block of memory allocated by malloc/contigmalloc. * This routine may not block. */ void -zfree(void *addr, struct malloc_type *mtp) +free(void *addr, struct malloc_type *mtp) { - uma_zone_t zone; - uma_slab_t slab; - u_long size; - -#ifdef MALLOC_DEBUG - if (free_dbg(&addr, mtp) != 0) - return; -#endif - /* free(NULL, ...) does nothing */ - if (addr == NULL) - return; - - vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab); - if (slab == NULL) - panic("free: address %p(%p) has not been allocated", - addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); + _free(addr, mtp, false); +} - switch (GET_SLAB_COOKIE(slab)) { - case __predict_true(SLAB_COOKIE_SLAB_PTR): - size = zone->uz_size; -#if defined(INVARIANTS) && !defined(KASAN) - free_save_type(addr, mtp, size); -#endif - kasan_mark(addr, size, size, 0); - explicit_bzero(addr, size); - uma_zfree_arg(zone, addr, slab); - break; - case SLAB_COOKIE_MALLOC_LARGE: - size = malloc_large_size(slab); - kasan_mark(addr, size, size, 0); - explicit_bzero(addr, size); - free_large(addr, size); - break; - case SLAB_COOKIE_CONTIG_MALLOC: - size = round_page(contigmalloc_size(slab)); - explicit_bzero(addr, size); - kmem_free(addr, size); - break; - default: - panic("%s: addr %p slab %p with unknown cookie %d", __func__, - addr, slab, GET_SLAB_COOKIE(slab)); - /* NOTREACHED */ - } - malloc_type_freed(mtp, size); +/* + * zfree: + * Zero then free a block of memory allocated by malloc/contigmalloc. + * This routine may not block. + */ +void +zfree(void *addr, struct malloc_type *mtp) +{ + _free(addr, mtp, true); } /*