public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Peng Zheng <pengzheng@apache.org>
To: libc-alpha@sourceware.org
Cc: triegel@redhat.com
Subject: Re: Priority Inversion and Unlimited Spin of pthread_rwlock_t
Date: Thu, 14 Mar 2024 15:32:26 +0800	[thread overview]
Message-ID: <ffd9db52-9a1b-4ade-9d75-419eab56b91f@apache.org> (raw)
In-Reply-To: <b68aa613-7a1f-4fae-8bb9-f306b76cba74@apache.org>

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


  reply	other threads:[~2024-03-14  7:32 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-12  3:19 Peng Zheng
2024-03-12  3:48 ` Peng Zheng
2024-03-14  7:32   ` Peng Zheng [this message]
2024-03-12 14:39 ` Florian Weimer
2024-03-13  1:48   ` Peng Zheng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ffd9db52-9a1b-4ade-9d75-419eab56b91f@apache.org \
    --to=pengzheng@apache.org \
    --cc=libc-alpha@sourceware.org \
    --cc=triegel@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).