Deferred scheduling for user-space critical sections
lwn.net
lwn.net
Interestingly, XNU already has an implementation of this that is somewhat similar in spirit: preemption free zones (https://github.com/search?q=repo%3Aapple-oss-distributions%2...). As far as I can tell os_atomic_lock does not use it for this purpose. Instead, it exists to allow OSAtomicFifoEnqueue/OSAtomicFifoDequeue to be lock-free, and in the typical Apple fashion solves the problem of "what if a userspace application uses this for evil" by implementing it in the kernel and mapping it via the commpage (think vDSO), so there isn't really any room for anyone but Apple to put code there.
Apple being Apple, they could have absolutely added PFZ to os_atomic_lock (for themselves, of course). One wonders why they didn't; perhaps it just turns out that it isn't worth the complexity.
(Look, I'm sorry all I talk about is Apple stuff, that's all I know.)
This is an old idea and many real-world mutex implementation have been doing this since forever. Some even let you configure the spin count: https://learn.microsoft.com/en-us/windows/win32/api/synchapi...
It's sort of similar to using a TCP congestion algorithm that's overly aggressive. Your own network connections might become faster as a result of them, but at the same time your flatmates won't be able to check their mail.
This talks about fairness between threads, and is from my understanding also how the current CFS scheduler approaches fairness. I also understand that fairness on a process level might be difficult to obtain.
But doesn't this thread level fairness render the point of a "malicous thread" moot? Fairness of threads within a process is pointless anyway, because this is all in the hands of the programmer and all they can do is shoot themselves in their own feet.
Fairness between processes would be interesting but we don't have it anyway, so deferred scheduling doesn't make the situation worse in that regard?
I think locks, parallelism, multithreading, async is difficult. I would like some visualisations to visualise locking behaviours and obvious bottlenecks. Being able to visually see a lock convoy or priority inversion would be helpful.
The kernel-userspace transition seems to be expensive and if you have 90 threads all contending for the same resource, then you cannot scale memory write/mutation access by 90 times by locking.
If the holder of the lock is not scheduled and is preempted then the lock attempter shall spin, potentially preventing the holder of the lock from being scheduled.
I wonder if there's lessons from static and dynamic scheduling here? If you know the lock hierarchy in the kernel, couldn't you intelligently schedule? I read a really good comment on HN by jcranmer about VLIW architecture recently. [1]
Hardware evolves faster than software.
This can be a general mechanism, which can be incorporated in the things kernel already know, creating a chain of dependencies and giving priority to something at the end of chain.
That's what, for example, futexes are for. But the "donating" part can be expensive, so short spins can actually be beneficial for overall system efficiency.