tcp: add zero copy receive
Subsystems (8)
cover tcp: add zero copy receive
This patch series add mmap() support to TCP sockets for RX zero copy.
While tcp_mmap() patch itself is quite small (~100 LOC), optimal support for asynchronous mmap() required better SO_RCVLOWAT behavior, and a test program to demonstrate how mmap() on TCP sockets can be used.
Note that mmap() (and associated munmap()) calls are adding more pressure on per-process VM semaphore, so might not show benefit for processus with high number of threads.
Eric Dumazet (5):
tcp: fix SO_RCVLOWAT and RCVBUF autotuning
tcp: fix delayed acks behavior for SO_RCVLOWAT
tcp: avoid extra wakeups for SO_RCVLOWAT users
tcp: implement mmap() for zero copy receive
selftests: net: add tcp_mmap program
include/linux/net.h | 1 + include/net/tcp.h | 4 + net/core/sock.c | 5 +- net/ipv4/af_inet.c | 3 +- net/ipv4/tcp.c | 138 ++++++++ net/ipv4/tcp_input.c | 22 +- net/ipv6/af_inet6.c | 3 +- tools/testing/selftests/net/Makefile | 2 + tools/testing/selftests/net/tcp_mmap.c | 437 +++++++++++++++++++++++++ 9 files changed, 608 insertions(+), 7 deletions(-) create mode 100644 tools/testing/selftests/net/tcp_mmap.c
Signature · 1 line
2.17.0.484.g0c8726318c-goog
PATCH 1/5 tcp: fix SO_RCVLOWAT and RCVBUF autotuning
Applications might use SO_RCVLOWAT on TCP socket hoping to receive one [E]POLLIN event only when a given amount of bytes are ready in socket receive queue.
Problem is that receive autotuning is not aware of this constraint, meaning sk_rcvbuf might be too small to allow all bytes to be stored.
Add a new (struct proto_ops)->set_rcvlowat method so that a protocol can override the default setsockopt(SO_RCVLOWAT) behavior.
--- include/linux/net.h | 1 + include/net/tcp.h | 1 + net/core/sock.c | 5 ++++- net/ipv4/af_inet.c | 1 + net/ipv4/tcp.c | 21 +++++++++++++++++++++ net/ipv6/af_inet6.c | 1 + 6 files changed, 29 insertions(+), 1 deletion(-)
include/linux/net.h +1 -0
@@ -197,6 +197,7 @@ struct proto_ops { int offset, size_t size, int flags);int (*sendmsg_locked)(struct sock *sk,struct msghdr *msg,size_t size); +int (*set_rcvlowat)(struct sock *sk,int val); };#define DECLARE_SOCKADDR (type, dst, src) \
include/net/tcp.h +1 -0
@@ -402,6 +402,7 @@ void tcp_set_keepalive(struct sock *sk, int val);void tcp_syn_ack_timeout (const struct request_sock *req);int tcp_recvmsg (struct sock *sk,struct msghdr *msg,size_t len,int nonblock,int flags,int *addr_len); +int tcp_set_rcvlowat (struct sock *sk,int val);void tcp_parse_options (const struct net *net,const struct sk_buff *skb,struct tcp_options_received *opt_rx,int estab,struct tcp_fastopen_cookie *foc);
net/core/sock.c +4 -1
@@ -905,7 +905,10 @@ int sock_setsockopt(struct socket *sock, int level, int optname,case SO_RCVLOWAT :if (val <0 ) val =INT_MAX ; - sk->sk_rcvlowat = val ? :1 ; +if (sock->ops->set_rcvlowat) + ret = sock->ops->set_rcvlowat (sk, val); +else + sk->sk_rcvlowat = val ? :1 ;break ;case SO_RCVTIMEO :
net/ipv4/af_inet.c +1 -0
@@ -1006,6 +1006,7 @@ const struct proto_ops inet_stream_ops = { .compat_getsockopt = compat_sock_common_getsockopt, .compat_ioctl = inet_compat_ioctl, #endif + .set_rcvlowat = tcp_set_rcvlowat, };EXPORT_SYMBOL (inet_stream_ops);
net/ipv4/tcp.c +21 -0
@@ -1701,6 +1701,27 @@ int tcp_peek_len(struct socket *sock) }EXPORT_SYMBOL (tcp_peek_len); +/* Make sure sk_rcvbuf is big enough to satisfy SO_RCVLOWAT hint */ +int tcp_set_rcvlowat (struct sock *sk,int val) +{ + sk->sk_rcvlowat = val ? :1 ; +if (sk->sk_userlocks &SOCK_RCVBUF_LOCK ) +return 0 ; + +/* val comes from user space and might be close to INT_MAX */ + val <<=1 ; +if (val <0 ) + val =INT_MAX ; + + val =min (val,sock_net (sk)->ipv4.sysctl_tcp_rmem[2 ]); +if (val > sk->sk_rcvbuf) { + sk->sk_rcvbuf = val; +tcp_sk (sk)->window_clamp =tcp_win_from_space (sk, val); + } +return 0 ; +} +EXPORT_SYMBOL (tcp_set_rcvlowat); +static void tcp_update_recv_tstamps (struct sk_buff *skb,struct scm_timestamping *tss) {
net/ipv6/af_inet6.c +1 -0
@@ -590,6 +590,7 @@ const struct proto_ops inet6_stream_ops = { .compat_setsockopt = compat_sock_common_setsockopt, .compat_getsockopt = compat_sock_common_getsockopt, #endif + .set_rcvlowat = tcp_set_rcvlowat, }; const struct proto_ops inet6_dgram_ops = {
Signature · 1 line
2.17.0.484.g0c8726318c-goog
PATCH 2/5 tcp: fix delayed acks behavior for SO_RCVLOWAT
In reply to Eric DumazetWe should not delay acks if there are not enough bytes in receive queue to satisfy SO_RCVLOWAT.
Since [E]POLLIN event is not going to be generated, there is little hope for a delayed ack to be useful.
In fact, delaying ACK prevents sender from completing the transfer.
--- net/ipv4/tcp_input.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
net/ipv4/tcp_input.c +5 -2
@@ -5026,9 +5026,12 @@ static void __tcp_ack_snd_check(struct sock *sk, int ofo_possible)/* More than one full frame received... */ if (((tp->rcv_nxt - tp->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss && /* ... and right edge of window advances far enough. - * (tcp_recvmsg() will send ACK otherwise). Or... + * (tcp_recvmsg() will send ACK otherwise). + * If application uses SO_RCVLOWAT, we want send ack now if + * we have not received enough bytes to satisfy the condition. */ - __tcp_select_window(sk) >= tp->rcv_wnd) || + (tp->rcv_nxt - tp->copied_seq < sk->sk_rcvlowat || + __tcp_select_window(sk) >= tp->rcv_wnd)) || /* We ACK each frame or... */ tcp_in_quickack_mode(sk) ||/* We have out of order data. */
Signature · 1 line
2.17.0.484.g0c8726318c-goog
PATCH 3/5 tcp: avoid extra wakeups for SO_RCVLOWAT users
In reply to Eric DumazetSO_RCVLOWAT is properly handled in tcp_poll(), so that POLLIN is only generated when enough bytes are available in receive queue, after David change (commit c7004482e8dc "tcp: Respect SO_RCVLOWAT in tcp_poll().")
But TCP still calls sk->sk_data_ready() for each chunk added in receive queue, meaning thread is awaken, and goes back to sleep shortly after.
Tested:
tcp_mmap test program, receiving 32768 MB of data with SO_RCVLOWAT set to 512KB
-> Should get ~2 wakeups (c-switches) per MB, regardless of how many (tiny or big) packets were received.
High speed (mostly full size GRO packets)
received 32768 MB (100 % mmap'ed) in 8.03112 s, 34.2266 Gbit,
cpu usage user:0.037 sys:1.404, 43.9758 usec per MB, 65497 c-switches
received 32768 MB (99.9954 % mmap'ed) in 7.98453 s, 34.4263 Gbit,
cpu usage user:0.03 sys:1.422, 44.3115 usec per MB, 65485 c-switches
Low speed (sender is ratelimited and sends 1-MSS at a time, so GRO is not helping)
received 22474.5 MB (100 % mmap'ed) in 6015.35 s, 0.0313414 Gbit,
cpu usage user:0.05 sys:1.586, 72.7952 usec per MB, 44950 c-switches
--- include/net/tcp.h | 1 + net/ipv4/tcp.c | 4 ++++ net/ipv4/tcp_input.c | 15 +++++++++++++-- 3 files changed, 18 insertions(+), 2 deletions(-)
include/net/tcp.h +1 -0
@@ -403,6 +403,7 @@ void tcp_syn_ack_timeout(const struct request_sock *req);int tcp_recvmsg (struct sock *sk,struct msghdr *msg,size_t len,int nonblock,int flags,int *addr_len);int tcp_set_rcvlowat (struct sock *sk,int val); +void tcp_data_ready (struct sock *sk);void tcp_parse_options (const struct net *net,const struct sk_buff *skb,struct tcp_options_received *opt_rx,int estab,struct tcp_fastopen_cookie *foc);
net/ipv4/tcp.c +4 -0
@@ -1705,6 +1705,10 @@ EXPORT_SYMBOL(tcp_peek_len);int tcp_set_rcvlowat (struct sock *sk,int val) { sk->sk_rcvlowat = val ? :1 ; + +/* Check if we need to signal EPOLLIN right now */ +tcp_data_ready (sk); +if (sk->sk_userlocks &SOCK_RCVBUF_LOCK )return 0 ;
net/ipv4/tcp_input.c +13 -2
@@ -4576,6 +4576,17 @@ int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size) } +void tcp_data_ready (struct sock *sk) +{ +const struct tcp_sock *tp =tcp_sk (sk); +int avail = tp->rcv_nxt - tp->copied_seq; + +if (avail < sk->sk_rcvlowat && !sock_flag (sk,SOCK_DONE )) +return ; + + sk->sk_data_ready (sk); +} +static void tcp_data_queue (struct sock *sk,struct sk_buff *skb) {struct tcp_sock *tp =tcp_sk (sk);
@@ -4633,7 +4644,7 @@ static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)if (eaten >0 )kfree_skb_partial (skb, fragstolen);if (!sock_flag (sk,SOCK_DEAD )) - sk->sk_data_ready (sk); +tcp_data_ready (sk);return ; }
@@ -5434,7 +5445,7 @@ void tcp_rcv_established(struct sock *sk, struct sk_buff *skb, no_ack:if (eaten)kfree_skb_partial (skb, fragstolen); - sk->sk_data_ready (sk); +tcp_data_ready (sk);return ; } }
Signature · 1 line
2.17.0.484.g0c8726318c-goog
PATCH 4/5 tcp: implement mmap() for zero copy receive
In reply to Eric DumazetSome networks can make sure TCP payload can exactly fit 4KB pages, with well chosen MSS/MTU and architectures.
Implement mmap() system call so that applications can avoid copying data without complex splice() games.
Note that a successful mmap( X bytes) on TCP socket is consuming bytes, as if recvmsg() has been done. (tp->copied += X)
Only PROT_READ mappings are accepted, as skb page frags are fundamentally shared and read only.
If tcp_mmap() finds data that is not a full page, or a patch of urgent data, -EINVAL is returned, no bytes are consumed.
Application must fallback to recvmsg() to read the problematic sequence.
mmap() wont block, regardless of socket being in blocking or non-blocking mode. If not enough bytes are in receive queue, mmap() would return -EAGAIN, or -EIO if socket is in a state where no other bytes can be added into receive queue.
An application might use SO_RCVLOWAT, poll() and/or ioctl( FIONREAD) to efficiently use mmap()
On the sender side, MSG_EOR might help to clearly separate unaligned headers and 4K-aligned chunks if necessary.
Tested:
mlx4 (cx-3) 40Gbit NIC, with tcp_mmap program provided in following patch. MTU set to 4168 (4096 TCP payload, 40 bytes IPv6 header, 32 bytes TCP header)
Without mmap() (tcp_mmap -s)
received 32768 MB (0 % mmap'ed) in 8.13342 s, 33.7961 Gbit,
cpu usage user:0.034 sys:3.778, 116.333 usec per MB, 63062 c-switches
received 32768 MB (0 % mmap'ed) in 8.14501 s, 33.748 Gbit,
cpu usage user:0.029 sys:3.997, 122.864 usec per MB, 61903 c-switches
received 32768 MB (0 % mmap'ed) in 8.11723 s, 33.8635 Gbit,
cpu usage user:0.048 sys:3.964, 122.437 usec per MB, 62983 c-switches
received 32768 MB (0 % mmap'ed) in 8.39189 s, 32.7552 Gbit,
cpu usage user:0.038 sys:4.181, 128.754 usec per MB, 55834 c-switches
With mmap() on receiver (tcp_mmap -s -z)
received 32768 MB (100 % mmap'ed) in 8.03083 s, 34.2278 Gbit,
cpu usage user:0.024 sys:1.466, 45.4712 usec per MB, 65479 c-switches
received 32768 MB (100 % mmap'ed) in 7.98805 s, 34.4111 Gbit,
cpu usage user:0.026 sys:1.401, 43.5486 usec per MB, 65447 c-switches
received 32768 MB (100 % mmap'ed) in 7.98377 s, 34.4296 Gbit,
cpu usage user:0.028 sys:1.452, 45.166 usec per MB, 65496 c-switches
received 32768 MB (99.9969 % mmap'ed) in 8.01838 s, 34.281 Gbit,
cpu usage user:0.02 sys:1.446, 44.7388 usec per MB, 65505 c-switches
--- include/net/tcp.h | 2 + net/ipv4/af_inet.c | 2 +- net/ipv4/tcp.c | 113 ++++++++++++++++++++++++++++++++++++++++++++ net/ipv6/af_inet6.c | 2 +- 4 files changed, 117 insertions(+), 2 deletions(-)
include/net/tcp.h +2 -0
@@ -404,6 +404,8 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, int flags, int *addr_len);int tcp_set_rcvlowat (struct sock *sk,int val);void tcp_data_ready (struct sock *sk); +int tcp_mmap (struct file *file,struct socket *sock, +struct vm_area_struct *vma);void tcp_parse_options (const struct net *net,const struct sk_buff *skb,struct tcp_options_received *opt_rx,int estab,struct tcp_fastopen_cookie *foc);
net/ipv4/af_inet.c +1 -1
@@ -994,7 +994,7 @@ const struct proto_ops inet_stream_ops = { .getsockopt = sock_common_getsockopt, .sendmsg = inet_sendmsg, .recvmsg = inet_recvmsg, - .mmap = sock_no_mmap, + .mmap = tcp_mmap, .sendpage = inet_sendpage, .splice_read = tcp_splice_read, .read_sock = tcp_read_sock,
net/ipv4/tcp.c +113 -0
119 lines not on this page · raw message
net/ipv6/af_inet6.c +1 -1
@@ -579,7 +579,7 @@ const struct proto_ops inet6_stream_ops = { .getsockopt = sock_common_getsockopt, /* ok */ .sendmsg = inet_sendmsg, /* ok */ .recvmsg = inet_recvmsg, /* ok */ - .mmap = sock_no_mmap, + .mmap = tcp_mmap, .sendpage = inet_sendpage, .sendmsg_locked = tcp_sendmsg_locked, .sendpage_locked = tcp_sendpage_locked,
Signature · 1 line
2.17.0.484.g0c8726318c-goog
PATCH 5/5 selftests: net: add tcp_mmap program
In reply to Eric DumazetThis is a reference program showing how mmap() can be used on TCP flows to implement receive zero copy.
--- tools/testing/selftests/net/Makefile | 2 + tools/testing/selftests/net/tcp_mmap.c | 437 +++++++++++++++++++++++++ 2 files changed, 439 insertions(+) create mode 100644 tools/testing/selftests/net/tcp_mmap.c
tools/testing/selftests/net/Makefile +2 -0
@@ -8,9 +8,11 @@ TEST_PROGS := run_netsocktests run_afpackettests test_bpf.sh netdevice.sh rtnetl TEST_PROGS += fib_tests.sh fib-onlink-tests.sh pmtu.sh TEST_GEN_FILES = socket TEST_GEN_FILES += psock_fanout psock_tpacket msg_zerocopy +TEST_GEN_FILES += tcp_mmap TEST_GEN_PROGS = reuseport_bpf reuseport_bpf_cpu reuseport_bpf_numa TEST_GEN_PROGS += reuseport_dualstack reuseaddr_conflict include ../lib.mk $(OUTPUT)/reuseport_bpf_numa: LDFLAGS += -lnuma +$(OUTPUT)/tcp_mmap: LDFLAGS += -lpthread
tools/testing/selftests/net/tcp_mmap.c new file +437 -0
437 lines not on this page · raw message
Signature · 1 line
2.17.0.484.g0c8726318c-goog
David Miller
maintainerFrom: Eric Dumazet <[email protected]>
Date: Mon, 16 Apr 2018 10:33:34 -0700
Quoted 9 lines
This patch series add
mmap()support to TCP sockets for RX zero copy.While
tcp_mmap()patch itself is quite small (~100 LOC), optimal support for asynchronousmmap()required betterSO_RCVLOWATbehavior, and a test program to demonstrate howmmap()on TCP sockets can be used.Note that
mmap()(and associatedmunmap()) calls are adding more pressure on per-process VM semaphore, so might not show benefit for processus with high number of threads.
Great work. I can see how it is less effective without the rcvlowat fixes/tweaks.
Series applied, thanks Eric!
Re: [PATCH net-next 4/5] tcp: implement mmap() for zero copy receive
In reply to Eric DumazetQuoted 9 lines · Eric Dumazet
Some networks can make sure TCP payload can exactly fit 4KB pages, with well chosen MSS/MTU and architectures.
Implement
mmap()system call so that applications can avoid copying data without complexsplice()games.Note that a successful mmap( X bytes) on TCP socket is consuming bytes, as if
recvmsg()has been done. (tp->copied+= X)
Oh well, I should have run this code with LOCKDEP enabled :/
[ 974.320412] ======================================================
[ 974.326631] WARNING: possible circular locking dependency detected
[ 974.332816] 4.16.0-dbx-DEV #40 Not tainted
[ 974.336927] ------------------------------------------------------
[ 974.343107] b78299096/15790 is trying to acquire lock:
[ 974.348246] 000000006074c9cf (sk_lock-AF_INET6){+.+.}, at: tcp_mmap+0x7c/0x550
[ 974.355505]
but task is already holding lock:
[ 974.361366] 000000008dbe063b (&mm->mmap_sem){++++}, at: vm_mmap_pgoff+0x99/0x100
[ 974.368801]
which lock already depends on the new lock.[ 974.377010]
the existing dependency chain (in reverse order) is:
[ 974.384501]
-> #1 (&mm->mmap_sem){++++}:
[ 974.389911] __might_fault+0x68/0x90
[ 974.394025] _copy_from_user+0x23/0xa0
[ 974.398311] sock_setsockopt+0x4a2/0xac0
[ 974.402761] __sys_setsockopt+0xd9/0xf0
[ 974.407118] SyS_setsockopt+0xe/0x20
[ 974.411242] do_syscall_64+0x6e/0x1a0
[ 974.415431] entry_SYSCALL_64_after_hwframe+0x42/0xb7
[ 974.421011]
-> #0 (sk_lock-AF_INET6){+.+.}:
[ 974.426690] lock_acquire+0x95/0x1e0
[ 974.430813] lock_sock_nested+0x71/0xa0
[ 974.435196] tcp_mmap+0x7c/0x550
[ 974.438940] sock_mmap+0x23/0x30
[ 974.442695] mmap_region+0x3a4/0x5d0
[ 974.446808] do_mmap+0x313/0x530
[ 974.450571] vm_mmap_pgoff+0xc7/0x100
[ 974.454769] ksys_mmap_pgoff+0x1d5/0x260
[ 974.459247] SyS_mmap+0x1b/0x30
[ 974.462936] do_syscall_64+0x6e/0x1a0
[ 974.467114] entry_SYSCALL_64_after_hwframe+0x42/0xb7
[ 974.472678]
other info that might help us debug this:[ 974.480677] Possible unsafe locking scenario:
[ 974.486600] CPU0 CPU1
[ 974.491152] ---- ----
[ 974.495684] lock(&mm->mmap_sem);
[ 974.499089] lock(sk_lock-AF_INET6);
[ 974.505285] lock(&mm->mmap_sem);
[ 974.511211] lock(sk_lock-AF_INET6);
[ 974.514885]
*** DEADLOCK ***[ 974.520825] 1 lock held by b78299096/15790:
[ 974.525018] #0: 000000008dbe063b (&mm->mmap_sem){++++}, at: vm_mmap_pgoff+0x99/0x100
[ 974.532852]
stack backtrace:
[ 974.537224] CPU: 25 PID: 15790 Comm: b78299096 Not tainted 4.16.0-dbx-DEV #40
[ 974.544371] Hardware name: Intel RML,PCH/Iota_QC_19, BIOS 2.40.0 06/22/2016
[ 974.551333] Call Trace:
[ 974.553792] dump_stack+0x70/0xa5
[ 974.557111] print_circular_bug.isra.39+0x1d8/0x1e6
[ 974.561982] __lock_acquire+0x1284/0x1340
[ 974.565992] ? tcp_mmap+0x7c/0x550
[ 974.569419] lock_acquire+0x95/0x1e0
[ 974.573011] ? lock_acquire+0x95/0x1e0
[ 974.576767] ? tcp_mmap+0x7c/0x550
[ 974.580167] lock_sock_nested+0x71/0xa0
[ 974.584023] ? tcp_mmap+0x7c/0x550
[ 974.587437] tcp_mmap+0x7c/0x550
[ 974.590677] sock_mmap+0x23/0x30
[ 974.593909] mmap_region+0x3a4/0x5d0
[ 974.597506] do_mmap+0x313/0x530
[ 974.600749] vm_mmap_pgoff+0xc7/0x100
[ 974.604414] ksys_mmap_pgoff+0x1d5/0x260
[ 974.608341] ? fd_install+0x25/0x30
[ 974.611849] ? trace_hardirqs_on_caller+0xef/0x180
[ 974.616641] SyS_mmap+0x1b/0x30
[ 974.619804] do_syscall_64+0x6e/0x1a0
[ 974.623462] entry_SYSCALL_64_after_hwframe+0x42/0xb7
[ 974.628549] RIP: 0033:0x433749
[ 974.631600] RSP: 002b:00007ffd29fdb438 EFLAGS: 00000216 ORIG_RAX: 0000000000000009
[ 974.639197] RAX: ffffffffffffffda RBX: 00000000004002e0 RCX: 0000000000433749
[ 974.646323] RDX: 0000000000000008 RSI: 0000000000004000 RDI: 0000000020ab7000
[ 974.653463] RBP: 00007ffd29fdb460 R08: 0000000000000003 R09: 0000000000000000
[ 974.660603] R10: 0000000000000012 R11: 0000000000000216 R12: 0000000000401670
[ 974.667737] R13: 0000000000401700 R14: 0000000000000000 R15: 0000000000000000I am not sure we can keep mmap() API, since we probably need to first lock the socket, then grab vm semaphore.
Re: [PATCH net-next 4/5] tcp: implement mmap() for zero copy receive
Quoted 3 lines · Eric Dumazet
I am not sure we can keep
mmap()API, since we probably need to first lock the socket, then grab vm semaphore.
We can keep mmap() nice interface, granted we can add one hook like in following patch.
David, do you think such patch would be acceptable by lkml and mm/fs maintainers ?
Alternative would be implementing an ioctl() or getsockopt() operation, but it seems less natural...
Thanks !
include/linux/fs.h +1 -0
@@ -1714,6 +1714,7 @@ struct file_operations {long (*unlocked_ioctl) (struct file *,unsigned int ,unsigned long );long (*compat_ioctl) (struct file *,unsigned int ,unsigned long );int (*mmap) (struct file *,struct vm_area_struct *); +void (*mmap_hook) (struct file *,bool );unsigned long mmap_supported_flags;int (*open) (struct inode *,struct file *);int (*flush) (struct file *,fl_owner_t id);
mm/util.c +10 -1
@@ -350,11 +350,20 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr, ret =security_mmap_file (file, prot, flag);if (!ret) { -if (down_write_killable (&mm->mmap_sem)) +void (*mmap_hook)(struct file *,bool ) = file ? file->f_op->mmap_hook :NULL ; + +if (mmap_hook) +mmap_hook (file, true); +if (down_write_killable (&mm->mmap_sem)) { +if (mmap_hook) +mmap_hook (file, false);return -EINTR ; + } ret =do_mmap_pgoff (file, addr, len, prot, flag, pgoff, &populate, &uf);up_write (&mm->mmap_sem); +if (mmap_hook) +mmap_hook (file, false);userfaultfd_unmap_complete (mm, &uf);if (populate)mm_populate (ret, populate);
net/ipv4/tcp.c +0 -3
@@ -1756,8 +1756,6 @@ int tcp_mmap(struct file *file, struct socket *sock,/* TODO: Maybe the following is not needed if pages are COW */ vma->vm_flags &= ~VM_MAYWRITE ; -lock_sock (sk); - ret = -ENOTCONN ;if (sk->sk_state ==TCP_LISTEN ) goto out;
@@ -1833,7 +1831,6 @@ int tcp_mmap(struct file *file, struct socket *sock, ret =0 ; out: -release_sock (sk);kvfree (pages_array);return ret; }
net/socket.c +11 -0
@@ -131,6 +131,16 @@ static ssize_t sock_splice_read(struct file *file, loff_t *ppos, struct pipe_inode_info *pipe, size_t len, unsigned int flags); +static void sock_mmap_hook (struct file *file,bool enter) +{ +struct socket *sock = file->private_data; +struct sock *sk = sock->sk; + +if (enter) +lock_sock (sk); +else +release_sock (sk); +}/* * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear * in the operation structures but are done directly via the socketcall() multiplexor.
@@ -147,6 +157,7 @@ static const struct file_operations socket_file_ops = { .compat_ioctl = compat_sock_ioctl, #endif .mmap = sock_mmap, + .mmap_hook = sock_mmap_hook, .release = sock_close, .fasync = sock_fasync, .sendpage = sock_sendpage,
David Miller
maintainerRe: [PATCH net-next 4/5] tcp: implement mmap() for zero copy receive
From: Eric Dumazet <[email protected]>
Date: Thu, 19 Apr 2018 18:01:32 -0700
Quoted 2 lines
David, do you think such patch would be acceptable by lkml and mm/fs maintainers ?
You will have to ask them directly I think :)
Re: [PATCH net-next 1/5] tcp: fix SO_RCVLOWAT and RCVBUF autotuning
In reply to Eric DumazetQuoted 10 lines · Eric Dumazet
Applications might use
SO_RCVLOWATon TCP socket hoping to receive one [E]POLLIN event only when a given amount of bytes are ready in socket receive queue.Problem is that receive autotuning is not aware of this constraint, meaning
sk_rcvbufmight be too small to allow all bytes to be stored.Add a new (struct proto_ops)->set_rcvlowat method so that a protocol can override the default
setsockopt(SO_RCVLOWAT)behavior.
...
net/ipv4/tcp.c int tcp_peek_len(struct socket *sock) · line 1716
Quoted 13 lines
net/ipv4/tcp.c +13
-0+/* Make sure sk_rcvbuf is big enough to satisfy SO_RCVLOWAT hint */ +int tcp_set_rcvlowat (struct sock *sk,int val) +{ + sk->sk_rcvlowat = val ? :1 ; +if (sk->sk_userlocks &SOCK_RCVBUF_LOCK ) +return 0 ; + +/* val comes from user space and might be close to INT_MAX */ + val <<=1 ; +if (val <0 ) + val =INT_MAX ; + + val =min (val,sock_net (sk)->ipv4.sysctl_tcp_rmem[2 ]);
Hi Eric,
As val may be changed to a smaller value by the line above, shouldn't it assign sk->sk_rcvlowat again? Otherwise it may still be bigger than sk_rcvbuf.
Say val = 512k, sysctl_tcp_rmem[2] = 256k
val <<= 1 , val = 1M
val = min() , val = 256k
val > sk_rcvbuf
sk_rcvbuf = 256k , at most, which is smaller than sk_rcvlowat
Without reassigning the application has to check how big is tcp_rmem[2] and be sure to not go above /2 of it to not trip on this again.
Or, as you have added a return value here, it could return -EINVAL in such cases. Probably better, as then the application will not get a smaller buffer than wanted later.
Quoted 8 lines
net/ipv4/tcp.c +8
-0+if (val > sk->sk_rcvbuf) { + sk->sk_rcvbuf = val; +tcp_sk (sk)->window_clamp =tcp_win_from_space (sk, val); + } +return 0 ; +} +EXPORT_SYMBOL (tcp_set_rcvlowat); +
...
Re: [PATCH net-next 1/5] tcp: fix SO_RCVLOWAT and RCVBUF autotuning
Quoted 12 lines · Marcelo Ricardo Leitner
Hi Eric,
As val may be changed to a smaller value by the line above, shouldn't it assign
sk->sk_rcvlowatagain? Otherwise it may still be bigger thansk_rcvbuf.Say val = 512k, sysctl_tcp_rmem[2] = 256k
val <<= 1 , val = 1M
val =min(), val = 256k
val >sk_rcvbuf
sk_rcvbuf= 256k , at most, which is smaller thansk_rcvlowat
Quoted 3 lines
Without reassigning the application has to check how big is tcp_rmem[2] and be sure to not go above /2 of it to not trip on this again.
I am not sure about that :
Reporting an error might break existing applications that were not expecting setsockopt()
to return an error, even if the value was 'probably too big to be okay'
Quoted 3 lines
Or, as you have added a return value here, it could return
-EINVALin such cases. Probably better, as then the application will not get a smaller buffer than wanted later.
Note that maybe some applications might first set SO_RCVLOWAT, then SO_RCVBUF,
we do not want to break them.
My patch really covers the case were autotuning should immediately grow the sk_rcvbuf
for reasonable SO_RCVLOWAT values.
Re: [PATCH net-next 1/5] tcp: fix SO_RCVLOWAT and RCVBUF autotuning
Quoted 19 lines · Eric Dumazet
Quoted 12 lines · Marcelo Ricardo Leitner
Hi Eric,
As val may be changed to a smaller value by the line above, shouldn't it assign
sk->sk_rcvlowatagain? Otherwise it may still be bigger thansk_rcvbuf.Say val = 512k, sysctl_tcp_rmem[2] = 256k
val <<= 1 , val = 1M
val =min(), val = 256k
val >sk_rcvbuf
sk_rcvbuf= 256k , at most, which is smaller thansk_rcvlowatQuoted 3 lines
Without reassigning the application has to check how big is tcp_rmem[2] and be sure to not go above /2 of it to not trip on this again.
Quoted 5 lines
I am not sure about that :
Reporting an error might break existing applications that were not expecting
setsockopt()
to return an error, even if the value was 'probably too big to be okay'
I would argue that they are already broken but...
Quoted 9 lines
Quoted 5 lines
Or, as you have added a return value here, it could return
-EINVALin such cases. Probably better, as then the application will not get a smaller buffer than wanted later.Note that maybe some applications might first set
SO_RCVLOWAT, thenSO_RCVBUF,
we do not want to break them.
... yeah.. if they do it this way, they work today. Good point.
Quoted 5 lines
My patch really covers the case were autotuning should immediately grow the
sk_rcvbuf
for reasonableSO_RCVLOWATvalues.
That's not exactly what the comment above the function says, thus why my comments.
Thanks.
Re: [PATCH net-next 4/5] tcp: implement mmap() for zero copy receive
In reply to Eric DumazetQuoted 6 lines · On Thu, 19 Apr 2018 18:01:32 -0700 Eric Dumazet <[email protected]> wrote:
We can keep
mmap()nice interface, granted we can add one hook like in following patch.David, do you think such patch would be acceptable by lkml and mm/fs maintainers ?
Alternative would be implementing an
ioctl()orgetsockopt()operation, but it seems less natural...
So I have little standing here, but what the heck, not letting that bother me has earned me a living for the last 20 years or so...:)
I think you should consider switching over to an interface where you mmap() the region once, and use ioctl() to move the data into that region, for a couple of reasons beyond the locking issues you've already found:
- The "mmap() consumes data" semantics are a bit ... strange, IMO. That's not what mmap() normally does. People expect ioctl() to do magic things, instead.
- I would expect it to be a tiny bit faster, since you wouldn't be doing the VMA setup and teardown each time.
Thanks,
jon
Re: [PATCH net-next 4/5] tcp: implement mmap() for zero copy receive
Quoted 10 lines · Jonathan Corbet
Quoted 6 lines · On Thu, 19 Apr 2018 18:01:32 -0700 Eric Dumazet <[email protected]> wrote:
We can keep
mmap()nice interface, granted we can add one hook like in following patch.David, do you think such patch would be acceptable by lkml and mm/fs maintainers ?
Alternative would be implementing an
ioctl()orgetsockopt()operation, but it seems less natural...
Hi Jonathan
Quoted 10 lines
So I have little standing here, but what the heck, not letting that bother me has earned me a living for the last 20 years or so...:)
I think you should consider switching over to an interface where you
mmap()the region once, and useioctl()to move the data into that region, for a couple of reasons beyond the locking issues you've already found:- The "
mmap()consumes data" semantics are a bit ... strange, IMO. That's not whatmmap()normally does. People expectioctl()to do magic things, instead.
Well, the thing is that most of our use cases wont reuse same mmap() area.
RPC layer will provide all RPC with their associated pages to RPC consumers.
RPC consumers will decide to keep these pages or consume them.
So having to mmap() + another syscall to consume XXX bytes from receive queue is not going to save cpu cycles :/
Having the ability to call mmap() multiple times for the same TCP payload is not going to be of any use in real applications. This is why I only support 'offset 0' for the last mmap() parameter.
Quoted 3 lines
- I would expect it to be a tiny bit faster, since you wouldn't be doing the VMA setup and teardown each time.
Maybe for the degenerated case we can reuse the same region over and over.