From owner-freebsd-hackers@FreeBSD.ORG Thu Aug 4 12:08:33 2005 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id B300B16A41F; Thu, 4 Aug 2005 12:08:33 +0000 (GMT) (envelope-from max@love2party.net) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.186]) by mx1.FreeBSD.org (Postfix) with ESMTP id 24AE343D45; Thu, 4 Aug 2005 12:08:33 +0000 (GMT) (envelope-from max@love2party.net) Received: from p54A3F182.dip.t-dialin.net [84.163.241.130] (helo=donor.laier.local) by mrelayeu.kundenserver.de with ESMTP (Nemesis), id 0MKxQS-1E0eWU2RQS-0008CH; Thu, 04 Aug 2005 14:08:30 +0200 From: Max Laier To: freebsd-hackers@freebsd.org, hselasky@c2i.net Date: Thu, 4 Aug 2005 14:08:14 +0200 User-Agent: KMail/1.8 References: <200508030023.04748.hselasky@c2i.net> <200508031321.32276.jhb@FreeBSD.org> <200508041340.58851.hselasky@c2i.net> In-Reply-To: <200508041340.58851.hselasky@c2i.net> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart6576948.aXnCKbuJDp"; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-Transfer-Encoding: 7bit Message-Id: <200508041408.22226.max@love2party.net> X-Provags-ID: kundenserver.de abuse@kundenserver.de login:61c499deaeeba3ba5be80f48ecc83056 Cc: Subject: Re: How to do proper locking 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, 04 Aug 2005 12:08:33 -0000 --nextPart6576948.aXnCKbuJDp Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On Thursday 04 August 2005 13:40, Hans Petter Selasky wrote: > This is a copy and paste from the kernel sources: > > struct ucred * > crhold(struct ucred *cr) > { > The problem is, what happens if the kernel switches thread right here, and > then the other thread calls "crfree()" on this structure, that will > "free()" memory pointed to by "cr". Then the first line of code below will > access freed memory, and then we are going for a segment fault or worse. > > mtx_lock(cr->cr_mtxp); > cr->cr_ref++; > mtx_unlock(cr->cr_mtxp); > return (cr); > } It seems that you are misunderstanding the concept of reference counting. = The=20 idea is to have a reference as long as you are handing around the object. = In=20 order to call crhold() you must already hold a reference to the credential.= =20 This will prevent the credential structure to vanish and allow you to pass= =20 the credential to another consumer that now can assume to hold a reference = of=20 it's own. If you can access your objects from a external list, you have to hold a glo= bal=20 lock that protects: 1) Sanity of the list 2) Free operations to the member objects If you access the objects via the list/tree/whatsoever "most of the time" -= =20 refcounting isn't for you. =2D-=20 /"\ Best regards, | mlaier@freebsd.org \ / Max Laier | ICQ #67774661 X http://pf4freebsd.love2party.net/ | mlaier@EFnet / \ ASCII Ribbon Campaign | Against HTML Mail and News --nextPart6576948.aXnCKbuJDp Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (FreeBSD) iD8DBQBC8gU2XyyEoT62BG0RArv0AKCBQaMH99vb7IL5aIvbd/7Sj7UvJgCfZHzs jY5t4ZoLCl602BUTeBbg+Z4= =xfxh -----END PGP SIGNATURE----- --nextPart6576948.aXnCKbuJDp--