Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 22 Apr 2008 20:51:25 +0100 (BST)
From:      Robert Watson <rwatson@FreeBSD.org>
To:        vijay singh <vijjus@rocketmail.com>
Cc:        freebsd-net@freebsd.org, Brooks Davis <brooks@freebsd.org>
Subject:   Re: Regarding if_alloc()
Message-ID:  <20080422204551.P2206@fledge.watson.org>
In-Reply-To: <340035.20572.qm@web33502.mail.mud.yahoo.com>
References:  <340035.20572.qm@web33502.mail.mud.yahoo.com>

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

On Tue, 22 Apr 2008, vijay singh wrote:

> Robert, I am working on the patch, but I had a few questions. If we drop the 
> lock while if_grow() is running, how do we prevent a second caller, blocked 
> in if_alloc() to start scanning the ifindex_table, and end up deciding to 
> grow it again. In other words, we need to stall the ifindex_table scan in 
> if_alloc() till if_grow() has achieved finished. Since if_grow() will be 
> called relatively infrequently, should we consider holding the lock through 
> the routine? Your comments and suggestions are welcome.

Vijay,

if_grow() will be called only infrequently, but we do need to handle it 
correctly.  Usually code of this sort handles races in the following sort of 
optimistic way:

 	LOCK();
 	while (need space) {
 		LOCK();
 		allocate new space;
 		UNLOCK();
 		if (new space > old space)
 			install new space, free old space;
 		else
 			free new space;
 	}
 	use space;
 	UNLOCK();

Basically, you need to gracefully handle the case where you released the lock, 
allocated the space, re-acquired the lock, and discovered someone else had 
done the same.  The reason you need a while loop is that you may have rather 
seriously lost the race -- someone else may have allocated space, and then 
several someone elses may have then used the sapce, leaving you with less 
memory than you need, but still needing to allocate yet more memory.  The 
above is not starvation free, but in practice the byindex table doesn't grow 
all that much, and should never trigger the race anyway :-).

Robert N M Watson
Computer Laboratory
University of Cambridge



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