summaryrefslogtreecommitdiffstats
path: root/sys/kern/kern_umtx.c
diff options
context:
space:
mode:
authordavidxu <davidxu@FreeBSD.org>2012-04-05 02:24:08 +0000
committerdavidxu <davidxu@FreeBSD.org>2012-04-05 02:24:08 +0000
commit8c31e244f2179705d23a9bc680f47ba978ff9bcb (patch)
tree60c1de33c1c2173dd66d3d3bd988eaf58e759fd3 /sys/kern/kern_umtx.c
parentbbab93098d58b8643f834eba361bb0d661ba3876 (diff)
downloadFreeBSD-src-8c31e244f2179705d23a9bc680f47ba978ff9bcb.zip
FreeBSD-src-8c31e244f2179705d23a9bc680f47ba978ff9bcb.tar.gz
umtx operation UMTX_OP_MUTEX_WAKE has a side-effect that it accesses
a mutex after a thread has unlocked it, it event writes data to the mutex memory to clear contention bit, there is a race that other threads can lock it and unlock it, then destroy it, so it should not write data to the mutex memory if there isn't any waiter. The new operation UMTX_OP_MUTEX_WAKE2 try to fix the problem. It requires thread library to clear the lock word entirely, then call the WAKE2 operation to check if there is any waiter in kernel, and try to wake up a thread, if necessary, the contention bit is set again by the operation. This also mitgates the chance that other threads find the contention bit and try to enter kernel to compete with each other to wake up sleeping thread, this is unnecessary. With this change, the mutex owner is no longer holding the mutex until it reaches a point where kernel umtx queue is locked, it releases the mutex as soon as possible. Performance is improved when the mutex is contensted heavily. On Intel i3-2310M, the runtime of a benchmark program is reduced from 26.87 seconds to 2.39 seconds, it even is better than UMTX_OP_MUTEX_WAKE which is deprecated now. http://people.freebsd.org/~davidxu/bench/mutex_perf.c
Diffstat (limited to 'sys/kern/kern_umtx.c')
-rw-r--r--sys/kern/kern_umtx.c84
1 files changed, 82 insertions, 2 deletions
diff --git a/sys/kern/kern_umtx.c b/sys/kern/kern_umtx.c
index 5c57166..71b19e7 100644
--- a/sys/kern/kern_umtx.c
+++ b/sys/kern/kern_umtx.c
@@ -1319,6 +1319,78 @@ do_wake_umutex(struct thread *td, struct umutex *m)
return (0);
}
+/*
+ * Check if the mutex has waiters and tries to fix contention bit.
+ */
+static int
+do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
+{
+ struct umtx_key key;
+ uint32_t owner, old;
+ int type;
+ int error;
+ int count;
+
+ switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
+ case 0:
+ type = TYPE_NORMAL_UMUTEX;
+ break;
+ case UMUTEX_PRIO_INHERIT:
+ type = TYPE_PI_UMUTEX;
+ break;
+ case UMUTEX_PRIO_PROTECT:
+ type = TYPE_PP_UMUTEX;
+ break;
+ default:
+ return (EINVAL);
+ }
+ if ((error = umtx_key_get(m, type, GET_SHARE(flags),
+ &key)) != 0)
+ return (error);
+
+ owner = 0;
+ umtxq_lock(&key);
+ umtxq_busy(&key);
+ count = umtxq_count(&key);
+ umtxq_unlock(&key);
+ /*
+ * Only repair contention bit if there is a waiter, this means the mutex
+ * is still being referenced by userland code, otherwise don't update
+ * any memory.
+ */
+ if (count > 1) {
+ owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
+ while ((owner & UMUTEX_CONTESTED) ==0) {
+ old = casuword32(&m->m_owner, owner,
+ owner|UMUTEX_CONTESTED);
+ if (old == owner)
+ break;
+ owner = old;
+ }
+ } else if (count == 1) {
+ owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
+ while ((owner & ~UMUTEX_CONTESTED) != 0 &&
+ (owner & UMUTEX_CONTESTED) == 0) {
+ old = casuword32(&m->m_owner, owner,
+ owner|UMUTEX_CONTESTED);
+ if (old == owner)
+ break;
+ owner = old;
+ }
+ }
+ umtxq_lock(&key);
+ if (owner == -1) {
+ error = EFAULT;
+ umtxq_signal(&key, INT_MAX);
+ }
+ else if (count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
+ umtxq_signal(&key, 1);
+ umtxq_unbusy(&key);
+ umtxq_unlock(&key);
+ umtx_key_release(&key);
+ return (error);
+}
+
static inline struct umtx_pi *
umtx_pi_alloc(int flags)
{
@@ -3152,6 +3224,12 @@ __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
return do_sem_wake(td, uap->obj);
}
+static int
+__umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
+{
+ return do_wake2_umutex(td, uap->obj, uap->val);
+}
+
typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
static _umtx_op_func op_table[] = {
@@ -3176,7 +3254,8 @@ static _umtx_op_func op_table[] = {
__umtx_op_wake_umutex, /* UMTX_OP_UMUTEX_WAKE */
__umtx_op_sem_wait, /* UMTX_OP_SEM_WAIT */
__umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
- __umtx_op_nwake_private /* UMTX_OP_NWAKE_PRIVATE */
+ __umtx_op_nwake_private, /* UMTX_OP_NWAKE_PRIVATE */
+ __umtx_op_wake2_umutex /* UMTX_OP_UMUTEX_WAKE2 */
};
int
@@ -3478,7 +3557,8 @@ static _umtx_op_func op_table_compat32[] = {
__umtx_op_wake_umutex, /* UMTX_OP_UMUTEX_WAKE */
__umtx_op_sem_wait_compat32, /* UMTX_OP_SEM_WAIT */
__umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
- __umtx_op_nwake_private32 /* UMTX_OP_NWAKE_PRIVATE */
+ __umtx_op_nwake_private32, /* UMTX_OP_NWAKE_PRIVATE */
+ __umtx_op_wake2_umutex /* UMTX_OP_UMUTEX_WAKE2 */
};
int
OpenPOWER on IntegriCloud