git: bbfd52dcbeb7 - main - LinuxKPI: use LINUXKPI_DEBUG to determine on witness lock tracing

From: Bjoern A. Zeeb <bz_at_FreeBSD.org>
Date: Sat, 10 Oct 2026 18:29:04 UTC
The branch main has been updated by bz:

URL: https://cgit.FreeBSD.org/src/commit/?id=bbfd52dcbeb7b6e540220b3b24b664df6d200200

commit bbfd52dcbeb7b6e540220b3b24b664df6d200200
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2026-08-21 13:15:22 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2026-10-10 18:28:33 +0000

    LinuxKPI: use LINUXKPI_DEBUG to determine on witness lock tracing
    
    LinuxKPI by default tells witness to ignore the locks, which is
    unhelpful in case of panics like 'panic: sleeping thread holds lnxspin'.
    While I have edited the lock initialitzation manually in the past,
    make this dependent on both LINUXKPI_DEBUG and WITNESS.
    Define an internal _LKPI_MTX_NOWITNESS (and equivalent for other lock
    types used) and set them accordingly in kconfig.h, a file automatically
    included to all LinuxKPI compiled code (like global.h). (*)
    In addition, in the debug case, also define WITNESS_ALL to get file,
    line, and lock name as a lock name rather than just, e.g., lnxspin.
    
    (*) We will need another follow-up as -include ordering is currently
    wrong for LinuxKPI modules. See D60207.
    
    Sponsored by:   The FreeBSD Foundation
    MFC after:      3 days
    Reviewed by:    dumbbell
    Differential Revision: https://reviews.freebsd.org/D59231
---
 sys/compat/linuxkpi/common/include/linux/kconfig.h | 29 +++++++++++++++++++++-
 sys/compat/linuxkpi/common/include/linux/mutex.h   |  4 +--
 sys/compat/linuxkpi/common/include/linux/rwlock.h  |  2 +-
 sys/compat/linuxkpi/common/include/linux/rwsem.h   |  2 +-
 sys/compat/linuxkpi/common/include/linux/seqlock.h |  2 +-
 .../linuxkpi/common/include/linux/spinlock.h       |  8 +++---
 sys/compat/linuxkpi/common/include/linux/wait.h    |  2 +-
 .../linuxkpi/common/include/linux/ww_mutex.h       |  2 +-
 sys/compat/linuxkpi/common/src/linux_hrtimer.c     |  2 +-
 sys/compat/linuxkpi/common/src/linux_work.c        |  2 +-
 10 files changed, 42 insertions(+), 13 deletions(-)

diff --git a/sys/compat/linuxkpi/common/include/linux/kconfig.h b/sys/compat/linuxkpi/common/include/linux/kconfig.h
index 529d41991900..232bdd13656c 100644
--- a/sys/compat/linuxkpi/common/include/linux/kconfig.h
+++ b/sys/compat/linuxkpi/common/include/linux/kconfig.h
@@ -1,7 +1,7 @@
 /*-
  * SPDX-License-Identifier: BSD-2-Clause
  *
- * Copyright (c) 2020 The FreeBSD Foundation
+ * Copyright (c) 2020-2026 The FreeBSD Foundation
  *
  * This software was developed by Björn Zeeb under sponsorship from
  * the FreeBSD Foundation.
@@ -31,6 +31,33 @@
 #ifndef	_LINUXKPI_LINUX_KCONFIG_H_
 #define	_LINUXKPI_LINUX_KCONFIG_H_
 
+/* -------------------------------------------------------------------------- */
+#include "opt_linuxkpi.h"
+
+/*
+ * LinuxKPI internals are here as this file is globally included for everything
+ * compiled with LinuxKPI.
+ */
+
+/*
+ * Do not tell witness to ignore locks if debugging and witness are enabled.
+ * This helps debugging and hopefully also long-term code quality.
+ */
+#if defined(LINUXKPI_DEBUG) && defined(WITNESS)
+#define	_LKPI_MTX_NOWITNESS	(0)
+#define	_LKPI_RW_NOWITNESS	(0)
+#define	_LKPI_SX_NOWITNESS	(0)
+#define	WITNESS_ALL	/* Get better lock names. */
+#else
+#define	_LKPI_MTX_NOWITNESS	MTX_NOWITNESS
+#define	_LKPI_RW_NOWITNESS	RW_NOWITNESS
+#define	_LKPI_SX_NOWITNESS	SX_NOWITNESS
+#endif
+
+
+
+/* -------------------------------------------------------------------------- */
+
 /*
  * Checking if an option is defined would be easy if we could do CPP inside CPP.
  * The defined case whether -Dxxx or -Dxxx=1 are easy to deal with.  In either
diff --git a/sys/compat/linuxkpi/common/include/linux/mutex.h b/sys/compat/linuxkpi/common/include/linux/mutex.h
index 64d76491a76f..8008cb574552 100644
--- a/sys/compat/linuxkpi/common/include/linux/mutex.h
+++ b/sys/compat/linuxkpi/common/include/linux/mutex.h
@@ -109,10 +109,10 @@ mutex_trylock_recursive(struct mutex *lock)
 }
 
 #define	mutex_init(_m) \
-	linux_mutex_init(_m, mutex_name(#_m), SX_NOWITNESS)
+	linux_mutex_init(_m, mutex_name(#_m), _LKPI_SX_NOWITNESS)
 
 #define	__mutex_init(_m, _n, _l) \
-	linux_mutex_init(_m, _n, SX_NOWITNESS)
+	linux_mutex_init(_m, _n, _LKPI_SX_NOWITNESS)
 
 #define	mutex_init_witness(_m) \
 	linux_mutex_init(_m, mutex_name(#_m), SX_DUPOK)
diff --git a/sys/compat/linuxkpi/common/include/linux/rwlock.h b/sys/compat/linuxkpi/common/include/linux/rwlock.h
index 3030ec89ff1e..1a75831d3750 100644
--- a/sys/compat/linuxkpi/common/include/linux/rwlock.h
+++ b/sys/compat/linuxkpi/common/include/linux/rwlock.h
@@ -52,6 +52,6 @@ typedef struct rwlock rwlock_t;
     do { read_unlock(lock); } while (0)
 #define	write_unlock_irqrestore(lock, flags)				\
     do { write_unlock(lock); } while (0)
-#define	rwlock_init(_l)	rw_init_flags(_l, "lnxrw", RW_NOWITNESS | RW_NEW)
+#define	rwlock_init(_l)	rw_init_flags(_l, "lnxrw", _LKPI_RW_NOWITNESS | RW_NEW)
 
 #endif	/* _LINUXKPI_LINUX_RWLOCK_H_ */
