Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20261130134010.GG5716@brightrain.aerifal.cx>
Date: Mon, 30 Nov 2026 08:40:11 -0500
From: Rich Felker <dalias@...c.org>
To: Axel LORENTE <axel.lorente@...com>,
	"musl@...ts.openwall.com" <musl@...ts.openwall.com>,
	Stanislav Meduna <Stanislav.Meduna@...com>
Subject: Re: Priority inheritance for malloc internal locks

On Mon, Nov 30, 2026 at 01:36:46PM +0100, Szabolcs Nagy wrote:
> * Axel LORENTE <axel.lorente@...com> [2026-11-30 11:33:32 +0000]:
> > We recently hit a priority inversion issue involving malloc in our application.
> > 
> > A high priority thread was blocked in free() on a malloc lock held by a lower priority thread.
> > Since there is no priority inheritance on this lock, the lower priority thread could itself be preempted by intermediate priority threads.
> > In one of our traces this resulted in the high priority thread being blocked for around 34 ms, which was enough to miss our deadline.
> > 
> > Looking into it, I found this old change:
> > 
> > https://git.musl-libc.org/cgit/musl/commit/src/thread?h=rs-1.0&id=4750cf4202c29a895639b89099a7bdbe9ae422b6
> > 
> > I understand the choice made back in 2012 and the cost of using PI for internal locks, so I don't think changing the default behavior for everyone would necessarily make sense.
> > 
> > Would you consider having an option to build musl with priority inheritance for these locks?
> > 
> > On our side we try to avoid allocations from real-time paths, but there is a tradeoff.
> > Pre-allocating everything can mean reserving a lot more memory than we actually need, while allowing some allocations at runtime exposes us to this kind of priority inversion.
> > 
> > A compile-time option, or something opt-in, would let applications with RT constraints choose that tradeoff.
> > 
> > Is this something that could make sense for you?
> 
> note that if malloc is the only issue then
> an application can safely interpose that
> (e.g. copy musl malloc and change the locks)
> 
> this does not help with other locks though.

Indeed, this is a huge can of worms I don't think we want to get into.
There are many such internal locks, with a locking order protocol
between them needed by fork and in other places, and I don't think we
can even begin trying to make them priority-inversion-safe without
messing up and making promises we can't keep.

If you want just malloc, the best solution is probably to just link a
custom malloc that can use PI locks.

But really, it's generally not realtime-safe to call random libc
functions that might need locking from a context with realtime
priority needs. Even if malloc had the right PI locks for it,
allocation is inherently something that might need to evict other
pages and wait on locks in kernelspace, and I'm not sure if the kernel
does anything here to avoid priority inversion; my experience with it
is that it's really bad at stuff in this area. You really want to have
things preallocated for realtime, or when that's not possible, request
allocation asynchronously from another task rather than having it
directly in a hot realtime path.

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.