Commit f8864972 authored by Eric Dumazet's avatar Eric Dumazet Committed by David S. Miller

ipv4: fix dst race in sk_dst_get()

When IP route cache had been removed in linux-3.6, we broke assumption
that dst entries were all freed after rcu grace period. DST_NOCACHE
dst were supposed to be freed from dst_release(). But it appears
we want to keep such dst around, either in UDP sockets or tunnels.

In sk_dst_get() we need to make sure dst refcount is not 0
before incrementing it, or else we might end up freeing a dst
twice.

DST_NOCACHE set on a dst does not mean this dst can not be attached
to a socket or a tunnel.

Then, before actual freeing, we need to observe a rcu grace period
to make sure all other cpus can catch the fact the dst is no longer
usable.
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Reported-by: default avatarDormando <dormando@rydia.net>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 99e72a0f
...@@ -1730,8 +1730,8 @@ sk_dst_get(struct sock *sk) ...@@ -1730,8 +1730,8 @@ sk_dst_get(struct sock *sk)
rcu_read_lock(); rcu_read_lock();
dst = rcu_dereference(sk->sk_dst_cache); dst = rcu_dereference(sk->sk_dst_cache);
if (dst) if (dst && !atomic_inc_not_zero(&dst->__refcnt))
dst_hold(dst); dst = NULL;
rcu_read_unlock(); rcu_read_unlock();
return dst; return dst;
} }
......
...@@ -269,6 +269,15 @@ struct dst_entry *dst_destroy(struct dst_entry * dst) ...@@ -269,6 +269,15 @@ struct dst_entry *dst_destroy(struct dst_entry * dst)
} }
EXPORT_SYMBOL(dst_destroy); EXPORT_SYMBOL(dst_destroy);
static void dst_destroy_rcu(struct rcu_head *head)
{
struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
dst = dst_destroy(dst);
if (dst)
__dst_free(dst);
}
void dst_release(struct dst_entry *dst) void dst_release(struct dst_entry *dst)
{ {
if (dst) { if (dst) {
...@@ -276,11 +285,8 @@ void dst_release(struct dst_entry *dst) ...@@ -276,11 +285,8 @@ void dst_release(struct dst_entry *dst)
newrefcnt = atomic_dec_return(&dst->__refcnt); newrefcnt = atomic_dec_return(&dst->__refcnt);
WARN_ON(newrefcnt < 0); WARN_ON(newrefcnt < 0);
if (unlikely(dst->flags & DST_NOCACHE) && !newrefcnt) { if (unlikely(dst->flags & DST_NOCACHE) && !newrefcnt)
dst = dst_destroy(dst); call_rcu(&dst->rcu_head, dst_destroy_rcu);
if (dst)
__dst_free(dst);
}
} }
} }
EXPORT_SYMBOL(dst_release); EXPORT_SYMBOL(dst_release);
......
...@@ -73,12 +73,7 @@ static void __tunnel_dst_set(struct ip_tunnel_dst *idst, ...@@ -73,12 +73,7 @@ static void __tunnel_dst_set(struct ip_tunnel_dst *idst,
{ {
struct dst_entry *old_dst; struct dst_entry *old_dst;
if (dst) {
if (dst->flags & DST_NOCACHE)
dst = NULL;
else
dst_clone(dst); dst_clone(dst);
}
old_dst = xchg((__force struct dst_entry **)&idst->dst, dst); old_dst = xchg((__force struct dst_entry **)&idst->dst, dst);
dst_release(old_dst); dst_release(old_dst);
} }
...@@ -108,13 +103,14 @@ static struct rtable *tunnel_rtable_get(struct ip_tunnel *t, u32 cookie) ...@@ -108,13 +103,14 @@ static struct rtable *tunnel_rtable_get(struct ip_tunnel *t, u32 cookie)
rcu_read_lock(); rcu_read_lock();
dst = rcu_dereference(this_cpu_ptr(t->dst_cache)->dst); dst = rcu_dereference(this_cpu_ptr(t->dst_cache)->dst);
if (dst && !atomic_inc_not_zero(&dst->__refcnt))
dst = NULL;
if (dst) { if (dst) {
if (dst->obsolete && dst->ops->check(dst, cookie) == NULL) { if (dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
rcu_read_unlock();
tunnel_dst_reset(t); tunnel_dst_reset(t);
return NULL; dst_release(dst);
dst = NULL;
} }
dst_hold(dst);
} }
rcu_read_unlock(); rcu_read_unlock();
return (struct rtable *)dst; return (struct rtable *)dst;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment