From 02ea6033020e11afec6472bf560b0ddebd0fa97a Mon Sep 17 00:00:00 2001 From: Vladimir Kondratyev Date: Tue, 18 Jan 2022 23:14:12 +0300 Subject: LinuxKPI: Allow spin_lock_irqsave to be called within a critical section with spinning on spin_trylock. dma-buf part of drm-kmod depends on this property and absence of it support results in "mi_switch: switch in a critical section" assertions [1][2]. [1] https://github.com/freebsd/drm-kmod/issues/116 [2] https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=261166 MFC after: 1 week Reviewed by: manu Differential Revision: https://reviews.freebsd.org/D33887 --- .../linuxkpi/common/include/linux/spinlock.h | 27 ++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'sys/compat/linuxkpi/common/include/linux') diff --git a/sys/compat/linuxkpi/common/include/linux/spinlock.h b/sys/compat/linuxkpi/common/include/linux/spinlock.h index a87cb7180b28..31d47fa73986 100644 --- a/sys/compat/linuxkpi/common/include/linux/spinlock.h +++ b/sys/compat/linuxkpi/common/include/linux/spinlock.h @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -117,14 +118,32 @@ typedef struct { local_bh_disable(); \ } while (0) -#define spin_lock_irqsave(_l, flags) do { \ - (flags) = 0; \ - spin_lock(_l); \ +#define __spin_trylock_nested(_l, _n) ({ \ + int __ret; \ + if (SPIN_SKIP()) { \ + __ret = 1; \ + } else { \ + __ret = mtx_trylock_flags(&(_l)->m, MTX_DUPOK); \ + if (likely(__ret != 0)) \ + local_bh_disable(); \ + } \ + __ret; \ +}) + +#define spin_lock_irqsave(_l, flags) do { \ + (flags) = 0; \ + if (unlikely(curthread->td_critnest != 0)) \ + while (!spin_trylock(_l)) {} \ + else \ + spin_lock(_l); \ } while (0) #define spin_lock_irqsave_nested(_l, flags, _n) do { \ (flags) = 0; \ - spin_lock_nested(_l, _n); \ + if (unlikely(curthread->td_critnest != 0)) \ + while (!__spin_trylock_nested(_l, _n)) {} \ + else \ + spin_lock_nested(_l, _n); \ } while (0) #define spin_unlock_irqrestore(_l, flags) do { \ -- cgit v1.2.3