FreeBSD memguard + spinlocks
Andrew Brampton
brampton+freebsd-hackers at gmail.com
Fri Apr 10 23:37:47 UTC 2009
Hi,
I'm having a problem with memguard(9) on FreeBSD 7.1 but before I ask
about that I just need to check my facts about malloc.
When in interrupt context malloc must be called with M_NOWAIT, this is
because I can't sleep inside a interrupt. Now when I hold a spinlock
(MTX_SPIN) I am also not allowed to sleep or obtain a sleepable mutex
(such as MTX_DEF). So I assume while holding a spin lock any mallocs I
do must have the M_NOWAIT flag? This was not clear from the manual
pages, but at least makes sense to me.
So my problem with memguard stems from the fact that I am locking a
spinlock, and then I'm calling malloc with M_NOWAIT. But inside
memguard_alloc a MTX_DEF is acquired causing WITNESS to panic.
So I think fundamental memguard is flawed and should be using MTX_SPIN
instead of MTX_DEF otherwise it can't be called from inside a
interrupt or when a spin lock is held. But maybe I'm missing
something?
Also on a related note, I see that MTX_SPIN disables interrupts,
making it a rather "heavy" spinlock. Is there a lighter spin lock that
literally just spins? I read that MTX_DEF are far quicker to acquire ,
but surely a light spinlock would be easier to acquire than sleeping?
I think for the moment I will fix my code by not using a MTX_SPIN
(since the code is not in a interrupt), however, I think memguard
should change its lock.
thanks
Andrew
More information about the freebsd-hackers
mailing list