From ba0fc568d9c35fb642d91e327e264398ac267760 Mon Sep 17 00:00:00 2001 From: mpi Date: Mon, 4 Dec 2017 09:51:03 +0000 Subject: [PATCH] Change __mp_lock_held() to work with an arbitrary CPU info structure and extend ddb(4) "ps /o" output to print which CPU is currently holding the KERNEL_LOCK(). Tested by dhill@, ok visa@ --- sys/arch/alpha/alpha/lock_machdep.c | 6 +++--- sys/arch/alpha/include/mplock.h | 4 ++-- sys/arch/arm64/include/mplock.h | 4 ++-- sys/arch/hppa/hppa/lock_machdep.c | 8 ++++---- sys/arch/hppa/include/mplock.h | 4 ++-- sys/arch/m88k/include/mplock.h | 6 +++--- sys/arch/powerpc/include/mplock.h | 4 ++-- sys/arch/powerpc/powerpc/lock_machdep.c | 6 +++--- sys/kern/kern_lock.c | 14 +++++++------- sys/kern/kern_proc.c | 16 ++++++++++++---- sys/sys/mplock.h | 4 ++-- sys/sys/sched.h | 9 ++++++--- 12 files changed, 48 insertions(+), 37 deletions(-) diff --git a/sys/arch/alpha/alpha/lock_machdep.c b/sys/arch/alpha/alpha/lock_machdep.c index f9292e87ca0..d1267d019d4 100644 --- a/sys/arch/alpha/alpha/lock_machdep.c +++ b/sys/arch/alpha/alpha/lock_machdep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lock_machdep.c,v 1.6 2017/05/29 14:19:49 mpi Exp $ */ +/* $OpenBSD: lock_machdep.c,v 1.7 2017/12/04 09:51:03 mpi Exp $ */ /* * Copyright (c) 2007 Artur Grabowski @@ -186,7 +186,7 @@ __mp_acquire_count(struct __mp_lock *mpl, int count) } int -__mp_lock_held(struct __mp_lock *mpl) +__mp_lock_held(struct __mp_lock *mpl, struct cpu_info *ci) { - return (mpl->mpl_cpu == curcpu()); + return (mpl->mpl_cpu == ci); } diff --git a/sys/arch/alpha/include/mplock.h b/sys/arch/alpha/include/mplock.h index 8f8523a539e..664e9430b9e 100644 --- a/sys/arch/alpha/include/mplock.h +++ b/sys/arch/alpha/include/mplock.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mplock.h,v 1.1 2014/01/26 17:40:11 miod Exp $ */ +/* $OpenBSD: mplock.h,v 1.2 2017/12/04 09:51:03 mpi Exp $ */ /* * Copyright (c) 2004 Niklas Hallqvist. All rights reserved. @@ -45,7 +45,7 @@ void __mp_unlock(struct __mp_lock *); int __mp_release_all(struct __mp_lock *); int __mp_release_all_but_one(struct __mp_lock *); void __mp_acquire_count(struct __mp_lock *, int); -int __mp_lock_held(struct __mp_lock *); +int __mp_lock_held(struct __mp_lock *, struct cpu_info *); #endif diff --git a/sys/arch/arm64/include/mplock.h b/sys/arch/arm64/include/mplock.h index f85c6df27d8..9aed99ffbf0 100644 --- a/sys/arch/arm64/include/mplock.h +++ b/sys/arch/arm64/include/mplock.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mplock.h,v 1.1 2016/12/17 23:38:33 patrick Exp $ */ +/* $OpenBSD: mplock.h,v 1.2 2017/12/04 09:51:03 mpi Exp $ */ /* * Copyright (c) 2004 Niklas Hallqvist. All rights reserved. @@ -46,7 +46,7 @@ void __mp_unlock(struct __mp_lock *); int __mp_release_all(struct __mp_lock *); int __mp_release_all_but_one(struct __mp_lock *); void __mp_acquire_count(struct __mp_lock *, int); -int __mp_lock_held(struct __mp_lock *); +int __mp_lock_held(struct __mp_lock *, struct cpu_info *); #endif diff --git a/sys/arch/hppa/hppa/lock_machdep.c b/sys/arch/hppa/hppa/lock_machdep.c index 605a54f3420..aeccffad61c 100644 --- a/sys/arch/hppa/hppa/lock_machdep.c +++ b/sys/arch/hppa/hppa/lock_machdep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lock_machdep.c,v 1.12 2017/07/16 22:48:38 guenther Exp $ */ +/* $OpenBSD: lock_machdep.c,v 1.13 2017/12/04 09:51:03 mpi Exp $ */ /* * Copyright (c) 2007 Artur Grabowski @@ -100,7 +100,7 @@ ___mp_lock(struct __mp_lock *mpl LOCK_FL_VARS) int s; #ifdef WITNESS - if (!__mp_lock_held(mpl)) + if (!__mp_lock_held(mpl, curcpu())) WITNESS_CHECKORDER(&mpl->mpl_lock_obj, LOP_EXCLUSIVE | LOP_NEWORDER, file, line, NULL); #endif @@ -225,7 +225,7 @@ ___mp_acquire_count(struct __mp_lock *mpl, int count LOCK_FL_VARS) } int -__mp_lock_held(struct __mp_lock *mpl) +__mp_lock_held(struct __mp_lock *mpl, struct cpu_info *ci) { - return mpl->mpl_cpu == curcpu(); + return mpl->mpl_cpu == ci; } diff --git a/sys/arch/hppa/include/mplock.h b/sys/arch/hppa/include/mplock.h index 331542434fe..db93266525a 100644 --- a/sys/arch/hppa/include/mplock.h +++ b/sys/arch/hppa/include/mplock.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mplock.h,v 1.2 2017/07/16 22:48:38 guenther Exp $ */ +/* $OpenBSD: mplock.h,v 1.3 2017/12/04 09:51:03 mpi Exp $ */ /* * Copyright (c) 2004 Niklas Hallqvist. All rights reserved. @@ -52,7 +52,7 @@ void ___mp_unlock(struct __mp_lock * LOCK_FL_VARS); int ___mp_release_all(struct __mp_lock * LOCK_FL_VARS); int ___mp_release_all_but_one(struct __mp_lock * LOCK_FL_VARS); void ___mp_acquire_count(struct __mp_lock *, int LOCK_FL_VARS); -int __mp_lock_held(struct __mp_lock *); +int __mp_lock_held(struct __mp_lock *, struct cpu_info *); #ifdef WITNESS diff --git a/sys/arch/m88k/include/mplock.h b/sys/arch/m88k/include/mplock.h index 8b9e828bd99..2a9e3ce9657 100644 --- a/sys/arch/m88k/include/mplock.h +++ b/sys/arch/m88k/include/mplock.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mplock.h,v 1.2 2011/03/23 16:54:35 pirofti Exp $ */ +/* $OpenBSD: mplock.h,v 1.3 2017/12/04 09:51:03 mpi Exp $ */ /* * Copyright (c) 2004 Niklas Hallqvist. All rights reserved. @@ -61,9 +61,9 @@ __mp_acquire_count(struct __mp_lock *mpl, int count) } static __inline__ int -__mp_lock_held(struct __mp_lock *mpl) +__mp_lock_held(struct __mp_lock *mpl, struct cpu_info *ci) { - return mpl->mpl_cpu == curcpu(); + return mpl->mpl_cpu == ci; } #endif diff --git a/sys/arch/powerpc/include/mplock.h b/sys/arch/powerpc/include/mplock.h index 29fbfbbdd14..a412a6c07a4 100644 --- a/sys/arch/powerpc/include/mplock.h +++ b/sys/arch/powerpc/include/mplock.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mplock.h,v 1.2 2015/06/26 11:22:12 dlg Exp $ */ +/* $OpenBSD: mplock.h,v 1.3 2017/12/04 09:51:03 mpi Exp $ */ /* * Copyright (c) 2004 Niklas Hallqvist. All rights reserved. @@ -45,7 +45,7 @@ void __mp_unlock(struct __mp_lock *); int __mp_release_all(struct __mp_lock *); int __mp_release_all_but_one(struct __mp_lock *); void __mp_acquire_count(struct __mp_lock *, int); -int __mp_lock_held(struct __mp_lock *); +int __mp_lock_held(struct __mp_lock *, struct cpu_info *); #endif diff --git a/sys/arch/powerpc/powerpc/lock_machdep.c b/sys/arch/powerpc/powerpc/lock_machdep.c index b78af743d4d..de6690da5a8 100644 --- a/sys/arch/powerpc/powerpc/lock_machdep.c +++ b/sys/arch/powerpc/powerpc/lock_machdep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lock_machdep.c,v 1.6 2017/05/29 14:19:50 mpi Exp $ */ +/* $OpenBSD: lock_machdep.c,v 1.7 2017/12/04 09:51:03 mpi Exp $ */ /* * Copyright (c) 2007 Artur Grabowski @@ -164,7 +164,7 @@ __mp_acquire_count(struct __mp_lock *mpl, int count) } int -__mp_lock_held(struct __mp_lock *mpl) +__mp_lock_held(struct __mp_lock *mpl, struct cpu_info *ci) { - return mpl->mpl_cpu == curcpu(); + return mpl->mpl_cpu == ci; } diff --git a/sys/kern/kern_lock.c b/sys/kern/kern_lock.c index 3b6adb16c93..f067a508bc7 100644 --- a/sys/kern/kern_lock.c +++ b/sys/kern/kern_lock.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_lock.c,v 1.51 2017/10/17 14:25:35 visa Exp $ */ +/* $OpenBSD: kern_lock.c,v 1.52 2017/12/04 09:51:03 mpi Exp $ */ /* * Copyright (c) 1995 @@ -96,7 +96,7 @@ _kernel_lock_held(void) { if (panicstr) return 1; - return (__mp_lock_held(&kernel_lock)); + return (__mp_lock_held(&kernel_lock, curcpu())); } #ifdef __USE_MI_MPLOCK @@ -168,7 +168,7 @@ ___mp_lock(struct __mp_lock *mpl LOCK_FL_VARS) unsigned long s; #ifdef WITNESS - if (!__mp_lock_held(mpl)) + if (!__mp_lock_held(mpl, curcpu())) WITNESS_CHECKORDER(&mpl->mpl_lock_obj, LOP_EXCLUSIVE | LOP_NEWORDER, file, line, NULL); #endif @@ -191,7 +191,7 @@ ___mp_unlock(struct __mp_lock *mpl LOCK_FL_VARS) unsigned long s; #ifdef MP_LOCKDEBUG - if (!__mp_lock_held(mpl)) { + if (!__mp_lock_held(mpl, curcpu())) { db_printf("__mp_unlock(%p): not held lock\n", mpl); db_enter(); } @@ -244,7 +244,7 @@ ___mp_release_all_but_one(struct __mp_lock *mpl LOCK_FL_VARS) #endif #ifdef MP_LOCKDEBUG - if (!__mp_lock_held(mpl)) { + if (!__mp_lock_held(mpl, curcpu())) { db_printf("__mp_release_all_but_one(%p): not held lock\n", mpl); db_enter(); } @@ -263,9 +263,9 @@ ___mp_acquire_count(struct __mp_lock *mpl, int count LOCK_FL_VARS) } int -__mp_lock_held(struct __mp_lock *mpl) +__mp_lock_held(struct __mp_lock *mpl, struct cpu_info *ci) { - struct __mp_lock_cpu *cpu = &mpl->mpl_cpus[cpu_number()]; + struct __mp_lock_cpu *cpu = &mpl->mpl_cpus[CPU_INFO_UNIT(ci)]; return (cpu->mplc_ticket == mpl->mpl_ticket && cpu->mplc_depth > 0); } diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c index 37ad3232b75..785636968be 100644 --- a/sys/kern/kern_proc.c +++ b/sys/kern/kern_proc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_proc.c,v 1.77 2017/09/29 09:36:04 mpi Exp $ */ +/* $OpenBSD: kern_proc.c,v 1.78 2017/12/04 09:51:03 mpi Exp $ */ /* $NetBSD: kern_proc.c,v 1.14 1996/02/09 18:59:41 christos Exp $ */ /* @@ -466,9 +466,10 @@ db_show_all_procs(db_expr_t addr, int haddr, db_expr_t count, char *modif) { char *mode; int skipzomb = 0; + int has_kernel_lock = 0; struct proc *p; struct process *pr, *ppr; - + if (modif[0] == 0) modif[0] = 'n'; /* default == normal mode */ @@ -483,7 +484,7 @@ db_show_all_procs(db_expr_t addr, int haddr, db_expr_t count, char *modif) db_printf("\t/o == show normal info for non-idle SONPROC\n"); return; } - + pr = LIST_FIRST(&allprocess); switch (*mode) { @@ -511,6 +512,12 @@ db_show_all_procs(db_expr_t addr, int haddr, db_expr_t count, char *modif) ppr = pr->ps_pptr; TAILQ_FOREACH(p, &pr->ps_threads, p_thr_link) { +#ifdef MULTIPROCESSOR + if (__mp_lock_held(&kernel_lock, p->p_cpu)) + has_kernel_lock = 1; + else + has_kernel_lock = 0; +#endif if (p->p_stat) { if (*mode == 'o') { if (p->p_stat != SONPROC) @@ -556,10 +563,11 @@ db_show_all_procs(db_expr_t addr, int haddr, db_expr_t count, char *modif) case 'o': db_printf("%5d %5d %#10x %#10x %3d" - " %-31s\n", + "%c %-31s\n", pr->ps_pid, pr->ps_ucred->cr_ruid, pr->ps_flags, p->p_flag, CPU_INFO_UNIT(p->p_cpu), + has_kernel_lock ? 'K' : ' ', pr->ps_comm); break; diff --git a/sys/sys/mplock.h b/sys/sys/mplock.h index a1a0623573e..1d64bbc904b 100644 --- a/sys/sys/mplock.h +++ b/sys/sys/mplock.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mplock.h,v 1.10 2017/10/17 14:25:35 visa Exp $ */ +/* $OpenBSD: mplock.h,v 1.11 2017/12/04 09:51:03 mpi Exp $ */ /* * Copyright (c) 2004 Niklas Hallqvist. All rights reserved. @@ -53,7 +53,7 @@ void ___mp_unlock(struct __mp_lock * LOCK_FL_VARS); int ___mp_release_all(struct __mp_lock * LOCK_FL_VARS); int ___mp_release_all_but_one(struct __mp_lock * LOCK_FL_VARS); void ___mp_acquire_count(struct __mp_lock *, int LOCK_FL_VARS); -int __mp_lock_held(struct __mp_lock *); +int __mp_lock_held(struct __mp_lock *, struct cpu_info *); #ifdef WITNESS diff --git a/sys/sys/sched.h b/sys/sys/sched.h index 0ae777f0d19..0da1f2c9afd 100644 --- a/sys/sys/sched.h +++ b/sys/sys/sched.h @@ -1,4 +1,4 @@ -/* $OpenBSD: sched.h,v 1.42 2017/02/14 10:31:15 mpi Exp $ */ +/* $OpenBSD: sched.h,v 1.43 2017/12/04 09:51:03 mpi Exp $ */ /* $NetBSD: sched.h,v 1.2 1999/02/28 18:14:58 ross Exp $ */ /*- @@ -199,9 +199,12 @@ extern struct __mp_lock sched_lock; #define SCHED_ASSERT_LOCKED() \ do { \ splassert(IPL_SCHED); \ - KASSERT(__mp_lock_held(&sched_lock)); \ + KASSERT(__mp_lock_held(&sched_lock, curcpu())); \ +} while (0) +#define SCHED_ASSERT_UNLOCKED() \ +do { \ + KASSERT(__mp_lock_held(&sched_lock, curcpu()) == 0); \ } while (0) -#define SCHED_ASSERT_UNLOCKED() KASSERT(__mp_lock_held(&sched_lock) == 0) #define SCHED_LOCK_INIT() __mp_lock_init(&sched_lock) -- 2.20.1