2 * This file is part of the SPL: Solaris Porting Layer.
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/mutex.h>
29 #ifdef DEBUG_SUBSYSTEM
30 #undef DEBUG_SUBSYSTEM
33 #define DEBUG_SUBSYSTEM S_MUTEX
35 /* Mutex implementation based on those found in Solaris. This means
36 * they the MUTEX_DEFAULT type is an adaptive mutex. When calling
37 * mutex_enter() your process will spin waiting for the lock if it's
38 * likely the lock will be free'd shortly. If it looks like the
39 * lock will be held for a longer time we schedule and sleep waiting
40 * for it. This determination is made by checking if the holder of
41 * the lock is currently running on cpu or sleeping waiting to be
42 * scheduled. If the holder is currently running it's likely the
43 * lock will be shortly dropped.
45 * XXX: This is basically a rough implementation to see if this
46 * helps our performance. If it does a more careful implementation
47 * should be done, perhaps in assembly.
50 /* 0: Never spin when trying to aquire lock
51 * -1: Spin until aquired or holder yeilds without dropping lock
52 * 1-MAX_INT: Spin for N attempts before sleeping for lock
54 int mutex_spin_max
= 0;
57 int mutex_stats
[MUTEX_STATS_SIZE
] = { 0 };
58 spinlock_t mutex_stats_lock
;
59 struct list_head mutex_stats_list
;
63 __spl_mutex_init(kmutex_t
*mp
, char *name
, int type
, void *ibc
)
72 mp
->km_name_size
= strlen(name
) + 1;
76 mp
->km_type
= MUTEX_ADAPTIVE
;
86 /* We may be called when there is a non-zero preempt_count or
87 * interrupts are disabled is which case we must not sleep.
89 if (current_thread_info()->preempt_count
|| irqs_disabled())
92 /* Semaphore kmem_alloc'ed to keep struct size down (<64b) */
93 mp
->km_sem
= kmem_alloc(sizeof(struct semaphore
), flags
);
94 if (mp
->km_sem
== NULL
)
97 mp
->km_name
= kmem_alloc(mp
->km_name_size
, flags
);
98 if (mp
->km_name
== NULL
) {
99 kmem_free(mp
->km_sem
, sizeof(struct semaphore
));
103 sema_init(mp
->km_sem
, 1);
104 strncpy(mp
->km_name
, name
, mp
->km_name_size
);
107 mp
->km_stats
= kmem_zalloc(sizeof(int) * MUTEX_STATS_SIZE
, flags
);
108 if (mp
->km_stats
== NULL
) {
109 kmem_free(mp
->km_name
, mp
->km_name_size
);
110 kmem_free(mp
->km_sem
, sizeof(struct semaphore
));
114 /* XXX - This appears to be a much more contended lock than I
115 * would have expected. To run with this debugging enabled and
116 * get reasonable performance we may need to be more clever and
117 * do something like hash the mutex ptr on to one of several
118 * lists to ease this single point of contention.
120 spin_lock(&mutex_stats_lock
);
121 list_add_tail(&mp
->km_list
, &mutex_stats_list
);
122 spin_unlock(&mutex_stats_lock
);
124 mp
->km_magic
= KM_MAGIC
;
129 EXPORT_SYMBOL(__spl_mutex_init
);
132 __spl_mutex_destroy(kmutex_t
*mp
)
135 ASSERT(mp
->km_magic
== KM_MAGIC
);
138 spin_lock(&mutex_stats_lock
);
139 list_del_init(&mp
->km_list
);
140 spin_unlock(&mutex_stats_lock
);
142 kmem_free(mp
->km_stats
, sizeof(int) * MUTEX_STATS_SIZE
);
144 kmem_free(mp
->km_name
, mp
->km_name_size
);
145 kmem_free(mp
->km_sem
, sizeof(struct semaphore
));
147 memset(mp
, KM_POISON
, sizeof(*mp
));
149 EXPORT_SYMBOL(__spl_mutex_destroy
);
151 /* Return 1 if we acquired the mutex, else zero. */
153 __mutex_tryenter(kmutex_t
*mp
)
159 ASSERT(mp
->km_magic
== KM_MAGIC
);
160 MUTEX_STAT_INC(mutex_stats
, MUTEX_TRYENTER_TOTAL
);
161 MUTEX_STAT_INC(mp
->km_stats
, MUTEX_TRYENTER_TOTAL
);
163 rc
= down_trylock(mp
->km_sem
);
165 ASSERT(mp
->km_owner
== NULL
);
166 mp
->km_owner
= current
;
167 MUTEX_STAT_INC(mutex_stats
, MUTEX_TRYENTER_NOT_HELD
);
168 MUTEX_STAT_INC(mp
->km_stats
, MUTEX_TRYENTER_NOT_HELD
);
173 EXPORT_SYMBOL(__mutex_tryenter
);
175 #ifndef HAVE_TASK_CURR
176 #define task_curr(owner) 0
181 mutex_enter_adaptive(kmutex_t
*mp
)
183 struct task_struct
*owner
;
186 /* Lock is not held so we expect to aquire the lock */
187 if ((owner
= mp
->km_owner
) == NULL
) {
189 MUTEX_STAT_INC(mutex_stats
, MUTEX_ENTER_NOT_HELD
);
190 MUTEX_STAT_INC(mp
->km_stats
, MUTEX_ENTER_NOT_HELD
);
192 /* The lock is held by a currently running task which
193 * we expect will drop the lock before leaving the
194 * head of the runqueue. So the ideal thing to do
195 * is spin until we aquire the lock and avoid a
196 * context switch. However it is also possible the
197 * task holding the lock yields the processor with
198 * out dropping lock. In which case, we know it's
199 * going to be a while so we stop spinning and go
200 * to sleep waiting for the lock to be available.
201 * This should strike the optimum balance between
202 * spinning and sleeping waiting for a lock.
204 while (task_curr(owner
) && (count
<= mutex_spin_max
)) {
205 if (down_trylock(mp
->km_sem
) == 0) {
206 MUTEX_STAT_INC(mutex_stats
, MUTEX_ENTER_SPIN
);
207 MUTEX_STAT_INC(mp
->km_stats
, MUTEX_ENTER_SPIN
);
213 /* The lock is held by a sleeping task so it's going to
214 * cost us minimally one context switch. We might as
215 * well sleep and yield the processor to other tasks.
218 MUTEX_STAT_INC(mutex_stats
, MUTEX_ENTER_SLEEP
);
219 MUTEX_STAT_INC(mp
->km_stats
, MUTEX_ENTER_SLEEP
);
222 MUTEX_STAT_INC(mutex_stats
, MUTEX_ENTER_TOTAL
);
223 MUTEX_STAT_INC(mp
->km_stats
, MUTEX_ENTER_TOTAL
);
227 __mutex_enter(kmutex_t
*mp
)
231 ASSERT(mp
->km_magic
== KM_MAGIC
);
233 switch (mp
->km_type
) {
235 while (down_trylock(mp
->km_sem
));
236 MUTEX_STAT_INC(mutex_stats
, MUTEX_ENTER_SPIN
);
237 MUTEX_STAT_INC(mp
->km_stats
, MUTEX_ENTER_SPIN
);
240 mutex_enter_adaptive(mp
);
244 ASSERT(mp
->km_owner
== NULL
);
245 mp
->km_owner
= current
;
249 EXPORT_SYMBOL(__mutex_enter
);
252 __mutex_exit(kmutex_t
*mp
)
256 ASSERT(mp
->km_magic
== KM_MAGIC
);
257 ASSERT(mp
->km_owner
== current
);
262 EXPORT_SYMBOL(__mutex_exit
);
264 /* Return 1 if mutex is held by current process, else zero. */
266 __mutex_owned(kmutex_t
*mp
)
270 ASSERT(mp
->km_magic
== KM_MAGIC
);
271 RETURN(mp
->km_owner
== current
);
273 EXPORT_SYMBOL(__mutex_owned
);
275 /* Return owner if mutex is owned, else NULL. */
277 __spl_mutex_owner(kmutex_t
*mp
)
281 ASSERT(mp
->km_magic
== KM_MAGIC
);
282 RETURN(mp
->km_owner
);
284 EXPORT_SYMBOL(__spl_mutex_owner
);
291 spin_lock_init(&mutex_stats_lock
);
292 INIT_LIST_HEAD(&mutex_stats_list
);
302 ASSERT(list_empty(&mutex_stats_list
));
307 module_param(mutex_spin_max
, int, 0644);
308 MODULE_PARM_DESC(mutex_spin_max
, "Spin a maximum of N times to aquire lock");