From owner-freebsd-hackers@FreeBSD.ORG Thu Feb 24 19:02: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 2B29D106566C for ; Thu, 24 Feb 2011 19:02:47 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from cyrus.watson.org (cyrus.watson.org [65.122.17.42]) by mx1.freebsd.org (Postfix) with ESMTP id EF6438FC18 for ; Thu, 24 Feb 2011 19:02:46 +0000 (UTC) Received: from bigwig.baldwin.cx (66.111.2.69.static.nyinternet.net [66.111.2.69]) by cyrus.watson.org (Postfix) with ESMTPSA id 9F24246B03; Thu, 24 Feb 2011 14:02:46 -0500 (EST) Received: from jhbbsd.localnet (unknown [209.249.190.10]) by bigwig.baldwin.cx (Postfix) with ESMTPSA id 3F8EA8A009; Thu, 24 Feb 2011 14:02:45 -0500 (EST) From: John Baldwin To: freebsd-hackers@freebsd.org Date: Thu, 24 Feb 2011 14:02:21 -0500 User-Agent: KMail/1.13.5 (FreeBSD/7.4-CBSD-20110107; KDE/4.4.5; amd64; ; ) References: In-Reply-To: MIME-Version: 1.0 Content-Type: Text/Plain; charset="windows-1252" Content-Transfer-Encoding: quoted-printable Message-Id: <201102241402.21556.jhb@freebsd.org> X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.2.6 (bigwig.baldwin.cx); Thu, 24 Feb 2011 14:02:45 -0500 (EST) X-Virus-Scanned: clamav-milter 0.96.3 at bigwig.baldwin.cx X-Virus-Status: Clean X-Spam-Status: No, score=0.5 required=4.2 tests=BAYES_00,MAY_BE_FORGED, RDNS_DYNAMIC autolearn=no version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on bigwig.baldwin.cx Cc: Dmitry Krivenok 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 19:02:47 -0000 On Thursday, February 24, 2011 10:47:27 am Dmitry Krivenok wrote: > Hello Hackers, >=20 > Is it allowed to call mtx_init on a mutex defined as an auto variable > and not initialized explicitly, i.e.: It does expect you to zero it first. I've considered adding a MTX_NEW flag= to=20 disable this check for places where the developer knows it is safe. Most=20 mutexes are allocated in an already-zero'd structure or BSS as Patrick note= d, so they are already correct. It is a trade off between catching double=20 initializations and requiring extra M_ZERO flags or bzero() calls in variou= s=20 places. > static int foo() > { > struct mtx m; // Uninitialized auto variable, so it's value is=20 undefined. > mtx_init(&m, "my_mutex", NULL, MTX_DEF); > =85 > // Do something > ... > mtx_destroy(&m); > return 0; > } >=20 > 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: >=20 > 79 /* Check for double-init and zero object. */ > 80 KASSERT(!lock_initalized(lock), ("lock \"%s\" %p already > initialized", > 81 name, lock)); >=20 > lock_initialized() just checks that a bit is set in lo_flags field of > struct lock_object: >=20 > 178 #define lock_initalized(lo) ((lo)->lo_flags & LO_INITIALIZED) >=20 > 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: >=20 > 37 struct mtx { > 38 struct lock_object lock_object; /* Common lock > properties. */ > 39 volatile uintptr_t mtx_lock; /* Owner and flags. */ > 40 }; >=20 > In some cases, the initial value of lo_flags _may_ have the > "initialized" bit set and KASSERT will call panic. >=20 > Is it user's responsibility to properly (how exactly?) initialize > struct mtx, e.g. > memset(&m, '\0', sizeof(struct mtx)); >=20 > Or should mtx_init() explicitly initialize all fields of struct mtx? >=20 > Thanks in advance! >=20 > --=20 > 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" >=20 =2D-=20 John Baldwin