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
)
70 ASSERT(mp
->km_magic
!= KM_MAGIC
); /* Never double init */
72 mp
->km_magic
= KM_MAGIC
;
75 mp
->km_name_size
= strlen(name
) + 1;
79 mp
->km_type
= MUTEX_ADAPTIVE
;
89 /* We may be called when there is a non-zero preempt_count or
90 * interrupts are disabled is which case we must not sleep.
92 if (current_thread_info()->preempt_count
|| irqs_disabled())
95 /* Semaphore kmem_alloc'ed to keep struct size down (<64b) */
96 mp
->km_sem
= kmem_alloc(sizeof(struct semaphore
), flags
);
97 if (mp
->km_sem
== NULL
)
100 mp
->km_name
= kmem_alloc(mp
->km_name_size
, flags
);
101 if (mp
->km_name
== NULL
) {
102 kmem_free(mp
->km_sem
, sizeof(struct semaphore
));
106 sema_init(mp
->km_sem
, 1);
107 strncpy(mp
->km_name
, name
, mp
->km_name_size
);
110 mp
->km_stats
= kmem_zalloc(sizeof(int) * MUTEX_STATS_SIZE
, flags
);
111 if (mp
->km_stats
== NULL
) {
112 kmem_free(mp
->km_name
, mp
->km_name_size
);
113 kmem_free(mp
->km_sem
, sizeof(struct semaphore
));
117 /* XXX - This appears to be a much more contended lock than I
118 * would have expected. To run with this debugging enabled and
119 * get reasonable performance we may need to be more clever and
120 * do something like hash the mutex ptr on to one of several
121 * lists to ease this single point of contention.
123 spin_lock(&mutex_stats_lock
);
124 list_add_tail(&mp
->km_list
, &mutex_stats_list
);
125 spin_unlock(&mutex_stats_lock
);
128 EXPORT_SYMBOL(__spl_mutex_init
);
131 __spl_mutex_destroy(kmutex_t
*mp
)
134 ASSERT(mp
->km_magic
== KM_MAGIC
);
137 spin_lock(&mutex_stats_lock
);
138 list_del_init(&mp
->km_list
);
139 spin_unlock(&mutex_stats_lock
);
141 kmem_free(mp
->km_stats
, sizeof(int) * MUTEX_STATS_SIZE
);
143 kmem_free(mp
->km_name
, mp
->km_name_size
);
144 kmem_free(mp
->km_sem
, sizeof(struct semaphore
));
146 memset(mp
, KM_POISON
, sizeof(*mp
));
148 EXPORT_SYMBOL(__spl_mutex_destroy
);
150 /* Return 1 if we acquired the mutex, else zero. */
152 __mutex_tryenter(kmutex_t
*mp
)
158 ASSERT(mp
->km_magic
== KM_MAGIC
);
159 MUTEX_STAT_INC(mutex_stats
, MUTEX_TRYENTER_TOTAL
);
160 MUTEX_STAT_INC(mp
->km_stats
, MUTEX_TRYENTER_TOTAL
);
162 rc
= down_trylock(mp
->km_sem
);
164 ASSERT(mp
->km_owner
== NULL
);
165 mp
->km_owner
= current
;
166 MUTEX_STAT_INC(mutex_stats
, MUTEX_TRYENTER_NOT_HELD
);
167 MUTEX_STAT_INC(mp
->km_stats
, MUTEX_TRYENTER_NOT_HELD
);
172 EXPORT_SYMBOL(__mutex_tryenter
);
175 mutex_enter_adaptive(kmutex_t
*mp
)
177 struct task_struct
*owner
;
180 /* Lock is not held so we expect to aquire the lock */
181 if ((owner
= mp
->km_owner
) == NULL
) {
183 MUTEX_STAT_INC(mutex_stats
, MUTEX_ENTER_NOT_HELD
);
184 MUTEX_STAT_INC(mp
->km_stats
, MUTEX_ENTER_NOT_HELD
);
186 /* The lock is held by a currently running task which
187 * we expect will drop the lock before leaving the
188 * head of the runqueue. So the ideal thing to do
189 * is spin until we aquire the lock and avoid a
190 * context switch. However it is also possible the
191 * task holding the lock yields the processor with
192 * out dropping lock. In which case, we know it's
193 * going to be a while so we stop spinning and go
194 * to sleep waiting for the lock to be available.
195 * This should strike the optimum balance between
196 * spinning and sleeping waiting for a lock.
198 while (task_curr(owner
) && (count
<= mutex_spin_max
)) {
199 if (down_trylock(mp
->km_sem
) == 0) {
200 MUTEX_STAT_INC(mutex_stats
, MUTEX_ENTER_SPIN
);
201 MUTEX_STAT_INC(mp
->km_stats
, MUTEX_ENTER_SPIN
);
207 /* The lock is held by a sleeping task so it's going to
208 * cost us minimally one context switch. We might as
209 * well sleep and yield the processor to other tasks.
212 MUTEX_STAT_INC(mutex_stats
, MUTEX_ENTER_SLEEP
);
213 MUTEX_STAT_INC(mp
->km_stats
, MUTEX_ENTER_SLEEP
);
216 MUTEX_STAT_INC(mutex_stats
, MUTEX_ENTER_TOTAL
);
217 MUTEX_STAT_INC(mp
->km_stats
, MUTEX_ENTER_TOTAL
);
221 __mutex_enter(kmutex_t
*mp
)
225 ASSERT(mp
->km_magic
== KM_MAGIC
);
227 switch (mp
->km_type
) {
229 while (down_trylock(mp
->km_sem
));
230 MUTEX_STAT_INC(mutex_stats
, MUTEX_ENTER_SPIN
);
231 MUTEX_STAT_INC(mp
->km_stats
, MUTEX_ENTER_SPIN
);
234 mutex_enter_adaptive(mp
);
238 ASSERT(mp
->km_owner
== NULL
);
239 mp
->km_owner
= current
;
243 EXPORT_SYMBOL(__mutex_enter
);
246 __mutex_exit(kmutex_t
*mp
)
250 ASSERT(mp
->km_magic
== KM_MAGIC
);
251 ASSERT(mp
->km_owner
== current
);
256 EXPORT_SYMBOL(__mutex_exit
);
258 /* Return 1 if mutex is held by current process, else zero. */
260 __mutex_owned(kmutex_t
*mp
)
264 ASSERT(mp
->km_magic
== KM_MAGIC
);
265 RETURN(mp
->km_owner
== current
);
267 EXPORT_SYMBOL(__mutex_owned
);
269 /* Return owner if mutex is owned, else NULL. */
271 __spl_mutex_owner(kmutex_t
*mp
)
275 ASSERT(mp
->km_magic
== KM_MAGIC
);
276 RETURN(mp
->km_owner
);
278 EXPORT_SYMBOL(__spl_mutex_owner
);
285 spin_lock_init(&mutex_stats_lock
);
286 INIT_LIST_HEAD(&mutex_stats_list
);
296 ASSERT(list_empty(&mutex_stats_list
));
301 module_param(mutex_spin_max
, int, 0644);
302 MODULE_PARM_DESC(mutex_spin_max
, "Spin a maximum of N times to aquire lock");