Date: Mon, 02 Jun 2003 15:23:07 -0400 (EDT) From: John Baldwin <jhb@FreeBSD.org> To: Pawel Jakub Dawidek <nick@garage.freebsd.pl> Cc: freebsd-hackers@freebsd.org Subject: RE: Locking, locking... Message-ID: <XFMail.20030602152307.jhb@FreeBSD.org> In-Reply-To: <20030602075328.GV45118@garage.freebsd.pl>
next in thread | previous in thread | raw e-mail | index | archive | help
On 02-Jun-2003 Pawel Jakub Dawidek wrote: > 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 = 0; > SLIST_FOREACH(elem, &list_head, l_next) { > n++; > } > mtx_unlock(&mtx_list_lock); > tab = 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? In the process case, the all process list is protected by an sx lock (which you can hold across malloc(M_WAITOK)). Another solution in some cases is that if the list grows, just copy out the first n items. -- John Baldwin <jhb@FreeBSD.org> <>< http://www.FreeBSD.org/~jhb/ "Power Users Use the Power to Serve!" - http://www.FreeBSD.org/
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?XFMail.20030602152307.jhb>