From owner-freebsd-hackers@FreeBSD.ORG Thu Feb 24 17:51:47 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 A38CD1065672 for ; Thu, 24 Feb 2011 17:51:47 +0000 (UTC) (envelope-from PMahan@adaranet.com) Received: from barracuda.adaranet.com (smtp.adaranet.com [72.5.229.2]) by mx1.freebsd.org (Postfix) with ESMTP id 7BD7D8FC08 for ; Thu, 24 Feb 2011 17:51:47 +0000 (UTC) X-ASG-Debug-ID: 1298567532-554971ca0001-P5m3U7 Received: from SJ-EXCH-1.adaranet.com ([10.10.1.29]) by barracuda.adaranet.com with ESMTP id EnDoOgblN6SnjMA8; Thu, 24 Feb 2011 09:12:12 -0800 (PST) X-Barracuda-Envelope-From: PMahan@adaranet.com Received: from SJ-EXCH-1.adaranet.com ([fe80::7042:d8c2:5973:c523]) by SJ-EXCH-1.adaranet.com ([fe80::7042:d8c2:5973:c523%14]) with mapi; Thu, 24 Feb 2011 09:12:12 -0800 From: Patrick Mahan X-Barracuda-BBL-IP: fe80::7042:d8c2:5973:c523 X-Barracuda-RBL-IP: fe80::7042:d8c2:5973:c523 To: Dmitry Krivenok , "freebsd-hackers@freebsd.org" Date: Thu, 24 Feb 2011 09:12:00 -0800 X-ASG-Orig-Subj: RE: mtx_init/lock_init and uninitialized struct mtx Thread-Topic: mtx_init/lock_init and uninitialized struct mtx Thread-Index: AcvUPXQVDy7BwaqZQm+rpoeC1wOgRQAB+pcw Message-ID: <32AB5C9615CC494997D9ABB1DB12783C024CD0266E@SJ-EXCH-1.adaranet.com> References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-cr-hashedpuzzle: hXI= CxA3 C5xt EC+O FdjR GA+g G2Hb Iy5O LCTO LMMx L/A2 NZRP OvRV PNxK P2pC TGN1; 2; ZgByAGUAZQBiAHMAZAAtAGgAYQBjAGsAZQByAHMAQABmAHIAZQBlAGIAcwBkAC4AbwByAGcAOwBrAHIAaQB2AGUAbgBvAGsALgBkAG0AaQB0AHIAeQBAAGcAbQBhAGkAbAAuAGMAbwBtAA==; Sosha1_v1; 7; {B412F822-DEF6-4767-A32E-DBC3ABCAA9D3}; cABtAGEAaABhAG4AQABhAGQAYQByAGEAbgBlAHQALgBjAG8AbQA=; Thu, 24 Feb 2011 17:12:01 GMT; UgBFADoAIABtAHQAeABfAGkAbgBpAHQALwBsAG8AYwBrAF8AaQBuAGkAdAAgAGEAbgBkACAAdQBuAGkAbgBpAHQAaQBhAGwAaQB6AGUAZAAgAHMAdAByAHUAYwB0ACAAbQB0AHgA x-cr-puzzleid: {B412F822-DEF6-4767-A32E-DBC3ABCAA9D3} acceptlanguage: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Barracuda-Connect: UNKNOWN[10.10.1.29] X-Barracuda-Start-Time: 1298567532 X-Barracuda-URL: http://172.16.10.203:8000/cgi-mod/mark.cgi X-Virus-Scanned: by bsmtpd at adaranet.com Cc: Subject: RE: 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 17:51:47 -0000 > -----Original Message----- > From: owner-freebsd-hackers@freebsd.org [mailto:owner-freebsd- > hackers@freebsd.org] On Behalf Of Dmitry Krivenok > Sent: Thursday, February 24, 2011 7:47 AM > To: freebsd-hackers@freebsd.org > Subject: mtx_init/lock_init and uninitialized struct mtx > > 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 > undefined. > mtx_init(&m, "my_mutex", NULL, MTX_DEF); > ... > // 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! > When dealing with stack variables, I always initialize them to a known stat= e, (mostly doing as you do above, with memset() though in the kernel I then to use bzero()) Given that if it is global then it is either in .bss or in .data if it has been initialized. If it is part of a structure then most often you zero ou= t the structure right after allocation. So there is an implicit assumption that the structure will be zero'd before calling mtx_init(). Patrick ---------------------------------------------------- Patrick Mahan Lead Technical Kernel Engineer Adara Networks Disclaimer: The opinions expressed here are solely the responsibility of th= e author and are not to be construed as an official opinion of Adara Networks. > -- > Sincerely yours, Dmitry V. Krivenok > e-mail: krivenok.dmitry@gmail.com > skype: krivenok_dmitry > jabber: krivenok_dmitry@jabber.ru > icq: 242-526-443 > _______________________________________________ > freebsd-hackers@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org= "