From: Peng Zheng <pengzheng@apache.org>
To: Florian Weimer <fweimer@redhat.com>
Cc: libc-alpha@sourceware.org
Subject: Re: Priority Inversion and Unlimited Spin of pthread_rwlock_t
Date: Wed, 13 Mar 2024 09:48:32 +0800 [thread overview]
Message-ID: <f1c5dba4-de52-4ae6-97ee-c1229d077ad5@apache.org> (raw)
In-Reply-To: <87jzm7tsz4.fsf@oldenburg.str.redhat.com>
On 2024/3/12 22:39, Florian Weimer wrote:
> * Peng Zheng:
>
>> And the SCHED_OTHER writer was just about to enable the `__wrphase_futex` in
>> `__pthread_rwlock_wrlock_full` (just one ARM instruction away)
>> but never able to do that (the two readers ate nearly all available CPUs):
>>
>> while ((r & PTHREAD_RWLOCK_WRPHASE) == 0
>> && (r >> PTHREAD_RWLOCK_READER_SHIFT) == 0)
>> {
>> if (atomic_compare_exchange_weak_acquire (&rwlock->__data.__readers,
>> &r, r | PTHREAD_RWLOCK_WRPHASE))
>> {
>> atomic_store_relaxed (&rwlock->__data.__wrphase_futex, 1); /* writer was stuck HERE! */
>>
>> goto done;
>> }
>> /* TODO Back-off. */
>> }
>
> Is this about filling in the TODO?
No, I forgot to remove inline comments from the original source.
It is about a low priority (SCHED_OTHER) writer, which was about to
acquire its lock, preempted by high priority readers, and thus was not
able to set `__wrphase_futex` to 1 (see IT IS PREEMPTED HERE comment).
while ((r & PTHREAD_RWLOCK_WRPHASE) == 0
&& (r >> PTHREAD_RWLOCK_READER_SHIFT) == 0)
{
if (atomic_compare_exchange_weak_acquire (&rwlock->__data.__readers,
&r, r | PTHREAD_RWLOCK_WRPHASE))
{
/* IT IS PREEMPTED HERE */
atomic_store_relaxed (&rwlock->__data.__wrphase_futex, 1);
goto done;
}
}
And these two priority readers were stuck in a loop near the end of
`__pthread_rwlock_rdlock_full` eating all available CPU.
for (;;)
{
while (((wpf = atomic_load_relaxed (&rwlock->__data.__wrphase_futex))
| PTHREAD_RWLOCK_FUTEX_USED) == (1 | PTHREAD_RWLOCK_FUTEX_USED))
{/*omitted*/}
if (ready)
break;
if ((atomic_load_acquire (&rwlock->__data.__readers)
& PTHREAD_RWLOCK_WRPHASE) == 0)
ready = true;
}
return 0;
Note that PTHREAD_RWLOCK_WRPHASE was already set by the preempted
writer. That means `ready` is always false.
Note also that `__wrphase_futex` was not yet enabled by the preempted
writer. That means these readers can not wait on futex to stop spinning.
This illustrates one of the several unlimited spin possibilities and I
encounter two/three of them. If you are interested, I could provide
corresponding postmortem debug sessions.
--
Peng Zheng
prev parent reply other threads:[~2024-03-13 1:48 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
2024-03-12 14:39 ` Florian Weimer
2024-03-13 1:48 ` Peng Zheng [this message]
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=f1c5dba4-de52-4ae6-97ee-c1229d077ad5@apache.org \
--to=pengzheng@apache.org \
--cc=fweimer@redhat.com \
--cc=libc-alpha@sourceware.org \
/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).