diff --git a/sys/compat/linuxkpi/common/include/linux/rwsem.h b/sys/compat/linuxkpi/common/include/linux/rwsem.h
index 43688ab93796..2fed94f0a81a 100644
--- a/sys/compat/linuxkpi/common/include/linux/rwsem.h
+++ b/sys/compat/linuxkpi/common/include/linux/rwsem.h
@@ -77,7 +77,7 @@ linux_init_rwsem(struct rw_semaphore *rw, const char *name)
 {
 
 	memset(rw, 0, sizeof(*rw));
-	sx_init_flags(&rw->sx, name, SX_NOWITNESS);
+	sx_init_flags(&rw->sx, name, _LKPI_SX_NOWITNESS);
 }
 
 extern int linux_down_read_killable(struct rw_semaphore *);
diff --git a/sys/compat/linuxkpi/common/include/linux/seqlock.h b/sys/compat/linuxkpi/common/include/linux/seqlock.h
index 554fdfd6e202..be2fe5f3daed 100644
--- a/sys/compat/linuxkpi/common/include/linux/seqlock.h
+++ b/sys/compat/linuxkpi/common/include/linux/seqlock.h
@@ -119,7 +119,7 @@ seqlock_init(struct seqlock *seqlock)
 	 * seqlock has been freed. There is no seqlock destructor exists so we
 	 * can't expect automatic mtx_destroy() execution before free().
 	 */
-	mtx_init(&seqlock->seql_lock, "seqlock", NULL, MTX_DEF|MTX_NOWITNESS);
+	mtx_init(&seqlock->seql_lock, "seqlock", NULL, MTX_DEF | _LKPI_MTX_NOWITNESS);
 	seqcount_init(&seqlock->seql_count);
 }
 
diff --git a/sys/compat/linuxkpi/common/include/linux/spinlock.h b/sys/compat/linuxkpi/common/include/linux/spinlock.h
index a786cbab5e13..258322ea8ce7 100644
--- a/sys/compat/linuxkpi/common/include/linux/spinlock.h
+++ b/sys/compat/linuxkpi/common/include/linux/spinlock.h
@@ -29,6 +29,8 @@
 #ifndef	_LINUXKPI_LINUX_SPINLOCK_H_
 #define	_LINUXKPI_LINUX_SPINLOCK_H_
 
+#include "opt_linuxkpi.h"
+
 #include <asm/atomic.h>
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -130,7 +132,7 @@ typedef struct mtx spinlock_t;
 	spin_unlock(_l);				\
 } while (0)
 
