tcp: add three static keys
Subsystems (7)
cover tcp: add three static keys
Recent addition of per TCP socket rx/tx cache brought regressions for some workloads, as reported by Feng Tang.
It seems better to make them opt-in, before we adopt better heuristics.
The last patch adds high_order_alloc_disable sysctl to ask TCP sendmsg() to exclusively use order-0 allocations, as mm layer has specific optimizations.
Eric Dumazet (4):
sysctl: define proc_do_static_key()
tcp: add tcp_rx_skb_cache sysctl
tcp: add tcp_tx_skb_cache sysctl
net: add high_order_alloc_disable sysctl/static key
Documentation/networking/ip-sysctl.txt | 8 +++++ include/linux/bpf.h | 1 - include/linux/sysctl.h | 3 ++ include/net/sock.h | 12 ++++--- kernel/bpf/core.c | 1 - kernel/sysctl.c | 44 ++++++++++++++------------ net/core/sock.c | 4 ++- net/core/sysctl_net_core.c | 7 ++++ net/ipv4/sysctl_net_ipv4.c | 17 ++++++++++ 9 files changed, 68 insertions(+), 29 deletions(-)
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
PATCH 1/4 sysctl: define proc_do_static_key()
Convert proc_dointvec_minmax_bpf_stats() into a more generic helper, since we are going to use jump labels more often.
Note that sysctl_bpf_stats_enabled is removed, since it is no longer needed/used.
--- include/linux/bpf.h | 1 - include/linux/sysctl.h | 3 +++ kernel/bpf/core.c | 1 - kernel/sysctl.c | 44 ++++++++++++++++++++++-------------------- 4 files changed, 26 insertions(+), 23 deletions(-)
include/linux/bpf.h +0 -1
@@ -600,7 +600,6 @@ void bpf_map_area_free(void *base);void bpf_map_init_from_attr (struct bpf_map *map,union bpf_attr *attr);extern int sysctl_unprivileged_bpf_disabled; -extern int sysctl_bpf_stats_enabled;int bpf_map_new_fd (struct bpf_map *map,int flags);int bpf_prog_new_fd (struct bpf_prog *prog);
include/linux/sysctl.h +3 -0
@@ -63,6 +63,9 @@ extern int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int, void __user *, size_t *, loff_t *);extern int proc_do_large_bitmap (struct ctl_table *,int ,void __user *,size_t *,loff_t *); +extern int proc_do_static_key (struct ctl_table *table,int write, +void __user *buffer,size_t *lenp, +loff_t *ppos);/* * Register a set of sysctl names by calling register_sysctl_table
kernel/bpf/core.c +0 -1
@@ -2097,7 +2097,6 @@ int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,DEFINE_STATIC_KEY_FALSE (bpf_stats_enabled_key);EXPORT_SYMBOL (bpf_stats_enabled_key); -int sysctl_bpf_stats_enabled__read_mostly ;/* All definitions of tracepoints related to BPF. */ #define CREATE_TRACE_POINTS
kernel/sysctl.c +23 -21
72 lines not on this page · raw message
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
Re: [PATCH net 1/4] sysctl: define proc_do_static_key()
Quoted 69 lines · Eric Dumazet
Convert
proc_dointvec_minmax_bpf_stats()into a more generic helper, since we are going to use jump labels more often.Note that
sysctl_bpf_stats_enabledis removed, since it is no longer needed/used.Signed-off-by Eric Dumazet <[email protected]>--- include/linux/bpf.h | 1 - include/linux/sysctl.h | 3 +++ kernel/bpf/core.c | 1 - kernel/sysctl.c | 44 ++++++++++++++++++++++-------------------- 4 files changed, 26 insertions(+), 23 deletions(-)include/linux/bpf.h +0
-1@@ -600,7 +600,6 @@ void bpf_map_area_free(void *base);void bpf_map_init_from_attr (struct bpf_map *map,union bpf_attr *attr);extern int sysctl_unprivileged_bpf_disabled; -extern int sysctl_bpf_stats_enabled;int bpf_map_new_fd (struct bpf_map *map,int flags);int bpf_prog_new_fd (struct bpf_prog *prog);include/linux/sysctl.h +3
-0@@ -63,6 +63,9 @@ extern int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int, void __user *, size_t *, loff_t *);extern int proc_do_large_bitmap (struct ctl_table *,int ,void __user *,size_t *,loff_t *); +extern int proc_do_static_key (struct ctl_table *table,int write, +void __user *buffer,size_t *lenp, +loff_t *ppos);/* * Register a set of sysctl names by calling register_sysctl_table kernel/bpf/core.c +0
-1@@ -2097,7 +2097,6 @@ int __weak skb_copy_bits(const struct sk_buff *skb, int offset, void *to,DEFINE_STATIC_KEY_FALSE (bpf_stats_enabled_key);EXPORT_SYMBOL (bpf_stats_enabled_key); -int sysctl_bpf_stats_enabled__read_mostly ;/* All definitions of tracepoints related to BPF. */ #define CREATE_TRACE_POINTS kernel/sysctl.c +0
-5@@ -230,11 +230,6 @@ static int proc_dostring_coredump(struct ctl_table *table, int write,#endif static int proc_dopipe_max_size (struct ctl_table *table,int write,void __user *buffer,size_t *lenp,loff_t *ppos); -#ifdef CONFIG_BPF_SYSCALL -static int proc_dointvec_minmax_bpf_stats (struct ctl_table *table,int write, -void __user *buffer,size_t *lenp, -loff_t *ppos); -#endif #ifdef CONFIG_MAGIC_SYSRQ /* Note: sysrq code uses its own private copy */
kernel/sysctl.c static struct ctl_table kern_table[] = { · line 1252
Quoted 7 lines
kernel/sysctl.c +2
-2@@ -1253,12 +1248,10 @@ static struct ctl_table kern_table[] = { }, { .procname = "bpf_stats_enabled", - .data = &sysctl_bpf_stats_enabled, - .maxlen = sizeof(sysctl_bpf_stats_enabled), + .data = &bpf_stats_enabled_key.key, + .maxlen = sizeof(bpf_stats_enabled_key),
maxlen is ignored by proc_do_static_key(), right?
Great idea, btw. I like the series.
Re: [PATCH net 1/4] sysctl: define proc_do_static_key()
Quoted 3 lines · Alexei Starovoitov
On Fri, Jun 14, 2019 at 04:22:18PM -0700, Eric Dumazet wrote:
maxlen is ignored by
proc_do_static_key(), right?
That is right, I was not sure putting a zero or sizeof(int) would make sense here.
Using sizeof(...key) is consistent with other sysctls, even of proc_do_static_key() uses a temporary structure and a temporary integer in its current implementation.
Quoted 3 lines
Great idea, btw. I like the series.
Thanks :)
Alexei Starovoitov
Acked-byRe: [PATCH net 1/4] sysctl: define proc_do_static_key()
Quoted 10 lines · Eric Dumazet
Quoted 3 lines · Alexei Starovoitov
On Fri, Jun 14, 2019 at 04:22:18PM -0700, Eric Dumazet wrote:
maxlen is ignored by
proc_do_static_key(), right?That is right, I was not sure putting a zero or sizeof(int) would make sense here.
Using sizeof(...key) is consistent with other sysctls,
yes. that makes sense. I was just curious whether I missed something.
Quoted 2 lines
even of
proc_do_static_key()uses a temporary structure and a temporary integer in its current implementation.
yep.
PATCH 2/4 tcp: add tcp_rx_skb_cache sysctl
In reply to Eric DumazetInstead of relying on rps_needed, it is safer to use a separate static key, since we do not want to enable TCP rx_skb_cache by default. This feature can cause huge increase of memory usage on hosts with millions of sockets.
--- Documentation/networking/ip-sysctl.txt | 8 ++++++++ include/net/sock.h | 6 ++---- net/ipv4/sysctl_net_ipv4.c | 9 +++++++++ 3 files changed, 19 insertions(+), 4 deletions(-)
Documentation/networking/ip-sysctl.txt +8 -0
@@ -772,6 +772,14 @@ tcp_challenge_ack_limit - INTEGER in RFC 5961 (Improving TCP's Robustness to Blind In-Window Attacks) Default: 100 +tcp_rx_skb_cache - BOOLEAN + Controls a per TCP socket cache of one skb, that might help + performance of some workloads. This might be dangerous + on systems with a lot of TCP sockets, since it increases + memory usage. + + Default: 0 (disabled) + UDP variables: udp_l3mdev_accept - BOOLEAN
include/net/sock.h +2 -4
@@ -2433,13 +2433,11 @@ static inline void skb_setup_tx_timestamp(struct sk_buff *skb, __u16 tsflags)* This routine must be called with interrupts disabled or with the socket * locked so that the sk_buff queue operation is ok. */ +DECLARE_STATIC_KEY_FALSE (tcp_rx_skb_cache_key);static inline void sk_eat_skb (struct sock *sk,struct sk_buff *skb) {__skb_unlink (skb, &sk->sk_receive_queue); - if ( -#ifdef CONFIG_RPS - !static_branch_unlikely(&rps_needed) && -#endif +if (static_branch_unlikely (&tcp_rx_skb_cache_key) && !sk->sk_rx_skb_cache) { sk->sk_rx_skb_cache = skb;skb_orphan (skb);
net/ipv4/sysctl_net_ipv4.c +9 -0
@@ -51,6 +51,9 @@ static int comp_sack_nr_max = 255;static u32 u32_max_div_HZ =UINT_MAX /HZ ;static int one_day_secs =24 *3600 ; +DEFINE_STATIC_KEY_FALSE (tcp_rx_skb_cache_key); +EXPORT_SYMBOL (tcp_rx_skb_cache_key); +/* obsolete */ static int sysctl_tcp_low_latency__read_mostly ;
@@ -559,6 +562,12 @@ static struct ctl_table ipv4_table[] = { .extra1 = &sysctl_fib_sync_mem_min, .extra2 = &sysctl_fib_sync_mem_max, }, + { + .procname = "tcp_rx_skb_cache", + .data = &tcp_rx_skb_cache_key.key, + .mode = 0644, + .proc_handler = proc_do_static_key, + }, { } };
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
Re: [PATCH net 2/4] tcp: add tcp_rx_skb_cache sysctl
Quoted 4 lines · Eric Dumazet
Instead of relying on
rps_needed, it is safer to use a separate static key, since we do not want to enable TCPrx_skb_cacheby default. This feature can cause huge increase of memory usage on hosts with millions of sockets.
Thanks for the effort!
Quoted 76 lines
Signed-off-by Eric Dumazet <[email protected]>--- Documentation/networking/ip-sysctl.txt | 8 ++++++++ include/net/sock.h | 6 ++---- net/ipv4/sysctl_net_ipv4.c | 9 +++++++++ 3 files changed, 19 insertions(+), 4 deletions(-)Documentation/networking/ip-sysctl.txt +8
-0@@ -772,6 +772,14 @@ tcp_challenge_ack_limit - INTEGER in RFC 5961 (Improving TCP's Robustness to Blind In-Window Attacks) Default: 100 +tcp_rx_skb_cache - BOOLEAN + Controls a per TCP socket cache of one skb, that might help + performance of some workloads. This might be dangerous + on systems with a lot of TCP sockets, since it increases + memory usage. + + Default: 0 (disabled) + UDP variables: udp_l3mdev_accept - BOOLEANinclude/net/sock.h +2
-4@@ -2433,13 +2433,11 @@ static inline void skb_setup_tx_timestamp(struct sk_buff *skb, __u16 tsflags)* This routine must be called with interrupts disabled or with the socket * locked so that the sk_buff queue operation is ok. */ +DECLARE_STATIC_KEY_FALSE (tcp_rx_skb_cache_key);static inline void sk_eat_skb (struct sock *sk,struct sk_buff *skb) {__skb_unlink (skb, &sk->sk_receive_queue); - if ( -#ifdef CONFIG_RPS - !static_branch_unlikely(&rps_needed) && -#endif +if (static_branch_unlikely (&tcp_rx_skb_cache_key) && !sk->sk_rx_skb_cache) { sk->sk_rx_skb_cache = skb;skb_orphan (skb);net/ipv4/sysctl_net_ipv4.c +9
-0@@ -51,6 +51,9 @@ static int comp_sack_nr_max = 255;static u32 u32_max_div_HZ =UINT_MAX /HZ ;static int one_day_secs =24 *3600 ; +DEFINE_STATIC_KEY_FALSE (tcp_rx_skb_cache_key); +EXPORT_SYMBOL (tcp_rx_skb_cache_key); +/* obsolete */ static int sysctl_tcp_low_latency__read_mostly ;@@ -559,6 +562,12 @@ static struct ctl_table ipv4_table[] = { .extra1 = &sysctl_fib_sync_mem_min, .extra2 = &sysctl_fib_sync_mem_max, }, + { + .procname = "tcp_rx_skb_cache", + .data = &tcp_rx_skb_cache_key.key, + .mode = 0644, + .proc_handler = proc_do_static_key, + }, { } };Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
PATCH 3/4 tcp: add tcp_tx_skb_cache sysctl
In reply to Eric DumazetFeng Tang reported a performance regression after introduction of per TCP socket tx/rx caches, for TCP over loopback (netperf)
There is high chance the regression is caused by a change on how well the 32 KB per-thread page (current->task_frag) can be recycled, and lack of pcp caches for order-3 pages.
I could not reproduce the regression myself, cpus all being spinning on the mm spinlocks for page allocs/freeing, regardless of enabling or disabling the per tcp socket caches.
It seems best to disable the feature by default, and let admins enabling it.
MM layer either needs to provide scalable order-3 pages allocations, or could attempt a trylock on zone->lock if the caller only attempts to get a high-order page and is able to fallback to order-0 ones in case of pressure.
Tests run on a 56 cores host (112 hyper threads)
- 35.49% netperf [kernel.vmlinux] [k] queued_spin_lock_slowpath
- 35.49% queued_spin_lock_slowpath
- 18.18% get_page_from_freelist
- __alloc_pages_nodemask
- 18.18% alloc_pages_current skb_page_frag_refill sk_page_frag_refill tcp_sendmsg_locked tcp_sendmsg inet_sendmsg sock_sendmsg __sys_sendto __x64_sys_sendto do_syscall_64 entry_SYSCALL_64_after_hwframe __libc_send
+ 17.31% __free_pages_ok
+ 31.43% swapper [kernel.vmlinux] [k] intel_idle
+ 9.12% netperf [kernel.vmlinux] [k] copy_user_enhanced_fast_string
+ 6.53% netserver [kernel.vmlinux] [k] copy_user_enhanced_fast_string
+ 0.69% netserver [kernel.vmlinux] [k] queued_spin_lock_slowpath
+ 0.68% netperf [kernel.vmlinux] [k] skb_release_data
+ 0.52% netperf [kernel.vmlinux] [k] tcp_sendmsg_locked
0.46% netperf [kernel.vmlinux] [k] _raw_spin_lock_irqsave
--- include/net/sock.h | 4 +++- net/ipv4/sysctl_net_ipv4.c | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-)
include/net/sock.h +3 -1
@@ -1463,12 +1463,14 @@ static inline void sk_mem_uncharge(struct sock *sk, int size)__sk_mem_reclaim (sk,1 <<20 ); } +DECLARE_STATIC_KEY_FALSE (tcp_tx_skb_cache_key);static inline void sk_wmem_free_skb (struct sock *sk,struct sk_buff *skb) {sock_set_flag (sk,SOCK_QUEUE_SHRUNK ); sk->sk_wmem_queued -= skb->truesize;sk_mem_uncharge (sk, skb->truesize); -if (!sk->sk_tx_skb_cache && !skb_cloned (skb)) { +if (static_branch_unlikely (&tcp_tx_skb_cache_key) && + !sk->sk_tx_skb_cache && !skb_cloned (skb)) {skb_zcopy_clear (skb, true); sk->sk_tx_skb_cache = skb;return ;
net/ipv4/sysctl_net_ipv4.c +8 -0
@@ -54,6 +54,8 @@ static int one_day_secs = 24 * 3600;DEFINE_STATIC_KEY_FALSE (tcp_rx_skb_cache_key);EXPORT_SYMBOL (tcp_rx_skb_cache_key); +DEFINE_STATIC_KEY_FALSE (tcp_tx_skb_cache_key); +/* obsolete */ static int sysctl_tcp_low_latency__read_mostly ;
@@ -568,6 +570,12 @@ static struct ctl_table ipv4_table[] = { .mode = 0644, .proc_handler = proc_do_static_key, }, + { + .procname = "tcp_tx_skb_cache", + .data = &tcp_tx_skb_cache_key.key, + .mode = 0644, + .proc_handler = proc_do_static_key, + }, { } };
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
Re: [PATCH net 3/4] tcp: add tcp_tx_skb_cache sysctl
Hi Eric,
Quoted 6 lines · Eric Dumazet
Feng Tang reported a performance regression after introduction of per TCP socket tx/rx caches, for TCP over loopback (netperf)
There is high chance the regression is caused by a change on how well the 32 KB per-thread page (
current->task_frag) can be recycled, and lack of pcp caches for order-3 pages.
Exactly! When I checked the regression, I did several experiments, and thought of the simliar idea to add the per-CPU orderX pcp list, the other idea is to add a order3 list in per-cpu softnet_data as local cache.
Thanks,
Feng
Quoted 47 lines
I could not reproduce the regression myself, cpus all being spinning on the mm spinlocks for page allocs/freeing, regardless of enabling or disabling the per tcp socket caches.
It seems best to disable the feature by default, and let admins enabling it.
MM layer either needs to provide scalable order-3 pages allocations, or could attempt a trylock on
zone->lockif the caller only attempts to get a high-order page and is able to fallback to order-0 ones in case of pressure.Tests run on a 56 cores host (112 hyper threads)
(quoted diff) +0
-1- 35.49% netperf [kernel.vmlinux] [k] queued_spin_lock_slowpath - 35.49% queued_spin_lock_slowpath- 18.18%
get_page_from_freelist
-__alloc_pages_nodemask
- 18.18%alloc_pages_currentskb_page_frag_refillsk_page_frag_refilltcp_sendmsg_lockedtcp_sendmsginet_sendmsgsock_sendmsg__sys_sendto__x64_sys_sendtodo_syscall_64entry_SYSCALL_64_after_hwframe__libc_send
+ 17.31%__free_pages_ok(quoted diff) +6
-0+ 31.43% swapper [kernel.vmlinux] [k] intel_idle + 9.12% netperf [kernel.vmlinux] [k] copy_user_enhanced_fast_string + 6.53% netserver [kernel.vmlinux] [k] copy_user_enhanced_fast_string + 0.69% netserver [kernel.vmlinux] [k] queued_spin_lock_slowpath + 0.68% netperf [kernel.vmlinux] [k] skb_release_data + 0.52% netperf [kernel.vmlinux] [k] tcp_sendmsg_locked0.46% netperf [kernel.vmlinux] [k] _raw_spin_lock_irqsaveFixes 472c2e07eef0 ("tcp: add one skb cache for tx")Signed-off-by Eric Dumazet <[email protected]>Reported-by Feng Tang <[email protected]>--- include/net/sock.h | 4 +++- net/ipv4/sysctl_net_ipv4.c | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-)
PATCH 4/4 net: add high_order_alloc_disable sysctl/static key
In reply to Eric DumazetFrom linux-3.7, (commit 5640f7685831 "net: use a per task frag allocator") TCP sendmsg() has preferred using order-3 allocations.
While it gives good results for most cases, we had reports that heavy uses of TCP over loopback were hitting a spinlock contention in page allocations/freeing.
This commits adds a sysctl so that admins can opt-in for order-0 allocations. Hopefully mm layer might optimize order-3 allocations in the future since it could give us a nice boost (see 8 lines of following benchmark)
The following benchmark shows a win when more than 8 TCP_STREAM threads are running (56 x86 cores server in my tests)
for thr in {1..30}
do
sysctl -wq net.core.high_order_alloc_disable=0
T0=./super_netperf $thr -H 127.0.0.1 -l 15
sysctl -wq net.core.high_order_alloc_disable=1
T1=./super_netperf $thr -H 127.0.0.1 -l 15
echo $thr:$T0:$T1
done
1: 49979: 37267
2: 98745: 76286
3: 141088: 110051
4: 177414: 144772
5: 197587: 173563
6: 215377: 208448
7: 241061: 234087
8: 267155: 263373
9: 295069: 297402
10: 312393: 335213
11: 340462: 368778
12: 371366: 403954
13: 412344: 443713
14: 426617: 473580
15: 474418: 507861
16: 503261: 538539
17: 522331: 563096
18: 532409: 567084
19: 550824: 605240
20: 525493: 641988
21: 564574: 665843
22: 567349: 690868
23: 583846: 710917
24: 588715: 736306
25: 603212: 763494
26: 604083: 792654
27: 602241: 796450
28: 604291: 797993
29: 611610: 833249
30: 577356: 841062
--- include/net/sock.h | 2 ++ net/core/sock.c | 4 +++- net/core/sysctl_net_core.c | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-)
include/net/sock.h +2 -0
@@ -2534,6 +2534,8 @@ extern int sysctl_optmem_max;extern __u32 sysctl_wmem_default;extern __u32 sysctl_rmem_default; +DECLARE_STATIC_KEY_FALSE (net_high_order_alloc_disable_key); +static inline int sk_get_wmem0 (const struct sock *sk,const struct proto *proto) {/* Does this proto have per netns sysctl_wmem ? */
net/core/sock.c +3 -1
@@ -2320,6 +2320,7 @@ static void sk_leave_memory_pressure(struct sock *sk)/* On 32bit arches, an skb frag is limited to 2^15 */ #define SKB_FRAG_PAGE_ORDER get_order(32768) +DEFINE_STATIC_KEY_FALSE (net_high_order_alloc_disable_key);/** * skb_page_frag_refill - check that a page_frag contains enough room
@@ -2344,7 +2345,8 @@ bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t gfp) } pfrag->offset =0 ; -if (SKB_FRAG_PAGE_ORDER ) { +if (SKB_FRAG_PAGE_ORDER && + !static_branch_unlikely (&net_high_order_alloc_disable_key)) {/* Avoid direct reclaim but allow kswapd to wake */ pfrag->page = alloc_pages((gfp & ~__GFP_DIRECT_RECLAIM) | __GFP_COMP | __GFP_NOWARN |
net/core/sysctl_net_core.c +7 -0
@@ -562,6 +562,13 @@ static struct ctl_table net_core_table[] = { .extra1 = &zero, .extra2 = &two, }, + { + .procname = "high_order_alloc_disable", + .data = &net_high_order_alloc_disable_key.key, + .maxlen = sizeof(net_high_order_alloc_disable_key), + .mode = 0644, + .proc_handler = proc_do_static_key, + }, { } };
Signature · 1 line
2.22.0.410.gd8fdbe21b5-goog
David Miller
maintainerFrom: Eric Dumazet <[email protected]>
Date: Fri, 14 Jun 2019 16:22:17 -0700
Quoted 9 lines
Recent addition of per TCP socket rx/tx cache brought regressions for some workloads, as reported by Feng Tang.
It seems better to make them opt-in, before we adopt better heuristics.
The last patch adds
high_order_alloc_disablesysctl to ask TCPsendmsg()to exclusively use order-0 allocations, as mm layer has specific optimizations.
Series applied.