From nobody Tue Feb 01 04:53:22 2022 X-Original-To: dev-commits-src-main@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 D1CC7198D7FD; Tue, 1 Feb 2022 04:53:22 +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 4JnsyB535Bz3jHj; Tue, 1 Feb 2022 04:53:22 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1643691202; 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=p3Y3tAWxKYPSGmeRtyXkK+4SYIv07WKo33mdnH5E17Q=; b=I7XdiEZ67BQWMHwmuPpx9psw/MhMxPTaU8cTp43aHmeSOZ4RrKsdV4Gx5J1tE1RzEhIdxZ DCds9IA+zHQAHesuORG4u1I1EhGaCtlhgKVzwuB/ahZm/hIc8kkdcjrXQQqyXjfcoikSnQ 0V4f5NhZOJTCXt1/qOVBWj3Wd1vakcLBXZxqCy8r1oQ55nh8wmUUTpyn+rtnxGdtYjxUq0 fvCGwmlMwUHzXr7n3o3NOPMKaJVL6S9YJ2LJ5jPHvoGzepPDGSya9aEI+dlWwo8EikcNI5 8PxCjO9iqWtY+iouHRXdeQvCJC1dE/I2TAcJprF3pu4pVw3svZiEF3alP0WhvQ== 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 8D38E1BB4; Tue, 1 Feb 2022 04:53:22 +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 2114rMqw007516; Tue, 1 Feb 2022 04:53:22 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2114rM0V007515; Tue, 1 Feb 2022 04:53:22 GMT (envelope-from git) Date: Tue, 1 Feb 2022 04:53:22 GMT Message-Id: <202202010453.2114rM0V007515@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: 54d34bfbdf08 - main - Introduce sys/kassert.h List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org 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: 54d34bfbdf08d383b0ecb9a721434ac732620c79 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1643691202; 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=p3Y3tAWxKYPSGmeRtyXkK+4SYIv07WKo33mdnH5E17Q=; b=jFwsr7PL+rfJ+16dPRbJf9qgPZisl6xUdaV2YJuPMMnUU899+BE+lqCHQG5PV8zTYA7YcU HV6fEIV+73YSrQtSWB3bmZN9QJeZMBDf3AgGYK2LrwNc22hLI9kmNALbMWPP0zwbNBZ87c pyFDn6dBAyyOPFCp2m/B7OVSNW6PLREwALPbUNh3eWFMURPlyfaTwO5a3TKjFRhIwGjKWD KQ5lh+BghrGOuQHWLSWEmUMRKAXy2vlXFTQKLGTI/Zc103VEaYR67LiTBlNhwGrHcM7fpI Rzi9HorQPGacIuGE3F63R+ZINaJk2Mgmva1Bfn2LwIfqoLpgi3SHU6mhFxejYA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1643691202; a=rsa-sha256; cv=none; b=D1xj1F0JiR04b+nqHfF2dqwB6uWMisPCGvZ+HcJ6piuiBr9SKFHcCDa+dTF+rPKoA382Sk yF+kEFzobuOD0qFjxRi3n6nXuYGbUk/gozu3ZcygMcmlmi4fNXV+cVubZZ8YD35FSMyp71 V5g3jDMfiAwpBMUjt97nS7Z+VQ5qcFQ/QNKvnC7/qPkzv2S4qf82FYrUF5CQlV3hkQ9Ku7 Ht0lH87ColvaC1CBoIGPOAVe4We/VIaXQrTOfgZlH0QEMmfIPmyx1x84QPqaJoriWLWChs wsX0Mc2IBkBgCDMWeBc6Tcrx6+hs9mCAfVC6qGB/x/s0wC65atkOvMQazA8FmA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=54d34bfbdf08d383b0ecb9a721434ac732620c79 commit 54d34bfbdf08d383b0ecb9a721434ac732620c79 Author: Konstantin Belousov AuthorDate: 2022-01-29 03:37:15 +0000 Commit: Konstantin Belousov CommitDate: 2022-02-01 03:14:14 +0000 Introduce sys/kassert.h It contains assert-related definitions previously provided by sys/systm.h. The new header is leaner than whole systm.h. Include kassert.h from systm.h for compatibility. The copyright assignment to Eivind Eklund was suggested by Kirk McKusick and is based in the commit 5526d2d920eb17b1507499f35b275b486f7fe8d0. Suggested by: jhb Reviewed by: alc, imp, jhb Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D34089 --- sys/sys/kassert.h | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ sys/sys/systm.h | 115 ++--------------------------------------- 2 files changed, 154 insertions(+), 110 deletions(-) diff --git a/sys/sys/kassert.h b/sys/sys/kassert.h new file mode 100644 index 000000000000..bb6847dc404f --- /dev/null +++ b/sys/sys/kassert.h @@ -0,0 +1,149 @@ +/*- + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 1999 Eivind Eklund + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _SYS_KASSERT_H_ +#define _SYS_KASSERT_H_ + +#include + +#ifdef _KERNEL +extern const char *panicstr; /* panic message */ +extern bool panicked; +#define KERNEL_PANICKED() __predict_false(panicked) + +#ifdef INVARIANTS /* The option is always available */ +#define VNASSERT(exp, vp, msg) do { \ + if (__predict_false(!(exp))) { \ + vn_printf(vp, "VNASSERT failed: %s not true at %s:%d (%s)\n",\ + #exp, __FILE__, __LINE__, __func__); \ + kassert_panic msg; \ + } \ +} while (0) +#define VNPASS(exp, vp) do { \ + const char *_exp = #exp; \ + VNASSERT(exp, vp, ("condition %s not met at %s:%d (%s)", \ + _exp, __FILE__, __LINE__, __func__)); \ +} while (0) +#define __assert_unreachable() \ + panic("executing segment marked as unreachable at %s:%d (%s)\n", \ + __FILE__, __LINE__, __func__) +#else /* INVARIANTS */ +#define VNASSERT(exp, vp, msg) do { \ +} while (0) +#define VNPASS(exp, vp) do { \ +} while (0) +#define __assert_unreachable() __unreachable() +#endif /* INVARIANTS */ + +#ifndef CTASSERT /* Allow lint to override */ +#define CTASSERT(x) _Static_assert(x, "compile-time assertion failed") +#endif + +/* + * These functions need to be declared before the KASSERT macro is invoked in + * !KASSERT_PANIC_OPTIONAL builds, so their declarations are sort of out of + * place compared to other function definitions in this header. On the other + * hand, this header is a bit disorganized anyway. + */ +void panic(const char *, ...) __dead2 __printflike(1, 2); +void vpanic(const char *, __va_list) __dead2 __printflike(1, 0); +#endif /* _KERNEL */ + +#if defined(_STANDALONE) +/* + * Until we have more experience with KASSERTS that are called + * from the boot loader, they are off. The bootloader does this + * a little differently than the kernel (we just call printf atm). + * we avoid most of the common functions in the boot loader, so + * declare printf() here too. + */ +int printf(const char *, ...) __printflike(1, 2); +# define kassert_panic printf +#else /* !_STANDALONE */ +# if defined(WITNESS) || defined(INVARIANT_SUPPORT) +# ifdef KASSERT_PANIC_OPTIONAL +void kassert_panic(const char *fmt, ...) __printflike(1, 2); +# else +# define kassert_panic panic +# endif /* KASSERT_PANIC_OPTIONAL */ +# endif /* defined(WITNESS) || defined(INVARIANT_SUPPORT) */ +#endif /* _STANDALONE */ + +#if (defined(_KERNEL) && defined(INVARIANTS)) || defined(_STANDALONE) +#define KASSERT(exp,msg) do { \ + if (__predict_false(!(exp))) \ + kassert_panic msg; \ +} while (0) +#else /* !(KERNEL && INVARIANTS) && !_STANDALONE */ +#define KASSERT(exp,msg) do { \ +} while (0) +#endif /* (_KERNEL && INVARIANTS) || _STANDALONE */ + +#ifdef _KERNEL +/* + * Helpful macros for quickly coming up with assertions with informative + * panic messages. + */ +#define MPASS(ex) MPASS4(ex, #ex, __FILE__, __LINE__) +#define MPASS2(ex, what) MPASS4(ex, what, __FILE__, __LINE__) +#define MPASS3(ex, file, line) MPASS4(ex, #ex, file, line) +#define MPASS4(ex, what, file, line) \ + KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line)) + +/* + * Assert that a pointer can be loaded from memory atomically. + * + * This assertion enforces stronger alignment than necessary. For example, + * on some architectures, atomicity for unaligned loads will depend on + * whether or not the load spans multiple cache lines. + */ +#define ASSERT_ATOMIC_LOAD_PTR(var, msg) \ + KASSERT(sizeof(var) == sizeof(void *) && \ + ((uintptr_t)&(var) & (sizeof(void *) - 1)) == 0, msg) +/* + * Assert that a thread is in critical(9) section. + */ +#define CRITICAL_ASSERT(td) \ + KASSERT((td)->td_critnest >= 1, ("Not in critical section")) + +/* + * If we have already panic'd and this is the thread that called + * panic(), then don't block on any mutexes but silently succeed. + * Otherwise, the kernel will deadlock since the scheduler isn't + * going to run the thread that holds any lock we need. + */ +#define SCHEDULER_STOPPED_TD(td) ({ \ + MPASS((td) == curthread); \ + __predict_false((td)->td_stopsched); \ +}) +#define SCHEDULER_STOPPED() SCHEDULER_STOPPED_TD(curthread) +#endif /* _KERNEL */ + +#endif /* _SYS_KASSERT_H_ */ diff --git a/sys/sys/systm.h b/sys/sys/systm.h index 25a2d0c41e82..f2ffa7e6b815 100644 --- a/sys/sys/systm.h +++ b/sys/sys/systm.h @@ -44,6 +44,7 @@ #include #include #include +#include #include #include /* for people using printf mainly */ @@ -53,9 +54,6 @@ __NULLABILITY_PRAGMA_PUSH extern int cold; /* nonzero if we are doing a cold boot */ extern int suspend_blocked; /* block suspend due to pending shutdown */ extern int rebooting; /* kern_reboot() has been called. */ -extern const char *panicstr; /* panic message */ -extern bool panicked; -#define KERNEL_PANICKED() __predict_false(panicked) extern char version[]; /* system version */ extern char compiler_version[]; /* compiler version */ extern char copyright[]; /* system copyright */ @@ -86,86 +84,8 @@ enum VM_GUEST { VM_GUEST_NO = 0, VM_GUEST_VM, VM_GUEST_XEN, VM_GUEST_HV, VM_GUEST_VMWARE, VM_GUEST_KVM, VM_GUEST_BHYVE, VM_GUEST_VBOX, VM_GUEST_PARALLELS, VM_LAST }; -#ifdef INVARIANTS /* The option is always available */ -#define VNASSERT(exp, vp, msg) do { \ - if (__predict_false(!(exp))) { \ - vn_printf(vp, "VNASSERT failed: %s not true at %s:%d (%s)\n",\ - #exp, __FILE__, __LINE__, __func__); \ - kassert_panic msg; \ - } \ -} while (0) -#define VNPASS(exp, vp) do { \ - const char *_exp = #exp; \ - VNASSERT(exp, vp, ("condition %s not met at %s:%d (%s)", \ - _exp, __FILE__, __LINE__, __func__)); \ -} while (0) -#define __assert_unreachable() \ - panic("executing segment marked as unreachable at %s:%d (%s)\n", \ - __FILE__, __LINE__, __func__) -#else -#define VNASSERT(exp, vp, msg) do { \ -} while (0) -#define VNPASS(exp, vp) do { \ -} while (0) -#define __assert_unreachable() __unreachable() -#endif - -#ifndef CTASSERT /* Allow lint to override */ -#define CTASSERT(x) _Static_assert(x, "compile-time assertion failed") -#endif #endif /* KERNEL */ -/* - * These functions need to be declared before the KASSERT macro is invoked in - * !KASSERT_PANIC_OPTIONAL builds, so their declarations are sort of out of - * place compared to other function definitions in this header. On the other - * hand, this header is a bit disorganized anyway. - */ -void panic(const char *, ...) __dead2 __printflike(1, 2); -void vpanic(const char *, __va_list) __dead2 __printflike(1, 0); - - -#if defined(_STANDALONE) -struct ucred; -/* - * Until we have more experience with KASSERTS that are called - * from the boot loader, they are off. The bootloader does this - * a little differently than the kernel (we just call printf atm). - * we avoid most of the common functions in the boot loader, so - * declare printf() here too. - */ -int printf(const char *, ...) __printflike(1, 2); -# define kassert_panic printf -#else /* !_STANDALONE */ -# if defined(WITNESS) || defined(INVARIANT_SUPPORT) -# ifdef KASSERT_PANIC_OPTIONAL -void kassert_panic(const char *fmt, ...) __printflike(1, 2); -# else -# define kassert_panic panic -# endif /* KASSERT_PANIC_OPTIONAL */ -# endif /* defined(WITNESS) || defined(INVARIANT_SUPPORT) */ -#endif /* _STANDALONE */ - -#if defined(INVARIANTS) || defined(_STANDALONE) -#define KASSERT(exp,msg) do { \ - if (__predict_false(!(exp))) \ - kassert_panic msg; \ -} while (0) -#else /* !INVARIANTS && !_STANDALONE */ -#define KASSERT(exp,msg) do { \ -} while (0) -#endif /* INVARIANTS || _STANDALONE */ - -/* - * Helpful macros for quickly coming up with assertions with informative - * panic messages. - */ -#define MPASS(ex) MPASS4(ex, #ex, __FILE__, __LINE__) -#define MPASS2(ex, what) MPASS4(ex, what, __FILE__, __LINE__) -#define MPASS3(ex, file, line) MPASS4(ex, #ex, file, line) -#define MPASS4(ex, what, file, line) \ - KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line)) - /* * Align variables. */ @@ -173,40 +93,15 @@ void kassert_panic(const char *fmt, ...) __printflike(1, 2); #define __read_frequently __section(".data.read_frequently") #define __exclusive_cache_line __aligned(CACHE_LINE_SIZE) \ __section(".data.exclusive_cache_line") +#if defined(_STANDALONE) +struct ucred; +#endif + #ifdef _KERNEL #include /* MAXCPU */ #include /* curthread */ #include -/* - * Assert that a pointer can be loaded from memory atomically. - * - * This assertion enforces stronger alignment than necessary. For example, - * on some architectures, atomicity for unaligned loads will depend on - * whether or not the load spans multiple cache lines. - */ -#define ASSERT_ATOMIC_LOAD_PTR(var, msg) \ - KASSERT(sizeof(var) == sizeof(void *) && \ - ((uintptr_t)&(var) & (sizeof(void *) - 1)) == 0, msg) - -/* - * Assert that a thread is in critical(9) section. - */ -#define CRITICAL_ASSERT(td) \ - KASSERT((td)->td_critnest >= 1, ("Not in critical section")) - -/* - * If we have already panic'd and this is the thread that called - * panic(), then don't block on any mutexes but silently succeed. - * Otherwise, the kernel will deadlock since the scheduler isn't - * going to run the thread that holds any lock we need. - */ -#define SCHEDULER_STOPPED_TD(td) ({ \ - MPASS((td) == curthread); \ - __predict_false((td)->td_stopsched); \ -}) -#define SCHEDULER_STOPPED() SCHEDULER_STOPPED_TD(curthread) - extern int osreldate; extern const void *zero_region; /* address space maps to a zeroed page */