Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 2 Jun 2003 09:53:28 +0200
From:      Pawel Jakub Dawidek <nick@garage.freebsd.pl>
To:        freebsd-hackers@freebsd.org
Subject:   Locking, locking...
Message-ID:  <20030602075328.GV45118@garage.freebsd.pl>

next in thread | raw e-mail | index | archive | help

--aPbDRKcb5+hBUoJi
Content-Type: text/plain; charset=iso-8859-2
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hello hackers...

I need advice how to handle with one locking problem.

For example I got some list with some values. This list could be
modified by any process. I want to get values from this list and
copy them to allocated table. But how to count number of needed
fields?

	mtx_lock(&mtx_list_lock);
	n =3D 0;
	SLIST_FOREACH(elem, &list_head, l_next) {
		n++;
	}
	mtx_unlock(&mtx_list_lock);
	tab =3D malloc(n * sizeof(something), M_TEMP, M_WAITOK);
	mtx_lock(&mtx_list_lock);
	[...]

As we all knew size of list could be changed when we were in malloc().
Of course we could check list size again after malloc() and mtx_lock(),
but what to do when it was changed? Recall memory allocation?
If size of this list depends on every process there is a chance to DoS
such piece of code. Return an error? Not always it is possible.

IMHO there should be malloc() version that could be called under lock.
Malloc() M_NOWAIT could be called in such scenario?
How do you handle with situations like this?

Thanks!

--=20
Pawel Jakub Dawidek                       pawel@dawidek.net
UNIX Systems Programmer/Administrator     http://garage.freebsd.pl
Am I Evil? Yes, I Am!                     http://cerber.sourceforge.net

--aPbDRKcb5+hBUoJi
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (FreeBSD)

iQCVAwUBPtsCeD/PhmMH/Mf1AQEilwP+N/Umn+2e4+VFSQisBa6hKpIXfyPFFvoY
xnBoJuzBp+clBVn7cgTPHy1TfhWZF4caNcFBcjQLHENvdI/G45pFyXP3Xoe3BSrO
bXSTxevhzz7ISBxmjmh20acI3RNY3BdRNuKuXNN4Kob5TdMb1Lp72t3O/txEQCDf
DweTa/Buv1g=
=RcVv
-----END PGP SIGNATURE-----

--aPbDRKcb5+hBUoJi--



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