I'd like to easily see which CPU is holding the KERNEL_LOCK() in
ddb(4)'s "ps /o" output. This is really handy when debugging deadlocks.
So the diff below adds a 'struct cpu_info *' argument to __mp_lock_held()
and use it in the corresponding DDB command.
Note that this requires the previous ci_cpuid diff for sparc64 I just
sent.
ok?
Index: arch/alpha/alpha/lock_machdep.c
===================================================================
RCS file: /cvs/src/sys/arch/alpha/alpha/lock_machdep.c,v
retrieving revision 1.6
diff -u -p -r1.6 lock_machdep.c
--- arch/alpha/alpha/lock_machdep.c 29 May 2017 14:19:49 -0000 1.6
+++ arch/alpha/alpha/lock_machdep.c 1 Dec 2017 14:11:39 -0000
@@ -186,7 +186,7 @@ __mp_acquire_count(struct __mp_lock *mpl
}
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);
}
Index: arch/alpha/include/mplock.h
===================================================================
RCS file: /cvs/src/sys/arch/alpha/include/mplock.h,v
retrieving revision 1.1
diff -u -p -r1.1 mplock.h
--- arch/alpha/include/mplock.h 26 Jan 2014 17:40:11 -0000 1.1
+++ arch/alpha/include/mplock.h 1 Dec 2017 14:16:16 -0000
@@ -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
Index: arch/amd64/amd64/ipifuncs.c
===================================================================
RCS file: /cvs/src/sys/arch/amd64/amd64/ipifuncs.c,v
retrieving revision 1.28
diff -u -p -r1.28 ipifuncs.c
--- arch/amd64/amd64/ipifuncs.c 23 Nov 2015 22:57:12 -0000 1.28
+++ arch/amd64/amd64/ipifuncs.c 1 Dec 2017 14:28:44 -0000
@@ -113,8 +113,8 @@ void
x86_64_ipi_halt(struct cpu_info *ci)
{
SCHED_ASSERT_UNLOCKED();
- KASSERT(!__mp_lock_held(&kernel_lock));
-
+ KASSERT(!_kernel_lock_held());
+
fpusave_cpu(ci, 1);
disable_intr();
lapic_disable();
Index: arch/arm64/include/mplock.h
===================================================================
RCS file: /cvs/src/sys/arch/arm64/include/mplock.h,v
retrieving revision 1.1
diff -u -p -r1.1 mplock.h
--- arch/arm64/include/mplock.h 17 Dec 2016 23:38:33 -0000 1.1
+++ arch/arm64/include/mplock.h 1 Dec 2017 14:16:19 -0000
@@ -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
Index: arch/hppa/hppa/lock_machdep.c
===================================================================
RCS file: /cvs/src/sys/arch/hppa/hppa/lock_machdep.c,v
retrieving revision 1.12
diff -u -p -r1.12 lock_machdep.c
--- arch/hppa/hppa/lock_machdep.c 16 Jul 2017 22:48:38 -0000 1.12
+++ arch/hppa/hppa/lock_machdep.c 1 Dec 2017 14:11:31 -0000
@@ -225,7 +225,7 @@ ___mp_acquire_count(struct __mp_lock *mp
}
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;
}
Index: arch/hppa/include/mplock.h
===================================================================
RCS file: /cvs/src/sys/arch/hppa/include/mplock.h,v
retrieving revision 1.2
diff -u -p -r1.2 mplock.h
--- arch/hppa/include/mplock.h 16 Jul 2017 22:48:38 -0000 1.2
+++ arch/hppa/include/mplock.h 1 Dec 2017 14:16:25 -0000
@@ -52,7 +52,7 @@ void ___mp_unlock(struct __mp_lock * LOC
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
Index: arch/m88k/include/mplock.h
===================================================================
RCS file: /cvs/src/sys/arch/m88k/include/mplock.h,v
retrieving revision 1.2
diff -u -p -r1.2 mplock.h
--- arch/m88k/include/mplock.h 23 Mar 2011 16:54:35 -0000 1.2
+++ arch/m88k/include/mplock.h 1 Dec 2017 14:11:53 -0000
@@ -61,9 +61,9 @@ __mp_acquire_count(struct __mp_lock *mpl
}
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
Index: arch/powerpc/include/mplock.h
===================================================================
RCS file: /cvs/src/sys/arch/powerpc/include/mplock.h,v
retrieving revision 1.2
diff -u -p -r1.2 mplock.h
--- arch/powerpc/include/mplock.h 26 Jun 2015 11:22:12 -0000 1.2
+++ arch/powerpc/include/mplock.h 1 Dec 2017 14:16:34 -0000
@@ -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
Index: arch/powerpc/powerpc/lock_machdep.c
===================================================================
RCS file: /cvs/src/sys/arch/powerpc/powerpc/lock_machdep.c,v
retrieving revision 1.6
diff -u -p -r1.6 lock_machdep.c
--- arch/powerpc/powerpc/lock_machdep.c 29 May 2017 14:19:50 -0000 1.6
+++ arch/powerpc/powerpc/lock_machdep.c 1 Dec 2017 14:12:04 -0000
@@ -164,7 +164,7 @@ __mp_acquire_count(struct __mp_lock *mpl
}
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;
}
Index: kern/kern_lock.c
===================================================================
RCS file: /cvs/src/sys/kern/kern_lock.c,v
retrieving revision 1.51
diff -u -p -r1.51 kern_lock.c
--- kern/kern_lock.c 17 Oct 2017 14:25:35 -0000 1.51
+++ kern/kern_lock.c 1 Dec 2017 14:15:40 -0000
@@ -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
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_
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_lo
#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 *mp
}
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[ci->ci_cpuid];
return (cpu->mplc_ticket == mpl->mpl_ticket && cpu->mplc_depth > 0);
}
Index: kern/kern_proc.c
===================================================================
RCS file: /cvs/src/sys/kern/kern_proc.c,v
retrieving revision 1.77
diff -u -p -r1.77 kern_proc.c
--- kern/kern_proc.c 29 Sep 2017 09:36:04 -0000 1.77
+++ kern/kern_proc.c 1 Dec 2017 14:32:54 -0000
@@ -466,9 +466,10 @@ db_show_all_procs(db_expr_t addr, int ha
{
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 ha
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 ha
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 ha
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;
Index: kern/kern_synch.c
===================================================================
RCS file: /cvs/src/sys/kern/kern_synch.c,v
retrieving revision 1.141
diff -u -p -r1.141 kern_synch.c
--- kern/kern_synch.c 18 May 2017 07:08:45 -0000 1.141
+++ kern/kern_synch.c 1 Dec 2017 14:19:37 -0000
@@ -115,7 +115,7 @@ tsleep(const volatile void *ident, int p
KASSERT((priority & ~(PRIMASK | PCATCH)) == 0);
#ifdef MULTIPROCESSOR
- KASSERT(timo || __mp_lock_held(&kernel_lock));
+ KASSERT(timo || _kernel_lock_held());
#endif
#ifdef DDB
@@ -133,7 +133,7 @@ tsleep(const volatile void *ident, int p
s = splhigh();
splx(safepri);
#ifdef MULTIPROCESSOR
- if (__mp_lock_held(&kernel_lock)) {
+ if (_kernel_lock_held()) {
hold_count = __mp_release_all(&kernel_lock);
__mp_acquire_count(&kernel_lock, hold_count);
}
@@ -186,7 +186,7 @@ msleep(const volatile void *ident, struc
MUTEX_OLDIPL(mtx) = safepri;
mtx_leave(mtx);
#ifdef MULTIPROCESSOR
- if (__mp_lock_held(&kernel_lock)) {
+ if (_kernel_lock_held()) {
hold_count = __mp_release_all(&kernel_lock);
__mp_acquire_count(&kernel_lock, hold_count);
}
Index: kern/sched_bsd.c
===================================================================
RCS file: /cvs/src/sys/kern/sched_bsd.c,v
retrieving revision 1.46
diff -u -p -r1.46 sched_bsd.c
--- kern/sched_bsd.c 14 Feb 2017 10:31:15 -0000 1.46
+++ kern/sched_bsd.c 1 Dec 2017 14:20:08 -0000
@@ -354,7 +354,7 @@ mi_switch(void)
* Release the kernel_lock, as we are about to yield the CPU.
*/
sched_count = __mp_release_all_but_one(&sched_lock);
- if (__mp_lock_held(&kernel_lock))
+ if (_kernel_lock_held())
hold_count = __mp_release_all(&kernel_lock);
else
hold_count = 0;
Index: kern/vfs_subr.c
===================================================================
RCS file: /cvs/src/sys/kern/vfs_subr.c,v
retrieving revision 1.260
diff -u -p -r1.260 vfs_subr.c
--- kern/vfs_subr.c 31 Jul 2017 16:47:03 -0000 1.260
+++ kern/vfs_subr.c 1 Dec 2017 14:20:45 -0000
@@ -1688,7 +1688,7 @@ vfs_syncwait(int verbose)
if (verbose)
printf("%d ", nbusy);
#ifdef MULTIPROCESSOR
- if (__mp_lock_held(&kernel_lock))
+ if (_kernel_lock_held())
hold_count = __mp_release_all(&kernel_lock);
else
hold_count = 0;
Index: sys/mplock.h
===================================================================
RCS file: /cvs/src/sys/sys/mplock.h,v
retrieving revision 1.10
diff -u -p -r1.10 mplock.h
--- sys/mplock.h 17 Oct 2017 14:25:35 -0000 1.10
+++ sys/mplock.h 1 Dec 2017 14:16:41 -0000
@@ -53,7 +53,7 @@ void ___mp_unlock(struct __mp_lock * LOC
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
Index: sys/sched.h
===================================================================
RCS file: /cvs/src/sys/sys/sched.h,v
retrieving revision 1.42
diff -u -p -r1.42 sched.h
--- sys/sched.h 14 Feb 2017 10:31:15 -0000 1.42
+++ sys/sched.h 1 Dec 2017 14:18:00 -0000
@@ -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)