Skip to content

Commit 8728b0c

Browse files
edumazetJiri Slaby
authored andcommitted
ipv4: irq safe sk_dst_[re]set() and ipv4_sk_update_pmtu() fix
[ Upstream commit 7f50236 ] We have two different ways to handle changes to sk->sk_dst First way (used by TCP) assumes socket lock is owned by caller, and use no extra lock : __sk_dst_set() & __sk_dst_reset() Another way (used by UDP) uses sk_dst_lock because socket lock is not always taken. Note that sk_dst_lock is not softirq safe. These ways are not inter changeable for a given socket type. ipv4_sk_update_pmtu(), added in linux-3.8, added a race, as it used the socket lock as synchronization, but users might be UDP sockets. Instead of converting sk_dst_lock to a softirq safe version, use xchg() as we did for sk_rx_dst in commit e47eb5d ("udp: ipv4: do not use sk_dst_lock from softirq context") In a follow up patch, we probably can remove sk_dst_lock, as it is only used in IPv6. Signed-off-by: Eric Dumazet <edumazet@google.com> Cc: Steffen Klassert <steffen.klassert@secunet.com> Fixes: 9cb3a50 ("ipv4: Invalidate the socket cached route on pmtu events if possible") Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
1 parent 591b1e1 commit 8728b0c

2 files changed

Lines changed: 14 additions & 13 deletions

File tree

‎include/net/sock.h‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,9 +1789,11 @@ __sk_dst_set(struct sock *sk, struct dst_entry *dst)
17891789
static inline void
17901790
sk_dst_set(struct sock *sk, struct dst_entry *dst)
17911791
{
1792-
spin_lock(&sk->sk_dst_lock);
1793-
__sk_dst_set(sk, dst);
1794-
spin_unlock(&sk->sk_dst_lock);
1792+
struct dst_entry *old_dst;
1793+
1794+
sk_tx_queue_clear(sk);
1795+
old_dst = xchg(&sk->sk_dst_cache, dst);
1796+
dst_release(old_dst);
17951797
}
17961798

17971799
static inline void
@@ -1803,9 +1805,7 @@ __sk_dst_reset(struct sock *sk)
18031805
static inline void
18041806
sk_dst_reset(struct sock *sk)
18051807
{
1806-
spin_lock(&sk->sk_dst_lock);
1807-
__sk_dst_reset(sk);
1808-
spin_unlock(&sk->sk_dst_lock);
1808+
sk_dst_set(sk, NULL);
18091809
}
18101810

18111811
extern struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie);

‎net/ipv4/route.c‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,20 +1032,21 @@ void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
10321032
const struct iphdr *iph = (const struct iphdr *) skb->data;
10331033
struct flowi4 fl4;
10341034
struct rtable *rt;
1035-
struct dst_entry *dst;
1035+
struct dst_entry *odst = NULL;
10361036
bool new = false;
10371037

10381038
bh_lock_sock(sk);
1039-
rt = (struct rtable *) __sk_dst_get(sk);
1039+
odst = sk_dst_get(sk);
10401040

1041-
if (sock_owned_by_user(sk) || !rt) {
1041+
if (sock_owned_by_user(sk) || !odst) {
10421042
__ipv4_sk_update_pmtu(skb, sk, mtu);
10431043
goto out;
10441044
}
10451045

10461046
__build_flow_key(&fl4, sk, iph, 0, 0, 0, 0, 0);
10471047

1048-
if (!__sk_dst_check(sk, 0)) {
1048+
rt = (struct rtable *)odst;
1049+
if (odst->obsolete && odst->ops->check(odst, 0) == NULL) {
10491050
rt = ip_route_output_flow(sock_net(sk), &fl4, sk);
10501051
if (IS_ERR(rt))
10511052
goto out;
@@ -1055,8 +1056,7 @@ void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
10551056

10561057
__ip_rt_update_pmtu((struct rtable *) rt->dst.path, &fl4, mtu);
10571058

1058-
dst = dst_check(&rt->dst, 0);
1059-
if (!dst) {
1059+
if (!dst_check(&rt->dst, 0)) {
10601060
if (new)
10611061
dst_release(&rt->dst);
10621062

@@ -1068,10 +1068,11 @@ void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
10681068
}
10691069

10701070
if (new)
1071-
__sk_dst_set(sk, &rt->dst);
1071+
sk_dst_set(sk, &rt->dst);
10721072

10731073
out:
10741074
bh_unlock_sock(sk);
1075+
dst_release(odst);
10751076
}
10761077
EXPORT_SYMBOL_GPL(ipv4_sk_update_pmtu);
10771078

0 commit comments

Comments
 (0)