dlerror: don't gratuitously hold freebuf_queue lock while freeing
authorRich Felker <dalias@aerifal.cx>
Tue, 10 Nov 2020 19:29:05 +0000 (14:29 -0500)
committerRich Felker <dalias@aerifal.cx>
Wed, 11 Nov 2020 15:54:58 +0000 (10:54 -0500)
commitcbecda0b506c7d49a2f7fe3dc44e0e3dcf663764
treef6aaefddc91ac739a8c8bae6426ba7a5758a45c7
parent4ffa7068993ab4a1e1cedfed8954db979d9b7fbd
dlerror: don't gratuitously hold freebuf_queue lock while freeing

thread-local buffers allocated for dlerror need to be queued for free
at a later time when the owning thread exits, since malloc may be
replaced by application code and the exiting context is not valid to
call application code from. the code to process queue of pending
frees, introduced in commit aa5a9d15e09851f7b4a1668e9dbde0f6234abada,
gratuitously held the lock for the entire duration of queue
processing, updating the global queue pointer after each free, despite
there being no logical requirement that all frees finish before
another thread can access the queue.

instead, immediately claim the whole queue for freeing and release the
lock, then walk the list and perform frees without the lock held. the
change is unlikely to make any meaningful difference to performance,
but it eliminates one point where the allocator is called under an
internal lock. since the allocator may be application-provided, such
calls are undesirable because they allow application code to impede
forward progress of libc functions in other threads arbitrarily long,
and to induce deadlock if it calls a libc function that requires the
same lock.

the change also eliminates a lock ordering consideration that's an
impediment upcoming work with multithreaded fork.
src/ldso/dlerror.c