Date: Tue, 17 Apr 2012 09:02:56 +0000 (UTC) From: David Xu <davidxu@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-9@freebsd.org Subject: svn commit: r234371 - in stable/9/sys: kern sys Message-ID: <201204170902.q3H92u11073978@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: davidxu Date: Tue Apr 17 09:02:55 2012 New Revision: 234371 URL: http://svn.freebsd.org/changeset/base/234371 Log: MFC r233912: mtx 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 Modified: stable/9/sys/kern/kern_umtx.c stable/9/sys/sys/umtx.h Directory Properties: stable/9/sys/ (props changed) Modified: stable/9/sys/kern/kern_umtx.c ============================================================================== --- stable/9/sys/kern/kern_umtx.c Tue Apr 17 07:22:14 2012 (r234370) +++ stable/9/sys/kern/kern_umtx.c Tue Apr 17 09:02:55 2012 (r234371) @@ -1276,6 +1276,78 @@ do_wake_umutex(struct thread *td, struct 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) { @@ -3183,6 +3255,12 @@ __umtx_op_sem_wake(struct thread *td, st 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[] = { @@ -3207,7 +3285,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 @@ -3473,7 +3552,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 Modified: stable/9/sys/sys/umtx.h ============================================================================== --- stable/9/sys/sys/umtx.h Tue Apr 17 07:22:14 2012 (r234370) +++ stable/9/sys/sys/umtx.h Tue Apr 17 09:02:55 2012 (r234371) @@ -76,11 +76,12 @@ #define UMTX_OP_WAIT_UINT_PRIVATE 15 #define UMTX_OP_WAKE_PRIVATE 16 #define UMTX_OP_MUTEX_WAIT 17 -#define UMTX_OP_MUTEX_WAKE 18 +#define UMTX_OP_MUTEX_WAKE 18 /* deprecated */ #define UMTX_OP_SEM_WAIT 19 #define UMTX_OP_SEM_WAKE 20 #define UMTX_OP_NWAKE_PRIVATE 21 -#define UMTX_OP_MAX 22 +#define UMTX_OP_MUTEX_WAKE2 22 +#define UMTX_OP_MAX 23 /* Flags for UMTX_OP_CV_WAIT */ #define CVWAIT_CHECK_UNPARKING 0x01
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201204170902.q3H92u11073978>