From 59fb4a95c7f291f0cbbda1d56d3c7075410fca31 Mon Sep 17 00:00:00 2001 From: Ryan Libby Date: Wed, 27 Nov 2019 01:54:39 +0000 Subject: [PATCH] witness: sleepable rm locks are not sleepable in read mode There are two classes of rm lock, one "sleepable" and one not. But even a "sleepable" rm lock is only sleepable in write mode, and is non-sleepable when taken in read mode. Warn about sleepable rm locks in read mode as non-sleepable locks. Do this by defining a new lock operation flag, LOP_NOSLEEP, to indicate that a lock is non-sleepable despite what the LO_SLEEPABLE flag would indicate, and defining a new witness lock instance flag, LI_SLEEPABLE, to track the product of LO_SLEEPABLE and LOP_NOSLEEP on the lock instance. Reviewed by: markj Sponsored by: Dell EMC Isilon Differential Revision: https://reviews.freebsd.org/D22527 --- sys/kern/kern_rmlock.c | 6 +++--- sys/kern/subr_witness.c | 30 ++++++++++++++++++------------ sys/sys/lock.h | 1 + 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/sys/kern/kern_rmlock.c b/sys/kern/kern_rmlock.c index 5cb18fbfa582..96e11276e9b2 100644 --- a/sys/kern/kern_rmlock.c +++ b/sys/kern/kern_rmlock.c @@ -652,8 +652,8 @@ _rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker, KASSERT(!rm_wowned(rm), ("rm_rlock: wlock already held for %s @ %s:%d", rm->lock_object.lo_name, file, line)); - WITNESS_CHECKORDER(&rm->lock_object, LOP_NEWORDER, file, line, - NULL); + WITNESS_CHECKORDER(&rm->lock_object, + LOP_NEWORDER | LOP_NOSLEEP, file, line, NULL); } if (_rm_rlock(rm, tracker, trylock)) { @@ -663,7 +663,7 @@ _rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker, else LOCK_LOG_LOCK("RMRLOCK", &rm->lock_object, 0, 0, file, line); - WITNESS_LOCK(&rm->lock_object, 0, file, line); + WITNESS_LOCK(&rm->lock_object, LOP_NOSLEEP, file, line); TD_LOCKS_INC(curthread); return (1); } else if (trylock) diff --git a/sys/kern/subr_witness.c b/sys/kern/subr_witness.c index 1267a42410d5..39c6de81bb81 100644 --- a/sys/kern/subr_witness.c +++ b/sys/kern/subr_witness.c @@ -131,6 +131,7 @@ __FBSDID("$FreeBSD$"); #define LI_RECURSEMASK 0x0000ffff /* Recursion depth of lock instance. */ #define LI_EXCLUSIVE 0x00010000 /* Exclusive lock instance. */ #define LI_NORELEASE 0x00020000 /* Lock not allowed to be released. */ +#define LI_SLEEPABLE 0x00040000 /* Lock may be held while sleeping. */ #ifndef WITNESS_COUNT #define WITNESS_COUNT 1536 @@ -1302,7 +1303,7 @@ witness_checkorder(struct lock_object *lock, int flags, const char *file, * If we are locking Giant and this is a sleepable * lock, then skip it. */ - if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && + if ((lock1->li_flags & LI_SLEEPABLE) != 0 && lock == &Giant.lock_object) continue; @@ -1311,6 +1312,7 @@ witness_checkorder(struct lock_object *lock, int flags, const char *file, * is Giant, then skip it. */ if ((lock->lo_flags & LO_SLEEPABLE) != 0 && + (flags & LOP_NOSLEEP) == 0 && lock1->li_lock == &Giant.lock_object) continue; @@ -1320,15 +1322,16 @@ witness_checkorder(struct lock_object *lock, int flags, const char *file, * order violation to enfore a general lock order of * sleepable locks before non-sleepable locks. */ - if (((lock->lo_flags & LO_SLEEPABLE) != 0 && - (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) + if ((lock->lo_flags & LO_SLEEPABLE) != 0 && + (flags & LOP_NOSLEEP) == 0 && + (lock1->li_flags & LI_SLEEPABLE) == 0) goto reversal; /* * If we are locking Giant and this is a non-sleepable * lock, then treat it as a reversal. */ - if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 && + if ((lock1->li_flags & LI_SLEEPABLE) == 0 && lock == &Giant.lock_object) goto reversal; @@ -1378,11 +1381,12 @@ witness_checkorder(struct lock_object *lock, int flags, const char *file, /* * Ok, yell about it. */ - if (((lock->lo_flags & LO_SLEEPABLE) != 0 && - (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) + if ((lock->lo_flags & LO_SLEEPABLE) != 0 && + (flags & LOP_NOSLEEP) == 0 && + (lock1->li_flags & LI_SLEEPABLE) == 0) witness_output( "lock order reversal: (sleepable after non-sleepable)\n"); - else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 + else if ((lock1->li_flags & LI_SLEEPABLE) == 0 && lock == &Giant.lock_object) witness_output( "lock order reversal: (Giant after non-sleepable)\n"); @@ -1440,7 +1444,8 @@ witness_checkorder(struct lock_object *lock, int flags, const char *file, */ if (flags & LOP_NEWORDER && !(plock->li_lock == &Giant.lock_object && - (lock->lo_flags & LO_SLEEPABLE) != 0)) { + (lock->lo_flags & LO_SLEEPABLE) != 0 && + (flags & LOP_NOSLEEP) == 0)) { CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, w->w_name, plock->li_lock->lo_witness->w_name); itismychild(plock->li_lock->lo_witness, w); @@ -1500,10 +1505,11 @@ witness_lock(struct lock_object *lock, int flags, const char *file, int line) instance->li_lock = lock; instance->li_line = line; instance->li_file = file; + instance->li_flags = 0; if ((flags & LOP_EXCLUSIVE) != 0) - instance->li_flags = LI_EXCLUSIVE; - else - instance->li_flags = 0; + instance->li_flags |= LI_EXCLUSIVE; + if ((lock->lo_flags & LO_SLEEPABLE) != 0 && (flags & LOP_NOSLEEP) == 0) + instance->li_flags |= LI_SLEEPABLE; CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); } @@ -1763,7 +1769,7 @@ witness_warn(int flags, struct lock_object *lock, const char *fmt, ...) lock1->li_lock == &Giant.lock_object) continue; if (flags & WARN_SLEEPOK && - (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) + (lock1->li_flags & LI_SLEEPABLE) != 0) continue; if (n == 0) { va_start(ap, fmt); diff --git a/sys/sys/lock.h b/sys/sys/lock.h index db2e697e563d..f8bf53014ff6 100644 --- a/sys/sys/lock.h +++ b/sys/sys/lock.h @@ -107,6 +107,7 @@ struct lock_class { #define LOP_TRYLOCK 0x00000004 /* Don't check lock order. */ #define LOP_EXCLUSIVE 0x00000008 /* Exclusive lock. */ #define LOP_DUPOK 0x00000010 /* Don't check for duplicate acquires */ +#define LOP_NOSLEEP 0x00000020 /* Non-sleepable despite LO_SLEEPABLE */ /* Flags passed to witness_assert. */ #define LA_MASKASSERT 0x000000ff /* Mask for witness defined asserts. */