tun: Only free a netdev when all tun descriptors are closed
authorHerbert Xu <herbert@gondor.apana.org.au>
Sat, 18 Apr 2009 14:15:52 +0000 (14:15 +0000)
committerDavid S. Miller <davem@davemloft.net>
Mon, 20 Apr 2009 10:01:47 +0000 (03:01 -0700)
commit9c3fea6ab04a7bd9298e635bf29b4a5379f6c476
tree2b9977d85dc5f4de230a2d556e692cb30a6d945a
parent5db8765a86a4cbaf45adaf8c231cf9a6ca2dcfaf
tun: Only free a netdev when all tun descriptors are closed

The commit c70f182940f988448f3c12a209d18b1edc276e33 ("tun: Fix
races between tun_net_close and free_netdev") fixed a race where
an asynchronous deletion of a tun device can hose a poll(2) on
a tun fd attached to that device.

However, this came at the cost of moving the tun wait queue into
the tun file data structure.  The problem with this is that it
imposes restrictions on when and where the tun device can access
the wait queue since the tun file may change at any time due to
detaching and reattaching.

In particular, now that we need to use the wait queue on the
receive path it becomes difficult to properly synchronise this
with the detachment of the tun device.

This patch solves the original race in a different way.  Since
the race is only because the underlying memory gets freed, we
can prevent it simply by ensuring that we don't do that until
all tun descriptors ever attached to the device (even if they
have since be detached because they may still be sitting in poll)
have been closed.

This is done by using reference counting the attached tun file
descriptors.  The refcount in tun->sk has been reappropriated
for this purpose since it was already being used for that, albeit
from the opposite angle.

Note that we no longer zero tfile->tun since tun_get will return
NULL anyway after the refcount on tfile hits zero.  Instead it
represents whether this device has ever been attached to a device.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/tun.c