Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 11 Apr 2009 00:16:25 +0100
From:      Andrew Brampton <brampton+freebsd-hackers@gmail.com>
To:        freebsd-hackers@freebsd.org
Subject:   FreeBSD memguard + spinlocks
Message-ID:  <d41814900904101616n73c30e05t3c56a6cd8d9b6c0@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?d41814900904101616n73c30e05t3c56a6cd8d9b6c0>