Priority Inversion and Unlimited Spin of pthread_rwlock_t

Florian Weimer fweimer@redhat.com
Tue Mar 12 14:39:11 GMT 2024


* 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?

> Unlimited user space spin is too dangerous to be used, how about
> limiting the total number of spins before suspending using futex? Or
> using rseq as mentioned in the LWN artible?

> [0] https://lwn.net/Articles/944895/

The LWN article does not talk about priority inversion.  With rseq,
userspace can detect preemption, but that may never happen with certain
realtime scheduling priorities.

Thanks,
Florian



More information about the Libc-alpha mailing list