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
[-- Attachment #1 --]
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
idea is to have a reference as long as you are handing around the object. In
order to call crhold() you must already hold a reference to the credential.
This will prevent the credential structure to vanish and allow you to pass
the credential to another consumer that now can assume to hold a reference of
it's own.
If you can access your objects from a external list, you have to hold a global
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" -
refcounting isn't for you.
--
/"\ Best regards, | mlaier@freebsd.org
\ / Max Laier | ICQ #67774661
X http://pf4freebsd.love2party.net/ | mlaier@EFnet
/ \ ASCII Ribbon Campaign | Against HTML Mail and News
[-- Attachment #2 --]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (FreeBSD)
iD8DBQBC8gU2XyyEoT62BG0RArv0AKCBQaMH99vb7IL5aIvbd/7Sj7UvJgCfZHzs
jY5t4ZoLCl602BUTeBbg+Z4=
=xfxh
-----END PGP SIGNATURE-----
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200508041408.22226.max>
