xref: /openbsd-src/sys/dev/pci/drm/include/linux/ww_mutex.h (revision 7350f337b9e3eb4461d99580e625c7ef148d107c)
1 /*
2  * Copyright (c) 2015 Michael Neumann <mneumann@ntecs.de>
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 
27 #ifndef _LINUX_WW_MUTEX_H_
28 #define _LINUX_WW_MUTEX_H_
29 
30 /*
31  * A basic, unoptimized implementation of wound/wait mutexes for DragonFly
32  * modelled after the Linux API [1].
33  *
34  * [1]: http://lxr.free-electrons.com/source/include/linux/ww_mutex.h
35  */
36 
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/param.h>
40 #include <sys/mutex.h>
41 #include <machine/intr.h>
42 #include <linux/compiler.h>
43 #include <linux/mutex.h>
44 
45 struct ww_class {
46 	volatile u_long			stamp;
47 	const char			*name;
48 };
49 
50 struct ww_acquire_ctx {
51 	u_long				stamp;
52 	struct ww_class			*ww_class;
53 };
54 
55 struct ww_mutex {
56 	struct mutex			base;
57 	volatile int			acquired;
58 	volatile struct ww_acquire_ctx	*ctx;
59 	volatile struct proc		*owner;
60 };
61 
62 #define DEFINE_WW_CLASS(classname)	\
63 	struct ww_class classname = {	\
64 		.stamp = 0,		\
65 		.name = #classname	\
66 	}
67 
68 #define DEFINE_WD_CLASS(classname)	\
69 	struct ww_class classname = {	\
70 		.stamp = 0,		\
71 		.name = #classname	\
72 	}
73 
74 static inline void
75 ww_acquire_init(struct ww_acquire_ctx *ctx, struct ww_class *ww_class) {
76 	ctx->stamp = __sync_fetch_and_add(&ww_class->stamp, 1);
77 	ctx->ww_class = ww_class;
78 }
79 
80 static inline void
81 ww_acquire_done(__unused struct ww_acquire_ctx *ctx) {
82 }
83 
84 static inline void
85 ww_acquire_fini(__unused struct ww_acquire_ctx *ctx) {
86 }
87 
88 static inline void
89 ww_mutex_init(struct ww_mutex *lock, struct ww_class *ww_class) {
90 	mtx_init(&lock->base, IPL_NONE);
91 	lock->acquired = 0;
92 	lock->ctx = NULL;
93 	lock->owner = NULL;
94 }
95 
96 static inline bool
97 ww_mutex_is_locked(struct ww_mutex *lock) {
98 	bool res = false;
99 	mtx_enter(&lock->base);
100 	if (lock->acquired > 0) res = true;
101 	mtx_leave(&lock->base);
102 	return res;
103 }
104 
105 /*
106  * Return 1 if lock could be acquired, else 0 (contended).
107  */
108 static inline int
109 ww_mutex_trylock(struct ww_mutex *lock) {
110 	int res = 0;
111 
112 	mtx_enter(&lock->base);
113 	/*
114 	 * In case no one holds the ww_mutex yet, we acquire it.
115 	 */
116 	if (lock->acquired == 0) {
117 		KASSERT(lock->ctx == NULL);
118 		lock->acquired = 1;
119 		lock->owner = curproc;
120 		res = 1;
121 	}
122 	mtx_leave(&lock->base);
123 	return res;
124 }
125 
126 /*
127  * When `slow` is `true`, it will always block if the ww_mutex is contended.
128  * It is assumed that the called will not hold any (ww_mutex) resources when
129  * calling the slow path as this could lead to deadlocks.
130  *
131  * When `intr` is `true`, the ssleep will be interruptable.
132  */
133 static inline int
134 __ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx, bool slow, bool intr) {
135 	int err;
136 
137 	mtx_enter(&lock->base);
138 	for (;;) {
139 		/*
140 		 * In case no one holds the ww_mutex yet, we acquire it.
141 		 */
142 		if (lock->acquired == 0) {
143 			KASSERT(lock->ctx == NULL);
144 			lock->acquired = 1;
145 			lock->ctx = ctx;
146 			lock->owner = curproc;
147 			err = 0;
148 			break;
149 		}
150 		/*
151 		 * In case we already hold the return -EALREADY.
152 		 */
153 		else if (lock->owner == curproc) {
154 			err = -EALREADY;
155 			break;
156 		}
157 		/*
158 		 * This is the contention case where the ww_mutex is
159 		 * already held by another context.
160 		 */
161 		else {
162 			/*
163 			 * Three cases:
164 			 *
165 			 * - We are in the slow-path (first lock to obtain).
166                          *
167 			 * - No context was specified. We assume a single
168 			 *   resouce, so there is no danger of a deadlock.
169                          *
170 			 * - An `older` process (`ctx`) tries to acquire a
171 			 *   lock already held by a `younger` process.
172                          *   We put the `older` process to sleep until
173                          *   the `younger` process gives up all it's
174                          *   resources.
175 			 */
176 			if (slow || ctx == NULL ||
177 			    (lock->ctx && ctx->stamp < lock->ctx->stamp)) {
178 				KASSERT(!cold);
179 				int s = msleep(lock, &lock->base,
180 					       intr ? PCATCH : 0,
181 					       ctx ? ctx->ww_class->name : "ww_mutex_lock", 0);
182 				if (intr && (s == EINTR || s == ERESTART)) {
183 					// XXX: Should we handle ERESTART?
184 					err = -EINTR;
185 					break;
186 				}
187 			}
188 			/*
189 			 * If a `younger` process tries to acquire a lock
190 			 * already held by an `older` process, we `wound` it,
191 			 * i.e. we return -EDEADLK because there is a potential
192 			 * risk for a deadlock. The `younger` process then
193 			 * should give up all it's resources and try again to
194 			 * acquire the lock in question, this time in a
195 			 * blocking manner.
196 			 */
197 			else {
198 				err = -EDEADLK;
199 				break;
200 			}
201 		}
202 
203 	} /* for */
204 	mtx_leave(&lock->base);
205 	return err;
206 }
207 
208 static inline int
209 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) {
210 	return __ww_mutex_lock(lock, ctx, false, false);
211 }
212 
213 static inline void
214 ww_mutex_lock_slow(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) {
215 	(void)__ww_mutex_lock(lock, ctx, true, false);
216 }
217 
218 static inline int
219 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) {
220 	return __ww_mutex_lock(lock, ctx, false, true);
221 }
222 
223 static inline int __must_check
224 ww_mutex_lock_slow_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) {
225 	return __ww_mutex_lock(lock, ctx, true, true);
226 }
227 
228 static inline void
229 ww_mutex_unlock(struct ww_mutex *lock) {
230 	mtx_enter(&lock->base);
231 	KASSERT(lock->owner == curproc);
232 	KASSERT(lock->acquired == 1);
233 
234 	lock->acquired = 0;
235 	lock->ctx = NULL;
236 	lock->owner = NULL;
237 	mtx_leave(&lock->base);
238 	wakeup(lock);
239 }
240 
241 static inline void
242 ww_mutex_destroy(struct ww_mutex *lock) {
243 	KASSERT(lock->acquired == 0);
244 	KASSERT(lock->ctx == NULL);
245 	KASSERT(lock->owner == NULL);
246 }
247 
248 #endif	/* _LINUX_WW_MUTEX_H_ */
249