Priority Inversion and Unlimited Spin of pthread_rwlock_t
Peng Zheng
pengzheng@apache.org
Thu Mar 14 07:32:26 GMT 2024
On 2024/3/12 11:48, Peng Zheng wrote:
> On 2024/3/12 11:19, Peng Zheng wrote:
>> Hi,
>>
>> I found that there are several unlimited spins in the current
>> pthread_rwlock's implementation.
>> Therefore, it suffers from the same issue of user-space spinlocks as
>> mentioned in this LWN article ([0]):
>
> I forget to mention this issue is glibc-specific.
> Just checked musl's source code, all spin in its pthread_rwlock_t is
> limited to 100. For example:
It seems that this issue is introduced by
cc25c8b4c1196a8c29e9a45b1e096b99a87b7f8c, whose design is quite
complicated.
It's highly non-trivial to add limited spin to the current implementation.
This issue has extensive impact because OpenSSL use wrlock as ordinary
mutex by default.
https://github.com/openssl/openssl/blob/3cb0755323281267211fbe951b94a2552e99d32a/crypto/threads_pthread.c#L622
And it impacts nearly all subsystems of OpenSSL.
https://github.com/openssl/openssl/blob/b372b1f76450acdfed1e2301a39810146e28b02c/crypto/ex_data.c#L50C22-L50C34
I can safely tell that nearly all linux running real-time task using
OpenSSL might be affected by this issue (like real-time video streaming
over TLS connection).
>
> int __pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rw, const
> struct timespec *restrict at)
> {
> int r, t;
>
> r = pthread_rwlock_trywrlock(rw);
> if (r != EBUSY) return r;
>
> int spins = 100;
> while (spins-- && rw->_rw_lock && !rw->_rw_waiters) a_spin();
>
> while ((r=__pthread_rwlock_trywrlock(rw))==EBUSY) {
> if (!(r=rw->_rw_lock)) continue;
> t = r | 0x80000000;
> a_inc(&rw->_rw_waiters);
> a_cas(&rw->_rw_lock, r, t);
> r = __timedwait(&rw->_rw_lock, t, CLOCK_REALTIME, at,
> rw->_rw_shared^128);
> a_dec(&rw->_rw_waiters);
> if (r && r != EINTR) return r;
> }
> return r;
> }
>
> Regards,
>
--
Peng Zheng
More information about the Libc-alpha
mailing list