Update udp_tunnel{,6}_dst_lookup to always return noref dsts. The dst is only valid inside the RCU read-side critical section in which it was queried. The dst is fetched from the dst cache (as noref) or returned by a routing lookup operation, in which case its refcount is either stolen into the cache, or decremented shortly before returning (in case the cache can't be used). This is safe, since this code runs in an RCU read-side critical section, and the dst only lingers until the end of said section. Update all callers to use the new convention (of no longer calling dst_release, since all dsts are now noref). This affects the bareudp, geneve and vxlan tunnels. Signed-off-by: Marek Mietus --- drivers/net/bareudp.c | 6 ------ drivers/net/geneve.c | 13 ------------- drivers/net/vxlan/vxlan_core.c | 8 -------- net/ipv4/udp_tunnel_core.c | 6 ++++-- net/ipv6/ip6_udp_tunnel.c | 8 ++++++-- 5 files changed, 10 insertions(+), 31 deletions(-) diff --git a/drivers/net/bareudp.c b/drivers/net/bareudp.c index 92ee4a36f86f..456bc17c352d 100644 --- a/drivers/net/bareudp.c +++ b/drivers/net/bareudp.c @@ -364,11 +364,9 @@ static int bareudp_xmit_skb(struct sk_buff *skb, struct net_device *dev, !net_eq(bareudp->net, dev_net(bareudp->dev)), !test_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags), 0); - ip_rt_put(rt); return 0; free_dst: - dst_release(&rt->dst); return err; } @@ -434,11 +432,9 @@ static int bareudp6_xmit_skb(struct sk_buff *skb, struct net_device *dev, !test_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags), 0); - dst_release(dst); return 0; free_dst: - dst_release(dst); return err; } @@ -524,7 +520,6 @@ static int bareudp_fill_metadata_dst(struct net_device *dev, if (IS_ERR(rt)) return PTR_ERR(rt); - ip_rt_put(rt); info->key.u.ipv4.src = saddr; } else if (ip_tunnel_info_af(info) == AF_INET6) { struct dst_entry *dst; @@ -538,7 +533,6 @@ static int bareudp_fill_metadata_dst(struct net_device *dev, if (IS_ERR(dst)) return PTR_ERR(dst); - dst_release(dst); info->key.u.ipv6.src = saddr; } else { return -EINVAL; diff --git a/drivers/net/geneve.c b/drivers/net/geneve.c index 4216a8ffd591..8a918bd009dc 100644 --- a/drivers/net/geneve.c +++ b/drivers/net/geneve.c @@ -1268,7 +1268,6 @@ static int geneve_build_skb(struct dst_entry *dst, struct sk_buff *skb, return 0; free_dst: - dst_release(dst); return err; } @@ -1327,7 +1326,6 @@ static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev, geneve_build_gro_hint_opt(geneve, skb), netif_is_any_bridge_port(dev)); if (err < 0) { - dst_release(&rt->dst); return err; } else if (err) { struct ip_tunnel_info *info; @@ -1338,7 +1336,6 @@ static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev, unclone = skb_tunnel_info_unclone(skb); if (unlikely(!unclone)) { - dst_release(&rt->dst); return -ENOMEM; } @@ -1347,13 +1344,11 @@ static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev, } if (!pskb_may_pull(skb, ETH_HLEN)) { - dst_release(&rt->dst); return -EINVAL; } skb->protocol = eth_type_trans(skb, geneve->dev); __netif_rx(skb); - dst_release(&rt->dst); return -EMSGSIZE; } @@ -1396,7 +1391,6 @@ static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev, !net_eq(geneve->net, dev_net(geneve->dev)), !test_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags), 0); - ip_rt_put(rt); return 0; } @@ -1439,7 +1433,6 @@ static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev, geneve_build_gro_hint_opt(geneve, skb), netif_is_any_bridge_port(dev)); if (err < 0) { - dst_release(dst); return err; } else if (err) { struct ip_tunnel_info *info = skb_tunnel_info(skb); @@ -1449,7 +1442,6 @@ static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev, unclone = skb_tunnel_info_unclone(skb); if (unlikely(!unclone)) { - dst_release(dst); return -ENOMEM; } @@ -1458,13 +1450,11 @@ static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev, } if (!pskb_may_pull(skb, ETH_HLEN)) { - dst_release(dst); return -EINVAL; } skb->protocol = eth_type_trans(skb, geneve->dev); __netif_rx(skb); - dst_release(dst); return -EMSGSIZE; } @@ -1488,7 +1478,6 @@ static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev, !test_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags), 0); - dst_release(dst); return 0; } #endif @@ -1576,7 +1565,6 @@ static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) if (IS_ERR(rt)) return PTR_ERR(rt); - ip_rt_put(rt); info->key.u.ipv4.src = saddr; #if IS_ENABLED(CONFIG_IPV6) } else if (ip_tunnel_info_af(info) == AF_INET6) { @@ -1602,7 +1590,6 @@ static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) if (IS_ERR(dst)) return PTR_ERR(dst); - dst_release(dst); info->key.u.ipv6.src = saddr; #endif } else { diff --git a/drivers/net/vxlan/vxlan_core.c b/drivers/net/vxlan/vxlan_core.c index 4482a47dbe15..39fb2e6df6c4 100644 --- a/drivers/net/vxlan/vxlan_core.c +++ b/drivers/net/vxlan/vxlan_core.c @@ -2320,7 +2320,6 @@ static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev, vxlan->cfg.flags & VXLAN_F_LOCALBYPASS) { struct vxlan_dev *dst_vxlan; - dst_release(dst); dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni, addr_family, dst_port, vxlan->cfg.flags); @@ -2528,7 +2527,6 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, unclone->key.u.ipv4.dst = saddr; } vxlan_encap_bypass(skb, vxlan, vxlan, vni, false); - dst_release(ndst); goto out_unlock; } @@ -2545,7 +2543,6 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, pkey->u.ipv4.dst, tos, ttl, df, src_port, dst_port, xnet, !udp_sum, ipcb_flags); - ip_rt_put(rt); #if IS_ENABLED(CONFIG_IPV6) } else { struct vxlan_sock *sock6; @@ -2603,7 +2600,6 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, } vxlan_encap_bypass(skb, vxlan, vxlan, vni, false); - dst_release(ndst); goto out_unlock; } @@ -2621,7 +2617,6 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, &saddr, &pkey->u.ipv6.dst, tos, ttl, pkey->label, src_port, dst_port, !udp_sum, ip6cb_flags); - dst_release(ndst); #endif } vxlan_vnifilter_count(vxlan, vni, NULL, VXLAN_VNI_STATS_TX, pkt_len); @@ -2641,7 +2636,6 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, DEV_STATS_INC(dev, collisions); else if (err == -ENETUNREACH) DEV_STATS_INC(dev, tx_carrier_errors); - dst_release(ndst); DEV_STATS_INC(dev, tx_errors); vxlan_vnifilter_count(vxlan, vni, NULL, VXLAN_VNI_STATS_TX_ERRORS, 0); kfree_skb_reason(skb, reason); @@ -3248,7 +3242,6 @@ static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) &info->dst_cache); if (IS_ERR(rt)) return PTR_ERR(rt); - ip_rt_put(rt); } else { #if IS_ENABLED(CONFIG_IPV6) struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock); @@ -3264,7 +3257,6 @@ static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) &info->dst_cache); if (IS_ERR(ndst)) return PTR_ERR(ndst); - dst_release(ndst); #else /* !CONFIG_IPV6 */ return -EPFNOSUPPORT; #endif diff --git a/net/ipv4/udp_tunnel_core.c b/net/ipv4/udp_tunnel_core.c index b1f667c52cb2..c9c3fe8f0158 100644 --- a/net/ipv4/udp_tunnel_core.c +++ b/net/ipv4/udp_tunnel_core.c @@ -240,7 +240,7 @@ struct rtable *udp_tunnel_dst_lookup(struct sk_buff *skb, #ifdef CONFIG_DST_CACHE if (dst_cache) { - rt = dst_cache_get_ip4(dst_cache, saddr); + rt = dst_cache_get_ip4_rcu(dst_cache, saddr); if (rt) return rt; } @@ -269,8 +269,10 @@ struct rtable *udp_tunnel_dst_lookup(struct sk_buff *skb, } #ifdef CONFIG_DST_CACHE if (dst_cache) - dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr); + dst_cache_steal_ip4(dst_cache, &rt->dst, fl4.saddr); + else #endif + ip_rt_put(rt); *saddr = fl4.saddr; return rt; } diff --git a/net/ipv6/ip6_udp_tunnel.c b/net/ipv6/ip6_udp_tunnel.c index d58815db8182..94901935c9e9 100644 --- a/net/ipv6/ip6_udp_tunnel.c +++ b/net/ipv6/ip6_udp_tunnel.c @@ -130,6 +130,8 @@ EXPORT_SYMBOL_GPL(udp_tunnel6_xmit_skb); * * It returns a valid dst pointer and stores src address to be used in * tunnel in param saddr on success, else a pointer encoded error code. + * The returned dst pointer is noref and must only be used in the RCU + * read-side critical section in which it was queried. */ struct dst_entry *udp_tunnel6_dst_lookup(struct sk_buff *skb, @@ -147,7 +149,7 @@ struct dst_entry *udp_tunnel6_dst_lookup(struct sk_buff *skb, #ifdef CONFIG_DST_CACHE if (dst_cache) { - dst = dst_cache_get_ip6(dst_cache, saddr); + dst = dst_cache_get_ip6_rcu(dst_cache, saddr); if (dst) return dst; } @@ -175,8 +177,10 @@ struct dst_entry *udp_tunnel6_dst_lookup(struct sk_buff *skb, } #ifdef CONFIG_DST_CACHE if (dst_cache) - dst_cache_set_ip6(dst_cache, dst, &fl6.saddr); + dst_cache_steal_ip6(dst_cache, dst, &fl6.saddr); + else #endif + dst_release(dst); *saddr = fl6.saddr; return dst; } -- 2.51.0