From owner-freebsd-hackers@FreeBSD.ORG Fri Apr 10 23:37:47 2009 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 76D1C1065670 for ; Fri, 10 Apr 2009 23:37:47 +0000 (UTC) (envelope-from brampton@gmail.com) Received: from mail-ew0-f171.google.com (mail-ew0-f171.google.com [209.85.219.171]) by mx1.freebsd.org (Postfix) with ESMTP id D9B488FC19 for ; Fri, 10 Apr 2009 23:37:46 +0000 (UTC) (envelope-from brampton@gmail.com) Received: by ewy19 with SMTP id 19so1254002ewy.43 for ; Fri, 10 Apr 2009 16:37:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:date :x-google-sender-auth:message-id:subject:from:to:content-type :content-transfer-encoding; bh=4CuX10XSoox3AOVNrcA3mjoHkfn2qG5wzcpGEeSqA4M=; b=oydtt28nZ3Vcay5RPRqJKCUnYIut+nnXXgCUHp8sUduqGS4YfNMIU3RUgoxFDS66b/ RtOwG+BzX6vwLESKYQkuzDglap2Gf0evnku4r/pDDabKXJ1TM1/Rav8OO9jsBkjNpI9c 6tqcZ2kTouCIOzOTBEADBKyRl7ORcDNVyTXhg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:date:x-google-sender-auth:message-id:subject :from:to:content-type:content-transfer-encoding; b=J68cys4JSVVU83PRwmfYC3O/A9Omo4tSh6iyEq7RMYN1l1F0eK5TIRnyHjgwEFoPiq vEqnNCPYRnKchFH6T8df2h/tGo58XMu7ItuT6hnYLC1fBG21JV4vi0drt/J1OTXZsRqY v45u2TTS1DOBRTNdWJFUpgktdiZfEmylhS6ZI= MIME-Version: 1.0 Sender: brampton@gmail.com Received: by 10.210.42.13 with SMTP id p13mr1613136ebp.53.1239405385849; Fri, 10 Apr 2009 16:16:25 -0700 (PDT) Date: Sat, 11 Apr 2009 00:16:25 +0100 X-Google-Sender-Auth: b5a3b9511c10de23 Message-ID: From: Andrew Brampton To: freebsd-hackers@freebsd.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Subject: FreeBSD memguard + spinlocks X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 10 Apr 2009 23:37:47 -0000 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