tcp: receive path optimizations
Subsystems (6)
cover tcp: receive path optimizations
From: Eric Dumazet <[email protected]>
This series aims to reduce cache line misses in RX path.
I am still working on better cache locality in tcp_sock but this will wait few more weeks.
Eric Dumazet (9):
tcp: move inet->rx_dst_ifindex to sk->sk_rx_dst_ifindex
ipv6: move inet6_sk(sk)->rx_dst_cookie to sk->sk_rx_dst_cookie
net: avoid dirtying sk->sk_napi_id
net: avoid dirtying sk->sk_rx_queue_mapping
ipv6: annotate data races around np->min_hopcount
ipv6: guard IPV6_MINHOPCOUNT with a static key
ipv4: annotate data races arount inet->min_ttl
ipv4: guard IP_MINTTL with a static key
ipv6/tcp: small drop monitor changes
include/linux/ipv6.h | 1 - include/net/busy_poll.h | 3 ++- include/net/inet_sock.h | 3 +-- include/net/ip.h | 2 ++ include/net/ipv6.h | 1 + include/net/sock.h | 11 +++++++---- net/ipv4/ip_sockglue.c | 11 ++++++++++- net/ipv4/tcp_ipv4.c | 25 ++++++++++++++++--------- net/ipv6/ipv6_sockglue.c | 11 ++++++++++- net/ipv6/tcp_ipv6.c | 35 +++++++++++++++++++++-------------- net/ipv6/udp.c | 4 ++-- 11 files changed, 72 insertions(+), 35 deletions(-)
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
PATCH 1/2 net: sched: fix logic error in qdisc_run_begin()
From: Eric Dumazet <[email protected]>
For non TCQ_F_NOLOCK qdisc, qdisc_run_begin() tries to set __QDISC_STATE_RUNNING and should return true if the bit was not set.
test_and_set_bit() returns old bit value, therefore we need to invert.
--- include/net/sch_generic.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/net/sch_generic.h +1 -1
@@ -217,7 +217,7 @@ static inline bool qdisc_run_begin(struct Qdisc *qdisc)*/ return spin_trylock (&qdisc->seqlock); } -return test_and_set_bit (__QDISC_STATE_RUNNING, &qdisc->state); +return !test_and_set_bit (__QDISC_STATE_RUNNING, &qdisc->state); }static inline void qdisc_run_end (struct Qdisc *qdisc)
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
Re: [PATCH net-next 1/2] net: sched: fix logic error in qdisc_run_begin()
Quoted 6 lines · Eric Dumazet
From: Eric Dumazet <[email protected]>
For non
TCQ_F_NOLOCKqdisc,qdisc_run_begin()tries to set__QDISC_STATE_RUNNINGand should return true if the bit was not set.
Quoted 7 lines
test_and_set_bit()returns old bit value, therefore we need to invert.Fixes 29cbcd858283 ("net: sched: Remove Qdisc::running sequence counter")Signed-off-by Eric Dumazet <[email protected]>Cc Ahmed S. Darwish <[email protected]>Cc Sebastian Andrzej Siewior <[email protected]>---
Please disregard, I have accidentally resent this already merged patch.
PATCH 1/9 tcp: move inet->rx_dst_ifindex to sk->sk_rx_dst_ifindex
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
Increase cache locality by moving rx_dst_ifindex next to sk->sk_rx_dst
This is part of an effort to reduce cache line misses in TCP fast path.
This removes one cache line miss in early demux.
--- include/net/inet_sock.h | 3 +-- include/net/sock.h | 3 +++ net/ipv4/tcp_ipv4.c | 6 +++--- net/ipv6/tcp_ipv6.c | 6 +++--- 4 files changed, 10 insertions(+), 8 deletions(-)
include/net/inet_sock.h +1 -2
@@ -207,11 +207,10 @@ struct inet_sock {__be32 inet_saddr;__s16 uc_ttl;__u16 cmsg_flags; +struct ip_options_rcu __rcu *inet_opt;__be16 inet_sport;__u16 inet_id; -struct ip_options_rcu __rcu *inet_opt; -int rx_dst_ifindex;__u8 tos;__u8 min_ttl;__u8 mc_ttl;
include/net/sock.h +3 -0
@@ -259,6 +259,7 @@ struct bpf_local_storage;* @sk_rcvbuf: size of receive buffer in bytes * @sk_wq: sock wait queue and async head * @sk_rx_dst: receive input route used by early demux +* @sk_rx_dst_ifindex: ifindex for @sk_rx_dst * @sk_dst_cache: destination cache * @sk_dst_pending_confirm: need to confirm neighbour * @sk_policy: flow policy
@@ -430,6 +431,8 @@ struct sock {struct xfrm_policy __rcu *sk_policy[2 ];#endif struct dst_entry *sk_rx_dst; +int sk_rx_dst_ifindex; +struct dst_entry __rcu *sk_dst_cache;atomic_t sk_omem_alloc;int sk_sndbuf;
net/ipv4/tcp_ipv4.c +3 -3
@@ -1684,7 +1684,7 @@ int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)sock_rps_save_rxhash (sk, skb);sk_mark_napi_id (sk, skb);if (dst) { -if (inet_sk (sk)->rx_dst_ifindex != skb->skb_iif || +if (sk->sk_rx_dst_ifindex != skb->skb_iif || !INDIRECT_CALL_1 (dst->ops->check, ipv4_dst_check, dst,0 )) {dst_release (dst);
@@ -1769,7 +1769,7 @@ int tcp_v4_early_demux(struct sk_buff *skb)if (dst) dst =dst_check (dst,0 );if (dst && -inet_sk (sk)->rx_dst_ifindex == skb->skb_iif) + sk->sk_rx_dst_ifindex == skb->skb_iif)skb_dst_set_noref (skb, dst); } }
@@ -2176,7 +2176,7 @@ void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb)if (dst &&dst_hold_safe (dst)) { sk->sk_rx_dst = dst; -inet_sk (sk)->rx_dst_ifindex = skb->skb_iif; + sk->sk_rx_dst_ifindex = skb->skb_iif; } }EXPORT_SYMBOL (inet_sk_rx_dst_set);
net/ipv6/tcp_ipv6.c +3 -3
@@ -108,7 +108,7 @@ static void inet6_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb)const struct rt6_info *rt = (const struct rt6_info *)dst; sk->sk_rx_dst = dst; -inet_sk (sk)->rx_dst_ifindex = skb->skb_iif; + sk->sk_rx_dst_ifindex = skb->skb_iif;tcp_inet6_sk (sk)->rx_dst_cookie =rt6_get_cookie (rt); } }
@@ -1506,7 +1506,7 @@ static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)sock_rps_save_rxhash (sk, skb);sk_mark_napi_id (sk, skb);if (dst) { -if (inet_sk (sk)->rx_dst_ifindex != skb->skb_iif || +if (sk->sk_rx_dst_ifindex != skb->skb_iif ||INDIRECT_CALL_1 (dst->ops->check, ip6_dst_check, dst, np->rx_dst_cookie) ==NULL ) {dst_release (dst);
@@ -1871,7 +1871,7 @@ INDIRECT_CALLABLE_SCOPE void tcp_v6_early_demux(struct sk_buff *skb)if (dst) dst =dst_check (dst,tcp_inet6_sk (sk)->rx_dst_cookie);if (dst && -inet_sk (sk)->rx_dst_ifindex == skb->skb_iif) + sk->sk_rx_dst_ifindex == skb->skb_iif)skb_dst_set_noref (skb, dst); } }
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
PATCH 2/9 ipv6: move inet6_sk(sk)->rx_dst_cookie to sk->sk_rx_dst_cookie
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
Increase cache locality by moving rx_dst_coookie next to sk->sk_rx_dst
This removes one or two cache line misses in IPv6 early demux (TCP/UDP)
--- include/linux/ipv6.h | 1 - include/net/sock.h | 2 ++ net/ipv6/tcp_ipv6.c | 6 +++--- net/ipv6/udp.c | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-)
include/linux/ipv6.h +0 -1
@@ -282,7 +282,6 @@ struct ipv6_pinfo {__be32 rcv_flowinfo;__u32 dst_cookie; -__u32 rx_dst_cookie;struct ipv6_mc_socklist __rcu *ipv6_mc_list;struct ipv6_ac_socklist *ipv6_ac_list;
include/net/sock.h +2 -0
@@ -260,6 +260,7 @@ struct bpf_local_storage;* @sk_wq: sock wait queue and async head * @sk_rx_dst: receive input route used by early demux * @sk_rx_dst_ifindex: ifindex for @sk_rx_dst +* @sk_rx_dst_cookie: cookie for @sk_rx_dst * @sk_dst_cache: destination cache * @sk_dst_pending_confirm: need to confirm neighbour * @sk_policy: flow policy
@@ -432,6 +433,7 @@ struct sock {#endif struct dst_entry *sk_rx_dst;int sk_rx_dst_ifindex; +u32 sk_rx_dst_cookie;struct dst_entry __rcu *sk_dst_cache;atomic_t sk_omem_alloc;
net/ipv6/tcp_ipv6.c +3 -3
@@ -109,7 +109,7 @@ static void inet6_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb) sk->sk_rx_dst = dst; sk->sk_rx_dst_ifindex = skb->skb_iif; -tcp_inet6_sk (sk)->rx_dst_cookie =rt6_get_cookie (rt); + sk->sk_rx_dst_cookie =rt6_get_cookie (rt); } }
@@ -1508,7 +1508,7 @@ static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)if (dst) {if (sk->sk_rx_dst_ifindex != skb->skb_iif ||INDIRECT_CALL_1 (dst->ops->check, ip6_dst_check, - dst, np->rx_dst_cookie) ==NULL ) { + dst, sk->sk_rx_dst_cookie) ==NULL ) {dst_release (dst); sk->sk_rx_dst =NULL ; }
@@ -1869,7 +1869,7 @@ INDIRECT_CALLABLE_SCOPE void tcp_v6_early_demux(struct sk_buff *skb)struct dst_entry *dst =READ_ONCE (sk->sk_rx_dst);if (dst) - dst =dst_check (dst,tcp_inet6_sk (sk)->rx_dst_cookie); + dst =dst_check (dst, sk->sk_rx_dst_cookie);if (dst && sk->sk_rx_dst_ifindex == skb->skb_iif)skb_dst_set_noref (skb, dst);
net/ipv6/udp.c +2 -2
@@ -884,7 +884,7 @@ static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)if (udp_sk_rx_dst_set (sk, dst)) {const struct rt6_info *rt = (const struct rt6_info *)dst; -inet6_sk (sk)->rx_dst_cookie =rt6_get_cookie (rt); + sk->sk_rx_dst_cookie =rt6_get_cookie (rt); } }
@@ -1073,7 +1073,7 @@ INDIRECT_CALLABLE_SCOPE void udp_v6_early_demux(struct sk_buff *skb) dst =READ_ONCE (sk->sk_rx_dst);if (dst) - dst =dst_check (dst,inet6_sk (sk)->rx_dst_cookie); + dst =dst_check (dst, sk->sk_rx_dst_cookie);if (dst) {/* set noref for now. * any place which wants to hold dst has to call
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
PATCH 3/9 net: avoid dirtying sk->sk_napi_id
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
sk_napi_id is located in a cache line that can be kept read mostly.
--- include/net/busy_poll.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/net/busy_poll.h +2 -1
@@ -130,7 +130,8 @@ static inline void skb_mark_napi_id(struct sk_buff *skb,static inline void sk_mark_napi_id (struct sock *sk,const struct sk_buff *skb) {#ifdef CONFIG_NET_RX_BUSY_POLL -WRITE_ONCE (sk->sk_napi_id, skb->napi_id); +if (unlikely (READ_ONCE (sk->sk_napi_id) != skb->napi_id)) +WRITE_ONCE (sk->sk_napi_id, skb->napi_id);#endif sk_rx_queue_set (sk, skb); }
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
PATCH 2/2 net: sched: remove one pair of atomic operations
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
__QDISC_STATE_RUNNING is only set/cleared from contexts owning qdisc lock.
Thus we can use less expensive bit operations, as we were doing
before commit f9eb8aea2a1e ("net_sched: transform qdisc running bit into a seqcount")
--- include/net/sch_generic.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
include/net/sch_generic.h +8 -4
@@ -38,10 +38,13 @@ enum qdisc_state_t { __QDISC_STATE_DEACTIVATED, __QDISC_STATE_MISSED, __QDISC_STATE_DRAINING, +}; + +enum qdisc_state2_t {/* Only for !TCQ_F_NOLOCK qdisc. Never access it directly. * Use qdisc_run_begin/end() or qdisc_is_running() instead. */ - __QDISC_STATE_RUNNING, + __QDISC_STATE2_RUNNING, };#define QDISC_STATE_MISSED BIT(__QDISC_STATE_MISSED)
@@ -114,6 +117,7 @@ struct Qdisc {struct gnet_stats_basic_sync bstats;struct gnet_stats_queue qstats;unsigned long state; +unsigned long state2;/* must be written under qdisc spinlock */ struct Qdisc *next_sched;struct sk_buff_head skb_bad_txq;
@@ -154,7 +158,7 @@ static inline bool qdisc_is_running(struct Qdisc *qdisc) {if (qdisc->flags &TCQ_F_NOLOCK )return spin_is_locked (&qdisc->seqlock); -return test_bit (__QDISC_STATE_RUNNING, &qdisc->state); +return test_bit (__QDISC_STATE2_RUNNING, &qdisc->state2); }static inline bool nolock_qdisc_is_empty (const struct Qdisc *qdisc)
@@ -217,7 +221,7 @@ static inline bool qdisc_run_begin(struct Qdisc *qdisc)*/ return spin_trylock (&qdisc->seqlock); } -return !test_and_set_bit (__QDISC_STATE_RUNNING, &qdisc->state); +return !__test_and_set_bit (__QDISC_STATE2_RUNNING, &qdisc->state2); }static inline void qdisc_run_end (struct Qdisc *qdisc)
@@ -229,7 +233,7 @@ static inline void qdisc_run_end(struct Qdisc *qdisc) &qdisc->state)))__netif_schedule (qdisc); }else { -clear_bit (__QDISC_STATE_RUNNING, &qdisc->state); +__clear_bit (__QDISC_STATE2_RUNNING, &qdisc->state2); } }
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
Re: [PATCH net-next 2/2] net: sched: remove one pair of atomic operations
Quoted 5 lines · Eric Dumazet
From: Eric Dumazet <[email protected]>
__QDISC_STATE_RUNNINGis only set/cleared from contexts owning qdisc lock.
Quoted 8 lines
Thus we can use less expensive bit operations, as we were doing
before commitf9eb8aea2a1e("net_sched: transform qdisc running bit into a seqcount")Fixes 29cbcd858283 ("net: sched: Remove Qdisc::running sequence counter")Signed-off-by Eric Dumazet <[email protected]>Cc Ahmed S. Darwish <[email protected]>Cc Sebastian Andrzej Siewior <[email protected]>---
Please disregard, I have accidentally resent this patch while sending another unrelated patch series.
PATCH 4/9 net: avoid dirtying sk->sk_rx_queue_mapping
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
sk_rx_queue_mapping is located in a cache line that should be kept read mostly.
--- include/net/sock.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
include/net/sock.h +2 -4
@@ -1916,10 +1916,8 @@ static inline void sk_rx_queue_set(struct sock *sk, const struct sk_buff *skb)if (skb_rx_queue_recorded (skb)) {u16 rx_queue =skb_get_rx_queue (skb); -if (WARN_ON_ONCE (rx_queue ==NO_QUEUE_MAPPING )) -return ; - - sk->sk_rx_queue_mapping = rx_queue; +if (unlikely (READ_ONCE (sk->sk_rx_queue_mapping) != rx_queue)) +WRITE_ONCE (sk->sk_rx_queue_mapping, rx_queue); }#endif }
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
PATCH 5/9 ipv6: annotate data races around np->min_hopcount
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
No report yet from KCSAN, yet worth documenting the races.
--- net/ipv6/ipv6_sockglue.c | 5 ++++- net/ipv6/tcp_ipv6.c | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-)
net/ipv6/ipv6_sockglue.c +4 -1
@@ -950,7 +950,10 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname, goto e_inval;if (val <0 || val >255 ) goto e_inval; - np->min_hopcount = val; +/* tcp_v6_err() and tcp_v6_rcv() might read min_hopcount +* while we are changing it. +*/ +WRITE_ONCE (np->min_hopcount, val); retv =0 ;break ;case IPV6_DONTFRAG :
net/ipv6/tcp_ipv6.c +4 -2
@@ -414,7 +414,8 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,if (sk->sk_state ==TCP_CLOSE ) goto out; -if (ipv6_hdr (skb)->hop_limit <tcp_inet6_sk (sk)->min_hopcount) { +/* min_hopcount can be changed concurrently from do_ipv6_setsockopt() */ +if (ipv6_hdr (skb)->hop_limit <READ_ONCE (tcp_inet6_sk (sk)->min_hopcount)) {__NET_INC_STATS (net,LINUX_MIB_TCPMINTTLDROP ); goto out; }
@@ -1723,7 +1724,8 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)return 0 ; } } -if (hdr->hop_limit <tcp_inet6_sk (sk)->min_hopcount) { +/* min_hopcount can be changed concurrently from do_ipv6_setsockopt() */ +if (hdr->hop_limit <READ_ONCE (tcp_inet6_sk (sk)->min_hopcount)) {__NET_INC_STATS (net,LINUX_MIB_TCPMINTTLDROP ); goto discard_and_relse; }
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
PATCH 6/9 ipv6: guard IPV6_MINHOPCOUNT with a static key
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
RFC 5082 IPV6_MINHOPCOUNT is rarely used on hosts.
Add a static key to remove from TCP fast path useless code, and potential cache line miss to fetch tcp_inet6_sk(sk)->min_hopcount
Note that once ip6_min_hopcount static key has been enabled, it stays enabled until next boot.
--- include/net/ipv6.h | 1 + net/ipv6/ipv6_sockglue.c | 6 ++++++ net/ipv6/tcp_ipv6.c | 21 +++++++++++++-------- 3 files changed, 20 insertions(+), 8 deletions(-)
include/net/ipv6.h +1 -0
@@ -1092,6 +1092,7 @@ struct in6_addr *fl6_update_dst(struct flowi6 *fl6,/* * socket options (ipv6_sockglue.c) */ +DECLARE_STATIC_KEY_FALSE (ip6_min_hopcount);int ipv6_setsockopt (struct sock *sk,int level,int optname,sockptr_t optval,unsigned int optlen);
net/ipv6/ipv6_sockglue.c +6 -0
@@ -55,6 +55,8 @@struct ip6_ra_chain *ip6_ra_chain;DEFINE_RWLOCK (ip6_ra_lock); +DEFINE_STATIC_KEY_FALSE (ip6_min_hopcount); +int ip6_ra_control (struct sock *sk,int sel) {struct ip6_ra_chain *ra, *new_ra, **rap;
@@ -950,6 +952,10 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname, goto e_inval;if (val <0 || val >255 ) goto e_inval; + +if (val) +static_branch_enable (&ip6_min_hopcount); +/* tcp_v6_err() and tcp_v6_rcv() might read min_hopcount * while we are changing it. */
net/ipv6/tcp_ipv6.c +13 -8
@@ -414,10 +414,12 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,if (sk->sk_state ==TCP_CLOSE ) goto out; -/* min_hopcount can be changed concurrently from do_ipv6_setsockopt() */ -if (ipv6_hdr (skb)->hop_limit <READ_ONCE (tcp_inet6_sk (sk)->min_hopcount)) { -__NET_INC_STATS (net,LINUX_MIB_TCPMINTTLDROP ); - goto out; +if (static_branch_unlikely (&ip6_min_hopcount)) { +/* min_hopcount can be changed concurrently from do_ipv6_setsockopt() */ +if (ipv6_hdr (skb)->hop_limit <READ_ONCE (tcp_inet6_sk (sk)->min_hopcount)) { +__NET_INC_STATS (net,LINUX_MIB_TCPMINTTLDROP ); + goto out; + } } tp =tcp_sk (sk);
@@ -1724,10 +1726,13 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)return 0 ; } } -/* min_hopcount can be changed concurrently from do_ipv6_setsockopt() */ -if (hdr->hop_limit <READ_ONCE (tcp_inet6_sk (sk)->min_hopcount)) { -__NET_INC_STATS (net,LINUX_MIB_TCPMINTTLDROP ); - goto discard_and_relse; + +if (static_branch_unlikely (&ip6_min_hopcount)) { +/* min_hopcount can be changed concurrently from do_ipv6_setsockopt() */ +if (hdr->hop_limit <READ_ONCE (tcp_inet6_sk (sk)->min_hopcount)) { +__NET_INC_STATS (net,LINUX_MIB_TCPMINTTLDROP ); + goto discard_and_relse; + } }if (!xfrm6_policy_check (sk,XFRM_POLICY_IN , skb))
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
PATCH 7/9 ipv4: annotate data races arount inet->min_ttl
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
No report yet from KCSAN, yet worth documenting the races.
--- net/ipv4/ip_sockglue.c | 5 ++++- net/ipv4/tcp_ipv4.c | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-)
net/ipv4/ip_sockglue.c +4 -1
@@ -1352,7 +1352,10 @@ static int do_ip_setsockopt(struct sock *sk, int level, int optname, goto e_inval;if (val <0 || val >255 ) goto e_inval; - inet->min_ttl = val; +/* tcp_v4_err() and tcp_v4_rcv() might read min_ttl +* while we are changint it. +*/ +WRITE_ONCE (inet->min_ttl, val);break ;default :
net/ipv4/tcp_ipv4.c +5 -2
@@ -508,7 +508,8 @@ int tcp_v4_err(struct sk_buff *skb, u32 info)if (sk->sk_state ==TCP_CLOSE ) goto out; -if (unlikely (iph->ttl <inet_sk (sk)->min_ttl)) { +/* min_ttl can be changed concurrently from do_ip_setsockopt() */ +if (unlikely (iph->ttl <READ_ONCE (inet_sk (sk)->min_ttl))) {__NET_INC_STATS (net,LINUX_MIB_TCPMINTTLDROP ); goto out; }
@@ -2049,7 +2050,9 @@ int tcp_v4_rcv(struct sk_buff *skb)return 0 ; } } -if (unlikely (iph->ttl <inet_sk (sk)->min_ttl)) { + +/* min_ttl can be changed concurrently from do_ip_setsockopt() */ +if (unlikely (iph->ttl <READ_ONCE (inet_sk (sk)->min_ttl))) {__NET_INC_STATS (net,LINUX_MIB_TCPMINTTLDROP ); goto discard_and_relse; }
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
PATCH 8/9 ipv4: guard IP_MINTTL with a static key
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
RFC 5082 IP_MINTTL option is rarely used on hosts.
Add a static key to remove from TCP fast path useless code, and potential cache line miss to fetch inet_sk(sk)->min_ttl
Note that once ip4_min_ttl static key has been enabled, it stays enabled until next boot.
--- include/net/ip.h | 2 ++ net/ipv4/ip_sockglue.c | 6 ++++++ net/ipv4/tcp_ipv4.c | 20 ++++++++++++-------- 3 files changed, 20 insertions(+), 8 deletions(-)
include/net/ip.h +2 -0
@@ -24,6 +24,7 @@#include <linux/skbuff.h> #include <linux/jhash.h> #include <linux/sockptr.h> +#include <linux/static_key.h> #include <net/inet_sock.h> #include <net/route.h>
@@ -750,6 +751,7 @@ void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk, struct sk_buff *skb, int tlen, int offset);int ip_cmsg_send (struct sock *sk,struct msghdr *msg,struct ipcm_cookie *ipc,bool allow_ipv6); +DECLARE_STATIC_KEY_FALSE (ip4_min_ttl);int ip_setsockopt (struct sock *sk,int level,int optname,sockptr_t optval,unsigned int optlen); int ip_getsockopt(struct sock *sk, int level, int optname, char __user *optval,
net/ipv4/ip_sockglue.c +6 -0
@@ -886,6 +886,8 @@ static int compat_ip_mcast_join_leave(struct sock *sk, int optname,return ip_mc_leave_group (sk, &mreq); } +DEFINE_STATIC_KEY_FALSE (ip4_min_ttl); +static int do_ip_setsockopt (struct sock *sk,int level,int optname,sockptr_t optval,unsigned int optlen) {
@@ -1352,6 +1354,10 @@ static int do_ip_setsockopt(struct sock *sk, int level, int optname, goto e_inval;if (val <0 || val >255 ) goto e_inval; + +if (val) +static_branch_enable (&ip4_min_ttl); +/* tcp_v4_err() and tcp_v4_rcv() might read min_ttl * while we are changint it. */
net/ipv4/tcp_ipv4.c +12 -8
@@ -508,10 +508,12 @@ int tcp_v4_err(struct sk_buff *skb, u32 info)if (sk->sk_state ==TCP_CLOSE ) goto out; -/* min_ttl can be changed concurrently from do_ip_setsockopt() */ -if (unlikely (iph->ttl <READ_ONCE (inet_sk (sk)->min_ttl))) { -__NET_INC_STATS (net,LINUX_MIB_TCPMINTTLDROP ); - goto out; +if (static_branch_unlikely (&ip4_min_ttl)) { +/* min_ttl can be changed concurrently from do_ip_setsockopt() */ +if (unlikely (iph->ttl <READ_ONCE (inet_sk (sk)->min_ttl))) { +__NET_INC_STATS (net,LINUX_MIB_TCPMINTTLDROP ); + goto out; + } } tp =tcp_sk (sk);
@@ -2051,10 +2053,12 @@ int tcp_v4_rcv(struct sk_buff *skb) } } -/* min_ttl can be changed concurrently from do_ip_setsockopt() */ -if (unlikely (iph->ttl <READ_ONCE (inet_sk (sk)->min_ttl))) { -__NET_INC_STATS (net,LINUX_MIB_TCPMINTTLDROP ); - goto discard_and_relse; +if (static_branch_unlikely (&ip4_min_ttl)) { +/* min_ttl can be changed concurrently from do_ip_setsockopt() */ +if (unlikely (iph->ttl <READ_ONCE (inet_sk (sk)->min_ttl))) { +__NET_INC_STATS (net,LINUX_MIB_TCPMINTTLDROP ); + goto discard_and_relse; + } }if (!xfrm4_policy_check (sk,XFRM_POLICY_IN , skb))
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
PATCH 9/9 ipv6/tcp: small drop monitor changes
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
Two kfree_skb() calls must be replaced by consume_skb() for skbs that are not technically dropped.
--- net/ipv6/tcp_ipv6.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv6/tcp_ipv6.c +2 -2
@@ -572,7 +572,7 @@ static int tcp_v6_send_synack(const struct sock *sk, struct dst_entry *dst,static void tcp_v6_reqsk_destructor (struct request_sock *req) {kfree (inet_rsk (req)->ipv6_opt); -kfree_skb (inet_rsk (req)->pktopts); +consume_skb (inet_rsk (req)->pktopts); }#ifdef CONFIG_TCP_MD5SIG
@@ -1591,7 +1591,7 @@ static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb) } } -kfree_skb (opt_skb); +consume_skb (opt_skb);return 0 ; }
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
Quoted 8 lines · Eric Dumazet
From: Eric Dumazet <[email protected]>
This series aims to reduce cache line misses in RX path.
I am still working on better cache locality in
tcp_sockbut this will wait few more weeks.
Quoted 10 lines
Eric Dumazet (9):
tcp: moveinet->rx_dst_ifindextosk->sk_rx_dst_ifindex
ipv6: move inet6_sk(sk)->rx_dst_cookie tosk->sk_rx_dst_cookie
net: avoid dirtyingsk->sk_napi_id
net: avoid dirtyingsk->sk_rx_queue_mapping
ipv6: annotate data races aroundnp->min_hopcount
ipv6: guardIPV6_MINHOPCOUNTwith a static key
ipv4: annotate data races arountinet->min_ttl
ipv4: guardIP_MINTTLwith a static key
ipv6/tcp: small drop monitor changes
Very nice patch series! The IP_MINTTL patch is an excellent find. I wonder how many more of these we have. Thank you, Eric!
Quoted 16 lines
include/linux/ipv6.h | 1 - include/net/busy_poll.h | 3 ++- include/net/inet_sock.h | 3 +-- include/net/ip.h | 2 ++ include/net/ipv6.h | 1 + include/net/sock.h | 11 +++++++---- net/ipv4/ip_sockglue.c | 11 ++++++++++- net/ipv4/tcp_ipv4.c | 25 ++++++++++++++++--------- net/ipv6/ipv6_sockglue.c | 11 ++++++++++- net/ipv6/tcp_ipv6.c | 35 +++++++++++++++++++++-------------- net/ipv6/udp.c | 4 ++-- 11 files changed, 72 insertions(+), 35 deletions(-)--
2.33.0.1079.g6e70778dc9-goog