From owner-dev-commits-src-all@freebsd.org Fri Jul 9 17:26:57 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 8A27F663BA1; Fri, 9 Jul 2021 17:26:57 +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 4GM0TF3GxRz4b9w; Fri, 9 Jul 2021 17:26:57 +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 58AB91899A; Fri, 9 Jul 2021 17:26:57 +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 169HQvxR084474; Fri, 9 Jul 2021 17:26:57 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 169HQvGQ084473; Fri, 9 Jul 2021 17:26:57 GMT (envelope-from git) Date: Fri, 9 Jul 2021 17:26:57 GMT Message-Id: <202107091726.169HQvGQ084473@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: 72821668b039 - main - stand/kmem_zalloc: panic when a M_WAITOK allocation fails MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 72821668b039c276914569e9caa1cdfa4e4cb674 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: Fri, 09 Jul 2021 17:26:57 -0000 The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=72821668b039c276914569e9caa1cdfa4e4cb674 commit 72821668b039c276914569e9caa1cdfa4e4cb674 Author: Warner Losh AuthorDate: 2021-07-09 17:21:18 +0000 Commit: Warner Losh CommitDate: 2021-07-09 17:21:18 +0000 stand/kmem_zalloc: panic when a M_WAITOK allocation fails Malloc() might return NULL, in which case we will panic with a NULL pointer deref. Make it panic when the allocation fails to preserve the postcondtion that we never return a non-NULL value. Reviewed by: tsoome PR: 249859 Sponsored by: Netflix Differential Revision: https://reviews.freebsd.org/D31106 --- sys/sys/malloc.h | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/sys/sys/malloc.h b/sys/sys/malloc.h index 0c585c5a0dcf..b8c2788edd44 100644 --- a/sys/sys/malloc.h +++ b/sys/sys/malloc.h @@ -298,16 +298,20 @@ extern void *Malloc(size_t bytes, const char *file, int line); * flags mean anything and there's no need declare malloc types. * Define the simple alloc / free routines in terms of Malloc and * Free. None of the kernel features that this stuff disables are needed. - * - * XXX we are setting ourselves up for a potential crash if we can't allocate - * memory for a M_WAITOK call. */ -#define M_WAITOK 0 +#define M_WAITOK 1 #define M_ZERO 0 -#define M_NOWAIT 0 +#define M_NOWAIT 2 #define MALLOC_DECLARE(x) -#define kmem_zalloc(size, flags) Malloc((size), __FILE__, __LINE__) +#define kmem_zalloc(size, flags) ({ \ + void *p = Malloc((size), __FILE__, __LINE__); \ + if (p == NULL && (flags & M_WAITOK) != 0) \ + panic("Could not malloc %zd bytes with M_WAITOK from %s line %d", \ + (size_t)size, __FILE__, __LINE__); \ + p; \ +}) + #define kmem_free(p, size) Free(p, __FILE__, __LINE__) /* @@ -315,5 +319,6 @@ extern void *Malloc(size_t bytes, const char *file, int line); * M_WAITOK. Given the above, it will also be a nop. */ #define KM_SLEEP M_WAITOK +#define KM_NOSLEEP M_NOWAIT #endif /* _STANDALONE */ #endif /* !_SYS_MALLOC_H_ */