From nobody Sat Sep 28 21:52:17 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 4XGLf9423Rz5XtqV; Sat, 28 Sep 2024 21:52:17 +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 4XGLf93BlTz44R6; Sat, 28 Sep 2024 21:52:17 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1727560337; 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=WU/uvUN9sZ5+QA72Mnk/Ob5LCK/LW4b8N1k0CgEb8EA=; b=B4VGs8MhK0S9EFGAblbMjxdOEj8KXY7bX8cuEx4UZgz2TrqIL/81mlxyDnJSlpIwW/IC4H arrLTzmxixoZWNQxcii4xZEYSTu93wXaDeInXLbaOSecVXCP75Bm4warJQXjg7beekqia6 3VgFN7rIZUsFzfiiVaj2PciTodKfCKM4x3LUwN++OPThsdyETm6bjQn5bw+3Pe2j2MpbXB SZODmwC/AS+atGOdZ9wddkve5iAHXhGpYdQ+y+7OKvmzXMvVhm1zAYY1Btj39Yw6r45ENt t8KYVYV1IDyoP63O8qKQv4W+wj7JkOEYeJKPRgPMIHSVB999NNvP9ndxjfeLQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1727560337; 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=WU/uvUN9sZ5+QA72Mnk/Ob5LCK/LW4b8N1k0CgEb8EA=; b=fwj1fg3J6PgvkkeyLIPe4txNu1oYayrIL046gtbIIUV6fqZI8k7bu2wELhPuezFrPAefb9 090paG/lUn9EnQ6EoNkwvzxgHFdeFdIN88SjTERM6ZJmX41PLpU2/a0ifvEdhxTWJBKQew +6HgpE7AIQ2GrkprnMGRj1JiMZ0TXOdSLM3qgR0MBd3RpdapqNFEDll9yDM8bBDTHPzfH8 iKpgHE9TZzMHYUP+nCXSEzhQxn/1HhL6ErDn1rCyOTfsStFufVRcDG7U5VP3QjSktV8sQX gBFkA0vXksrmeKQPbIAAT7ZnbIrLb1vKhFPSz21Me/RyHG4qSxpEEtucpN3O2w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1727560337; a=rsa-sha256; cv=none; b=e22SJ4ISG+xeJwmSfD2q5KVWlkj1zotkXNSgHAsDnrlcqxrfeFDv1Tt2NCdlI02IPcxogI TCcvZ2p8xay2raUxgUa+bYKjkO1L4TlYsmkFzFMscE2tAbDSmyvXlLy5Nx8roTmgO69xIH +Zvb/gUhfWJJcyUIWeKJy7vUXZ8LQ/iVnpeqfAdO39FQmCo3UEXmgBBY++sVNbwGJ1gb2s +Yr6yRTjM+rTGS4I3GnTOYEdWZ/9iBGcmWc7TetXYQQat2DMoQtjUKa8eddBL25mOTQu1j UcanvG8mb8LACKGdxCPWsl0dyHPEcaRoVcwrlaono77rbn8XeWvhBc9QCy3UnQ== 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 4XGLf92MT3zhpX; Sat, 28 Sep 2024 21:52:17 +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 48SLqHKi051709; Sat, 28 Sep 2024 21:52:17 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 48SLqHsW051706; Sat, 28 Sep 2024 21:52:17 GMT (envelope-from git) Date: Sat, 28 Sep 2024 21:52:17 GMT Message-Id: <202409282152.48SLqHsW051706@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: "Bjoern A. Zeeb" Subject: git: d7670d965c3f - stable/13 - 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/stable/13 X-Git-Reftype: branch X-Git-Commit: d7670d965c3fc3dcb36162a5054356524b3fd4ce Auto-Submitted: auto-generated The branch stable/13 has been updated by bz: URL: https://cgit.FreeBSD.org/src/commit/?id=d7670d965c3fc3dcb36162a5054356524b3fd4ce commit d7670d965c3fc3dcb36162a5054356524b3fd4ce Author: Bjoern A. Zeeb AuthorDate: 2024-07-24 15:56:32 +0000 Commit: Bjoern A. Zeeb CommitDate: 2024-09-28 19:33:43 +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 Reviewed by: kib, markj Differential Revision: https://reviews.freebsd.org/D46101 And the fix from Olivier Certner (olce): kern_malloc: Restore working KASAN runtime after free() and zfree() folding In the zfree() case, the explicit_bzero() calls zero all the allocation, including the redzone which malloc() has marked as invalid. So calling kasan_mark() before those is in fact necessary. This fixes a crash at boot when 'ldconfig' is run and tries to get random bytes through getrandom() (relevant part of the stack is read_random_uio() -> zfree() -> explicit_bzero()) for kernels with KASAN compiled in. Approved by: markj (mentor) Fixes: 4fab5f005482 ("kern_malloc: fold free and zfree together into one __always_inline func") MFC with: 4fab5f005482 Sponsored by: The FreeBSD Foundation (cherry picked from commit 4fab5f005482aa88bc0f7d7a0a5e81b436869112) (cherry picked from commit 28391f188ca18b6251ba46040adf81946b0ccb03) --- sys/kern/kern_malloc.c | 94 +++++++++++++++++--------------------------------- 1 file changed, 31 insertions(+), 63 deletions(-) diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c index 6a36f982cc78..4669e072c966 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,54 @@ free(void *addr, struct malloc_type *mtp) #if defined(INVARIANTS) && !defined(KASAN) free_save_type(addr, mtp, size); #endif + if (dozero) { + 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); + if (dozero) { + kasan_mark(addr, size, size, 0); + 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((vm_offset_t)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((vm_offset_t)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); } /*