2 * Copyright (c) 2017 Mellanox Technologies, Ltd.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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.
29 #include <sys/queue.h>
31 #include <linux/sched.h>
32 #include <linux/ww_mutex.h>
34 struct ww_mutex_thread {
35 TAILQ_ENTRY(ww_mutex_thread) entry;
36 struct thread *thread;
37 struct ww_mutex *lock;
40 static TAILQ_HEAD(, ww_mutex_thread) ww_mutex_head;
41 static struct mtx ww_mutex_global;
44 linux_ww_init(void *arg)
46 TAILQ_INIT(&ww_mutex_head);
47 mtx_init(&ww_mutex_global, "lkpi-ww-mtx", NULL, MTX_DEF);
50 SYSINIT(ww_init, SI_SUB_LOCK, SI_ORDER_SECOND, linux_ww_init, NULL);
53 linux_ww_uninit(void *arg)
55 mtx_destroy(&ww_mutex_global);
58 SYSUNINIT(ww_uninit, SI_SUB_LOCK, SI_ORDER_SECOND, linux_ww_uninit, NULL);
63 mtx_lock(&ww_mutex_global);
69 mtx_unlock(&ww_mutex_global);
72 /* lock a mutex with deadlock avoidance */
74 linux_ww_mutex_lock_sub(struct ww_mutex *lock, int catch_signal)
76 struct task_struct *task;
77 struct ww_mutex_thread entry;
78 struct ww_mutex_thread *other;
84 if (unlikely(sx_try_xlock(&lock->base.sx) == 0)) {
85 entry.thread = curthread;
87 TAILQ_INSERT_TAIL(&ww_mutex_head, &entry, entry);
90 struct thread *owner = (struct thread *)
91 SX_OWNER(lock->base.sx.sx_lock);
93 /* scan for deadlock */
94 TAILQ_FOREACH(other, &ww_mutex_head, entry) {
99 * If another thread is owning our
100 * lock and is at the same time trying
101 * to acquire a lock this thread owns,
102 * that means deadlock.
104 if (other->thread == owner &&
105 (struct thread *)SX_OWNER(
106 other->lock->base.sx.sx_lock) == curthread) {
112 retval = -cv_wait_sig(&lock->condvar, &ww_mutex_global);
114 linux_schedule_save_interrupt_value(task, retval);
119 cv_wait(&lock->condvar, &ww_mutex_global);
121 } while (sx_try_xlock(&lock->base.sx) == 0);
123 TAILQ_REMOVE(&ww_mutex_head, &entry, entry);
125 /* if the lock is free, wakeup next lock waiter, if any */
126 if ((struct thread *)SX_OWNER(lock->base.sx.sx_lock) == NULL)
127 cv_signal(&lock->condvar);
134 linux_ww_mutex_unlock_sub(struct ww_mutex *lock)
136 /* protect ww_mutex ownership change */
138 sx_xunlock(&lock->base.sx);
139 /* wakeup a lock waiter, if any */
140 cv_signal(&lock->condvar);
145 linux_mutex_lock_interruptible(mutex_t *m)
149 error = -sx_xlock_sig(&m->sx);
151 linux_schedule_save_interrupt_value(current, error);
158 linux_down_write_killable(struct rw_semaphore *rw)
162 error = -sx_xlock_sig(&rw->sx);
164 linux_schedule_save_interrupt_value(current, error);