Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 4 Aug 2005 14:08:14 +0200
From:      Max Laier <max@love2party.net>
To:        freebsd-hackers@freebsd.org, hselasky@c2i.net
Subject:   Re: How to do proper locking
Message-ID:  <200508041408.22226.max@love2party.net>
In-Reply-To: <200508041340.58851.hselasky@c2i.net>
References:  <200508030023.04748.hselasky@c2i.net> <200508031321.32276.jhb@FreeBSD.org> <200508041340.58851.hselasky@c2i.net>

next in thread | previous in thread | raw e-mail | index | archive | help
--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--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200508041408.22226.max>