From ca86693be28e7194ad1cf3f628004f98c872ec26 Mon Sep 17 00:00:00 2001 From: uebayasi Date: Sun, 13 Jul 2014 15:46:21 +0000 Subject: [PATCH] KERNEL_ASSERT_LOCKED(9): Assertion for kernel lock (Rev. 3) This adds a new assertion macro, KERNEL_ASSERT_LOCKED(), to assert that kernel_lock is held. In the long process of removing kernel_lock, there will be a lot (hundreds or thousands) of use of this; virtually almost all functions in !MP-safe subsystems should have this assertion. Thus this assertion should have a short, good name. Not only that "KERNEL_ASSERT_LOCKED" is consistent with other KERNEL_* and SCHED_ASSERT_LOCKED() macros. Input from dlg@ guenther@ kettenis@. OK dlg@ guenther@ --- sys/kern/kern_fork.c | 4 ++-- sys/kern/kern_ktrace.c | 6 ++---- sys/kern/kern_lock.c | 8 +++++++- sys/kern/kern_sig.c | 10 +++------- sys/sys/systm.h | 7 ++++++- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c index 2e035f17ab2..3efa176192b 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_fork.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_fork.c,v 1.172 2014/07/12 18:43:32 tedu Exp $ */ +/* $OpenBSD: kern_fork.c,v 1.173 2014/07/13 15:46:21 uebayasi Exp $ */ /* $NetBSD: kern_fork.c,v 1.29 1996/02/09 18:59:34 christos Exp $ */ /* @@ -614,7 +614,7 @@ proc_trampoline_mp(void) __mp_unlock(&sched_lock); spl0(); SCHED_ASSERT_UNLOCKED(); - KASSERT(__mp_lock_held(&kernel_lock) == 0); + KERNEL_ASSERT_UNLOCKED(); KERNEL_LOCK(); } diff --git a/sys/kern/kern_ktrace.c b/sys/kern/kern_ktrace.c index d0f68be1153..7c3f1d4762d 100644 --- a/sys/kern/kern_ktrace.c +++ b/sys/kern/kern_ktrace.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_ktrace.c,v 1.68 2014/07/13 15:00:40 tedu Exp $ */ +/* $OpenBSD: kern_ktrace.c,v 1.69 2014/07/13 15:46:21 uebayasi Exp $ */ /* $NetBSD: kern_ktrace.c,v 1.23 1996/02/09 18:59:36 christos Exp $ */ /* @@ -332,9 +332,7 @@ ktrstruct(struct proc *p, const char *name, const void *data, size_t datalen) void *buf; size_t buflen; -#ifdef MULTIPROCESSOR - KASSERT(__mp_lock_held(&kernel_lock) > 0); -#endif + KERNEL_ASSERT_LOCKED(); atomic_setbits_int(&p->p_flag, P_INKTR); ktrinitheader(&kth, p, KTR_STRUCT); diff --git a/sys/kern/kern_lock.c b/sys/kern/kern_lock.c index 946fd17e3b3..a9eb9574cd3 100644 --- a/sys/kern/kern_lock.c +++ b/sys/kern/kern_lock.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_lock.c,v 1.44 2014/07/09 13:32:00 guenther Exp $ */ +/* $OpenBSD: kern_lock.c,v 1.45 2014/07/13 15:46:21 uebayasi Exp $ */ /* * Copyright (c) 1995 @@ -134,4 +134,10 @@ _kernel_unlock(void) { __mp_unlock(&kernel_lock); } + +int +_kernel_lock_held(void) +{ + return (__mp_lock_held(&kernel_lock)); +} #endif /* MULTIPROCESSOR */ diff --git a/sys/kern/kern_sig.c b/sys/kern/kern_sig.c index 28ebd811904..970507f28b6 100644 --- a/sys/kern/kern_sig.c +++ b/sys/kern/kern_sig.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_sig.c,v 1.171 2014/07/12 21:21:19 matthew Exp $ */ +/* $OpenBSD: kern_sig.c,v 1.172 2014/07/13 15:46:21 uebayasi Exp $ */ /* $NetBSD: kern_sig.c,v 1.54 1996/04/22 01:38:32 christos Exp $ */ /* @@ -1897,9 +1897,7 @@ single_thread_set(struct proc *p, enum single_thread_mode mode, int deep) struct proc *q; int error; -#ifdef MULTIPROCESSOR - KASSERT(__mp_lock_held(&kernel_lock)); -#endif + KERNEL_ASSERT_LOCKED(); if ((error = single_thread_check(p, deep))) return error; @@ -1996,9 +1994,7 @@ single_thread_clear(struct proc *p, int flag) struct proc *q; KASSERT(pr->ps_single == p); -#ifdef MULTIPROCESSOR - KASSERT(__mp_lock_held(&kernel_lock)); -#endif + KERNEL_ASSERT_LOCKED(); pr->ps_single = NULL; atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND | PS_SINGLEEXIT); diff --git a/sys/sys/systm.h b/sys/sys/systm.h index 96386d4d081..a3929a0eda8 100644 --- a/sys/sys/systm.h +++ b/sys/sys/systm.h @@ -1,4 +1,4 @@ -/* $OpenBSD: systm.h,v 1.100 2013/06/11 18:15:54 deraadt Exp $ */ +/* $OpenBSD: systm.h,v 1.101 2014/07/13 15:46:21 uebayasi Exp $ */ /* $NetBSD: systm.h,v 1.50 1996/06/09 04:55:09 briggs Exp $ */ /*- @@ -340,16 +340,21 @@ void user_config(void); void _kernel_lock_init(void); void _kernel_lock(void); void _kernel_unlock(void); +int _kernel_lock_held(void); #define KERNEL_LOCK_INIT() _kernel_lock_init() #define KERNEL_LOCK() _kernel_lock() #define KERNEL_UNLOCK() _kernel_unlock() +#define KERNEL_ASSERT_LOCKED() KASSERT(_kernel_lock_held()) +#define KERNEL_ASSERT_UNLOCKED() KASSERT(!_kernel_lock_held()) #else /* ! MULTIPROCESSOR */ #define KERNEL_LOCK_INIT() /* nothing */ #define KERNEL_LOCK() /* nothing */ #define KERNEL_UNLOCK() /* nothing */ +#define KERNEL_ASSERT_LOCKED() /* nothing */ +#define KERNEL_ASSERT_UNLOCKED() /* nothing */ #endif /* MULTIPROCESSOR */ -- 2.20.1