-#ifdef WITNESS_ALL
+#if defined(LINUXKPI_DEBUG) && defined(WITNESS)
 /* NOTE: the maximum WITNESS name is 64 chars */
 #define	__spin_lock_name(name, file, line)		\
 	(((const char *){file ":" #line "-" name}) +	\
@@ -142,7 +144,7 @@ typedef struct mtx spinlock_t;
 #define	spin_lock_name(name)		_spin_lock_name(name, __FILE__, __LINE__)
 
 #define	spin_lock_init(lock)	mtx_init(lock, spin_lock_name("lnxspin"), \
-				  NULL, MTX_DEF | MTX_NOWITNESS | MTX_NEW)
+				  NULL, MTX_DEF | _LKPI_MTX_NOWITNESS | MTX_NEW)
 
 #define	spin_lock_destroy(_l)	mtx_destroy(_l)
 
@@ -190,7 +192,7 @@ typedef struct raw_spinlock {
 
 #define	raw_spin_lock_init(rlock) \
 	mtx_init(&(rlock)->lock, spin_lock_name("lnxspin_raw"), \
-	    NULL, MTX_DEF | MTX_NOWITNESS | MTX_NEW)
+	    NULL, MTX_DEF | _LKPI_MTX_NOWITNESS | MTX_NEW)
 
 #define	raw_spin_lock(rl)	spin_lock(&(rl)->lock)
 #define	raw_spin_trylock(rl)	spin_trylock(&(rl)->lock)
diff --git a/sys/compat/linuxkpi/common/include/linux/wait.h b/sys/compat/linuxkpi/common/include/linux/wait.h
index 698a1056b713..5d98ccfa7af8 100644
--- a/sys/compat/linuxkpi/common/include/linux/wait.h
+++ b/sys/compat/linuxkpi/common/include/linux/wait.h
@@ -115,7 +115,7 @@ long linux_wait_woken(wait_queue_t *wq, unsigned state, long timeout);
 
 #define	init_waitqueue_head(wqh) do {					\
 	mtx_init(&(wqh)->lock, spin_lock_name("wqhead"),		\
-	    NULL, MTX_DEF | MTX_NEW | MTX_NOWITNESS);			\
+	    NULL, MTX_DEF | MTX_NEW | _LKPI_MTX_NOWITNESS);		\
 	INIT_LIST_HEAD(&(wqh)->task_list);				\
 } while (0)
 
diff --git a/sys/compat/linuxkpi/common/include/linux/ww_mutex.h b/sys/compat/linuxkpi/common/include/linux/ww_mutex.h
index 9219755bb78e..eff838ab214a 100644
--- a/sys/compat/linuxkpi/common/include/linux/ww_mutex.h
+++ b/sys/compat/linuxkpi/common/include/linux/ww_mutex.h
@@ -132,7 +132,7 @@ ww_acquire_init(struct ww_acquire_ctx *ctx, struct ww_class *ww_class)
 static inline void
 ww_mutex_init(struct ww_mutex *lock, struct ww_class *ww_class)
 {
-	linux_mutex_init(&lock->base, ww_class->mutex_name, SX_NOWITNESS);
+	linux_mutex_init(&lock->base, ww_class->mutex_name, _LKPI_SX_NOWITNESS);
 	cv_init(&lock->condvar, "lkpi-ww");
 }
 
diff --git a/sys/compat/linuxkpi/common/src/linux_hrtimer.c b/sys/compat/linuxkpi/common/src/linux_hrtimer.c
index bbd99dec2d83..58c55fed5d1a 100644
--- a/sys/compat/linuxkpi/common/src/linux_hrtimer.c
+++ b/sys/compat/linuxkpi/common/src/linux_hrtimer.c
@@ -101,7 +101,7 @@ linux_hrtimer_init(struct hrtimer *hrtimer)
 
 	memset(hrtimer, 0, sizeof(*hrtimer));
 	mtx_init(&hrtimer->mtx, "hrtimer", NULL,
-	    MTX_DEF | MTX_RECURSE | MTX_NOWITNESS);
+	    MTX_DEF | MTX_RECURSE | _LKPI_MTX_NOWITNESS);
 	callout_init_mtx(&hrtimer->callout, &hrtimer->mtx, 0);
 }
 
diff --git a/sys/compat/linuxkpi/common/src/linux_work.c b/sys/compat/linuxkpi/common/src/linux_work.c
index bf4c82767621..ef2891b1f249 100644
--- a/sys/compat/linuxkpi/common/src/linux_work.c
+++ b/sys/compat/linuxkpi/common/src/linux_work.c
@@ -706,7 +706,7 @@ linux_init_delayed_work(struct delayed_work *dwork, work_func_t func)
 	dwork->work.func = func;
 	TASK_INIT(&dwork->work.work_task, 0, linux_delayed_work_fn, dwork);
 	mtx_init(&dwork->timer.mtx, spin_lock_name("lkpi-dwork"), NULL,
-	    MTX_DEF | MTX_NOWITNESS);
+	    MTX_DEF | _LKPI_MTX_NOWITNESS);
 	callout_init_mtx(&dwork->timer.callout, &dwork->timer.mtx, 0);
 }