• Herbert Xu's avatar
    [TCP]: Fix sock_orphan dead lock · 75c2d907
    Herbert Xu authored
    Calling sock_orphan inside bh_lock_sock in tcp_close can lead to dead
    locks.  For example, the inet_diag code holds sk_callback_lock without
    disabling BH.  If an inbound packet arrives during that admittedly tiny
    window, it will cause a dead lock on bh_lock_sock.  Another possible
    path would be through sock_wfree if the network device driver frees the
    tx skb in process context with BH enabled.
    
    We can fix this by moving sock_orphan out of bh_lock_sock.
    
    The tricky bit is to work out when we need to destroy the socket
    ourselves and when it has already been destroyed by someone else.
    
    By moving sock_orphan before the release_sock we can solve this
    problem.  This is because as long as we own the socket lock its
    state cannot change.
    
    So we simply record the socket state before the release_sock
    and then check the state again after we regain the socket lock.
    If the socket state has transitioned to TCP_CLOSE in the time being,
    we know that the socket has been destroyed.  Otherwise the socket is
    still ours to keep.
    
    Note that I've also moved the increment on the orphan count forward.
    This may look like a problem as we're increasing it even if the socket
    is just about to be destroyed where it'll be decreased again.  However,
    this simply enlarges a window that already exists.  This also changes
    the orphan count test by one.
    
    Considering what the orphan count is meant to do this is no big deal.
    
    This problem was discoverd by Ingo Molnar using his lock validator.
    Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
    Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
    75c2d907
tcp.c 59.3 KB