From owner-freebsd-hackers@FreeBSD.ORG Thu Feb 24 16:10:48 2011 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 7C50F106566B for ; Thu, 24 Feb 2011 16:10:48 +0000 (UTC) (envelope-from krivenok.dmitry@gmail.com) Received: from mail-iy0-f182.google.com (mail-iy0-f182.google.com [209.85.210.182]) by mx1.freebsd.org (Postfix) with ESMTP id 46E8F8FC16 for ; Thu, 24 Feb 2011 16:10:48 +0000 (UTC) Received: by mail-iy0-f182.google.com with SMTP id 12so405638iyj.13 for ; Thu, 24 Feb 2011 08:10:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=mITIevIDF35GEt5qvEkWScrValjT73eaiL7IIQhInIY=; b=RIkvdhKfwxGZbtPhYL46Qg5GL9V8bqnscn8Vh1nNRhi/FXfwE8vWfHqcKNfTSGrmLD r6wxepTBQDuSvxtaoxF2Xx98P99sKyeGSN8FtEsamJrufvjqRmMNGnE3JK83IzMaY+5V vzb7sTnEuGdfW7eGknIpZj+UC2m4hwiaVjoJs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type :content-transfer-encoding; b=Dl+CKqht2pSZ0LG4SBKU72pzV19ANcKwFPRDOMTTuAwExpukOct7usK5Bd44jtbWlL MIxeZtdyse9M7mgISr+Scg17rAAJqJsJGRDY6MXT9/3X9VX12rO6TPV0oLMREouktvU3 n2XAEjRFDwBBBit0xK8DvI991tCLRGxWEI4MQ= MIME-Version: 1.0 Received: by 10.231.182.13 with SMTP id ca13mr1665660ibb.57.1298562447160; Thu, 24 Feb 2011 07:47:27 -0800 (PST) Received: by 10.231.144.140 with HTTP; Thu, 24 Feb 2011 07:47:27 -0800 (PST) Date: Thu, 24 Feb 2011 18:47:27 +0300 Message-ID: From: Dmitry Krivenok To: freebsd-hackers@freebsd.org Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable Subject: mtx_init/lock_init and uninitialized struct mtx 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: Thu, 24 Feb 2011 16:10:48 -0000 Hello Hackers, Is it allowed to call mtx_init on a mutex defined as an auto variable and not initialized explicitly, i.e.: static int foo() { struct mtx m; // Uninitialized auto variable, so it's value is undefine= d. mtx_init(&m, "my_mutex", NULL, MTX_DEF); =85 // Do something ... mtx_destroy(&m); return 0; } I encountered a problem with such code on a kernel compiled with INVARIANTS option. The problem is that mtx_init calls lock_init(&m->lock_object) and lock_init, in turn, calls: 79 /* Check for double-init and zero object. */ 80 KASSERT(!lock_initalized(lock), ("lock \"%s\" %p already initialized", 81 name, lock)); lock_initialized() just checks that a bit is set in lo_flags field of struct lock_object: 178 #define lock_initalized(lo) ((lo)->lo_flags & LO_INITIALIZED) However, the structure containing this field is never initialized (neither in mtx_init nor in lock_init). So, assuming that the mutex was defined as auto variable, the content of lock_object field of struct mtx is also undefined: 37 struct mtx { 38 struct lock_object lock_object; /* Common lock properties. */ 39 volatile uintptr_t mtx_lock; /* Owner and flags. */ 40 }; In some cases, the initial value of lo_flags _may_ have the "initialized" bit set and KASSERT will call panic. Is it user's responsibility to properly (how exactly?) initialize struct mtx, e.g. memset(&m, '\0', sizeof(struct mtx)); Or should mtx_init() explicitly initialize all fields of struct mtx? Thanks in advance! --=20 Sincerely yours, Dmitry V. Krivenok e-mail: krivenok.dmitry@gmail.com skype: krivenok_dmitry jabber: krivenok_dmitry@jabber.ru icq: 242-526-443