xref: /freebsd/sys/compat/linuxkpi/common/include/linux/ww_mutex.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1 /*-
2  * Copyright (c) 2017 Mellanox Technologies, Ltd.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #ifndef	_LINUXKPI_LINUX_WW_MUTEX_H_
27 #define	_LINUXKPI_LINUX_WW_MUTEX_H_
28 
29 #include <sys/param.h>
30 #include <sys/proc.h>
31 #include <sys/condvar.h>
32 #include <sys/kernel.h>
33 
34 #include <linux/mutex.h>
35 
36 struct ww_class {
37 	const char *mutex_name;
38 };
39 
40 struct ww_acquire_ctx {
41 };
42 
43 struct ww_mutex {
44 	struct mutex base;
45 	struct cv condvar;
46 	struct ww_acquire_ctx *ctx;
47 };
48 
49 #define	DEFINE_WW_CLASS(name)					\
50 	struct ww_class name = {				\
51 		.mutex_name = mutex_name(#name "_mutex")	\
52 	}
53 
54 #define	DEFINE_WW_MUTEX(name, ww_class)					\
55 	struct ww_mutex name;						\
56 	static void name##_init(void *arg)				\
57 	{								\
58 		ww_mutex_init(&name, &ww_class);			\
59 	}								\
60 	SYSINIT(name, SI_SUB_LOCK, SI_ORDER_SECOND, name##_init, NULL)
61 
62 #define	DEFINE_WD_CLASS(name)	DEFINE_WW_CLASS(name)
63 
64 #define	ww_mutex_is_locked(_m) \
65 	sx_xlocked(&(_m)->base.sx)
66 
67 #define	ww_mutex_lock_slow(_m, _x) \
68 	ww_mutex_lock(_m, _x)
69 
70 #define	ww_mutex_lock_slow_interruptible(_m, _x) \
71 	ww_mutex_lock_interruptible(_m, _x)
72 
73 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 51600
74 static inline int __must_check
ww_mutex_trylock(struct ww_mutex * lock,struct ww_acquire_ctx * ctx __unused)75 ww_mutex_trylock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx __unused)
76 #else
77 static inline int __must_check
78 ww_mutex_trylock(struct ww_mutex *lock)
79 #endif
80 {
81 	return (mutex_trylock(&lock->base));
82 }
83 
84 extern int linux_ww_mutex_lock_sub(struct ww_mutex *,
85     struct ww_acquire_ctx *, int catch_signal);
86 
87 static inline int
ww_mutex_lock(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)88 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
89 {
90 	if (MUTEX_SKIP())
91 		return (0);
92 	else if ((struct thread *)SX_OWNER(lock->base.sx.sx_lock) == curthread)
93 		return (-EALREADY);
94 	else
95 		return (linux_ww_mutex_lock_sub(lock, ctx, 0));
96 }
97 
98 static inline int
ww_mutex_lock_interruptible(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)99 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
100 {
101 	if (MUTEX_SKIP())
102 		return (0);
103 	else if ((struct thread *)SX_OWNER(lock->base.sx.sx_lock) == curthread)
104 		return (-EALREADY);
105 	else
106 		return (linux_ww_mutex_lock_sub(lock, ctx, 1));
107 }
108 
109 extern void linux_ww_mutex_unlock_sub(struct ww_mutex *);
110 
111 static inline void
ww_mutex_unlock(struct ww_mutex * lock)112 ww_mutex_unlock(struct ww_mutex *lock)
113 {
114 	if (MUTEX_SKIP())
115 		return;
116 	else
117 		linux_ww_mutex_unlock_sub(lock);
118 }
119 
120 static inline void
ww_mutex_destroy(struct ww_mutex * lock)121 ww_mutex_destroy(struct ww_mutex *lock)
122 {
123 	cv_destroy(&lock->condvar);
124 	mutex_destroy(&lock->base);
125 }
126 
127 static inline void
ww_acquire_init(struct ww_acquire_ctx * ctx,struct ww_class * ww_class)128 ww_acquire_init(struct ww_acquire_ctx *ctx, struct ww_class *ww_class)
129 {
130 }
131 
132 static inline void
ww_mutex_init(struct ww_mutex * lock,struct ww_class * ww_class)133 ww_mutex_init(struct ww_mutex *lock, struct ww_class *ww_class)
134 {
135 	linux_mutex_init(&lock->base, ww_class->mutex_name, SX_NOWITNESS);
136 	cv_init(&lock->condvar, "lkpi-ww");
137 }
138 
139 static inline void
ww_acquire_fini(struct ww_acquire_ctx * ctx)140 ww_acquire_fini(struct ww_acquire_ctx *ctx)
141 {
142 }
143 
144 static inline void
ww_acquire_done(struct ww_acquire_ctx * ctx)145 ww_acquire_done(struct ww_acquire_ctx *ctx)
146 {
147 }
148 
149 #endif					/* _LINUXKPI_LINUX_WW_MUTEX_H_ */
150