tcp: mmap: rework zerocopy receive
Subsystems (5)
v2 cover tcp: mmap: rework zerocopy receive
syzbot reported a lockdep issue caused by tcp mmap() support.
I implemented Andy Lutomirski nice suggestions to resolve the issue and increase scalability as well.
First patch is adding a new setsockopt() operation and changes mmap() behavior.
Second patch changes tcp_mmap reference program.
v2:
Added a missing page align of zc->length in tcp_zerocopy_receive() Properly clear zc->recv_skip_hint in case user request was completed.
Eric Dumazet (2):
tcp: add TCP_ZEROCOPY_RECEIVE support for zerocopy receive
selftests: net: tcp_mmap must use TCP_ZEROCOPY_RECEIVE
include/uapi/linux/tcp.h | 8 ++ net/ipv4/tcp.c | 189 +++++++++++++------------ tools/testing/selftests/net/tcp_mmap.c | 63 +++++---- 3 files changed, 142 insertions(+), 118 deletions(-)
Signature · 1 line
2.17.0.441.gb46fe60e1d-goog
PATCH v2 1/2 tcp: add TCP_ZEROCOPY_RECEIVE support for zerocopy receive
When adding tcp mmap() implementation, I forgot that socket lock had to be taken before current->mm->mmap_sem. syzbot eventually caught the bug.
Since we can not lock the socket in tcp mmap() handler we have to split the operation in two phases.
1) mmap() on a tcp socket simply reserves VMA space, and nothing else. This operation does not involve any TCP locking.
2) setsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, ...) implements the transfert of pages from skbs to one VMA.
This operation only uses down_read(¤t->mm->mmap_sem) after holding TCP lock, thus solving the lockdep issue.
This new implementation was suggested by Andy Lutomirski with great details.
Benefits are :
- Better scalability, in case multiple threads reuse VMAS (without mmap()/munmap() calls) since mmap_sem wont be write locked.
- Better error recovery.
The previous mmap() model had to provide the expected size of the mapping. If for some reason one part could not be mapped (partial MSS), the whole operation had to be aborted.
With the tcp_zerocopy_receive struct, kernel can report how many bytes were successfuly mapped, and how many bytes should be read to skip the problematic sequence.
- No more memory allocation to hold an array of page pointers. 16 MB mappings needed 32 KB for this array, potentially using vmalloc() :/
- skbs are freed while mmap_sem has been released
Following patch makes the change in tcp_mmap tool to demonstrate one possible use of mmap() and setsockopt(... TCP_ZEROCOPY_RECEIVE ...)
Note that memcg might require additional changes.
--- include/uapi/linux/tcp.h | 8 ++ net/ipv4/tcp.c | 189 ++++++++++++++++++++------------------- 2 files changed, 106 insertions(+), 91 deletions(-)
include/uapi/linux/tcp.h +8 -0
@@ -122,6 +122,7 @@ enum {#define TCP_MD5SIG_EXT 32/* TCP MD5 Signature with extensions */ #define TCP_FASTOPEN_KEY 33/* Set the key for Fast Open (cookie) */ #define TCP_FASTOPEN_NO_COOKIE 34/* Enable TFO without a TFO cookie */ +#define TCP_ZEROCOPY_RECEIVE 35struct tcp_repair_opt {__u32 opt_code;
@@ -276,4 +277,11 @@ struct tcp_diag_md5sig { __u8 tcpm_key[TCP_MD5SIG_MAXKEYLEN]; }; +/* setsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, ...) */ + +struct tcp_zerocopy_receive { +__u64 address;/* in: address of mapping */ +__u32 length;/* in/out: number of bytes to map/mapped */ +__u32 recv_skip_hint;/* out: amount of bytes to skip */ +};#endif /* _UAPI_LINUX_TCP_H */
net/ipv4/tcp.c +98 -91
222 lines not on this page · raw message
Signature · 1 line
2.17.0.441.gb46fe60e1d-goog
PATCH v2 2/2 selftests: net: tcp_mmap must use TCP_ZEROCOPY_RECEIVE
In reply to Eric DumazetAfter prior kernel change, mmap() on TCP socket only reserves VMA.
We have to use setsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, ...) to perform the transfert of pages from skbs in TCP receive queue into such VMA.
struct tcp_zerocopy_receive {
__u64 address; /* in: address of mapping */
__u32 length; /* in/out: number of bytes to map/mapped */
__u32 recv_skip_hint; /* out: amount of bytes to skip */
};
After a successful setsockopt(...TCP_ZEROCOPY_RECEIVE...), @length contains number of bytes that were mapped, and @recv_skip_hint contains number of bytes that should be read using conventional read()/recv()/recvmsg() system calls, to skip a sequence of bytes that can not be mapped, because not properly page aligned.
--- tools/testing/selftests/net/tcp_mmap.c | 63 +++++++++++++++----------- 1 file changed, 36 insertions(+), 27 deletions(-)
tools/testing/selftests/net/tcp_mmap.c +36 -27
@@ -76,9 +76,10 @@#include <time.h> #include <sys/time.h> #include <netinet/in.h> -#include <netinet/tcp.h> #include <arpa/inet.h> #include <poll.h> +#include <linux/tcp.h> +#include <assert.h> #ifndef MSG_ZEROCOPY #define MSG_ZEROCOPY 0x4000000
@@ -134,11 +135,12 @@ void hash_zone(void *zone, unsigned int length)void *child_thread (void *arg) {unsigned long total_mmap =0 , total =0 ; +struct tcp_zerocopy_receive zc;unsigned long delta_usec;int flags =MAP_SHARED ;struct timeval t0, t1;char *buffer =NULL ; -void *oaddr =NULL ; +void *addr =NULL ;double throughput;struct rusage ru;int lu, fd;
@@ -153,41 +155,45 @@ void *child_thread(void *arg)perror ("malloc" ); goto error; } +if (zflg) { + addr =mmap (NULL , chunk_size,PROT_READ , flags, fd,0 ); +if (addr == (void *)-1 ) + zflg =0 ; + }while (1 ) {struct pollfd pfd = { .fd = fd, .events =POLLIN , };int sub;poll (&pfd,1 ,10000 );if (zflg) { -void *naddr; +int res; - naddr =mmap (oaddr, chunk_size,PROT_READ , flags, fd,0 ); -if (naddr == (void *)-1 ) { -if (errno ==EAGAIN ) { -/* That is if SO_RCVLOWAT is buggy */ -usleep (1000 ); -continue ; - } -if (errno ==EINVAL ) { - flags =MAP_SHARED ; - oaddr =NULL ; - goto fallback; - } -if (errno !=EIO ) -perror ("mmap()" ); + zc.address = (__u64 )addr; + zc.length = chunk_size; + zc.recv_skip_hint =0 ; + res =setsockopt (fd,IPPROTO_TCP ,TCP_ZEROCOPY_RECEIVE , + &zc,sizeof (zc)); +if (res ==-1 )break ; + +if (zc.length) { +assert (zc.length <= chunk_size); + total_mmap += zc.length; +if (xflg) +hash_zone (addr, zc.length); + total += zc.length; } - total_mmap += chunk_size; -if (xflg) -hash_zone (naddr, chunk_size); - total += chunk_size; -if (!keepflag) { - flags |=MAP_FIXED ; - oaddr = naddr; +if (zc.recv_skip_hint) { +assert (zc.recv_skip_hint <= chunk_size); + lu =read (fd, buffer, zc.recv_skip_hint); +if (lu >0 ) { +if (xflg) +hash_zone (buffer, lu); + total += lu; + } }continue ; } -fallback: sub =0 ;while (sub < chunk_size) { lu =read (fd, buffer + sub, chunk_size - sub);
@@ -228,6 +234,8 @@ void *child_thread(void *arg) error:free (buffer);close (fd); +if (zflg) +munmap (addr, chunk_size);pthread_exit (0 ); }
@@ -371,7 +379,8 @@ int main(int argc, char *argv[])setup_sockaddr (cfg_family, host, &listenaddr);if (mss && -setsockopt (fdlisten,SOL_TCP ,TCP_MAXSEG , &mss,sizeof (mss)) ==-1 ) { +setsockopt (fdlisten,IPPROTO_TCP ,TCP_MAXSEG , + &mss,sizeof (mss)) ==-1 ) {perror ("setsockopt TCP_MAXSEG" );exit (1 ); }
@@ -402,7 +411,7 @@ int main(int argc, char *argv[])setup_sockaddr (cfg_family, host, &addr);if (mss && -setsockopt (fd,SOL_TCP ,TCP_MAXSEG , &mss,sizeof (mss)) ==-1 ) { +setsockopt (fd,IPPROTO_TCP ,TCP_MAXSEG , &mss,sizeof (mss)) ==-1 ) {perror ("setsockopt TCP_MAXSEG" );exit (1 ); }
Signature · 1 line
2.17.0.441.gb46fe60e1d-goog
Quoted 5 lines · Eric Dumazet
syzbot reported a lockdep issue caused by tcp
mmap()support.I implemented Andy Lutomirski nice suggestions to resolve the issue and increase scalability as well.
Quoted 8 lines
First patch is adding a new
setsockopt()operation and changesmmap()behavior.Second patch changes
tcp_mmapreference program.v2:
Added a missing page align ofzc->lengthintcp_zerocopy_receive()Properly clearzc->recv_skip_hintin case user request was completed.
Thank you Eric for the nice redesign!
Quoted 12 lines
Eric Dumazet (2):
tcp: addTCP_ZEROCOPY_RECEIVEsupport for zerocopy receive
selftests: net:tcp_mmapmust useTCP_ZEROCOPY_RECEIVEinclude/uapi/linux/tcp.h | 8 ++ net/ipv4/tcp.c | 189 +++++++++++++------------ tools/testing/selftests/net/tcp_mmap.c | 63 +++++---- 3 files changed, 142 insertions(+), 118 deletions(-)--
2.17.0.441.gb46fe60e1d-goog
Re: [PATCH v2 net-next 1/2] tcp: add TCP_ZEROCOPY_RECEIVE support for zerocopy receive
In reply to Eric DumazetQuoted 7 lines · Eric Dumazet
When adding tcp
mmap()implementation, I forgot that socket lock had to be taken beforecurrent->mm->mmap_sem. syzbot eventually caught the bug.Since we can not lock the socket in tcp
mmap()handler we have to split the operation in two phases.
Quoted 7 lines
1)
mmap()on a tcp socket simply reserves VMA space, and nothing else. This operation does not involve any TCP locking.2)
setsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, ...)implements the transfert of pages from skbs to one VMA.
This operation only usesdown_read(¤t->mm->mmap_sem)after holding TCP lock, thus solving the lockdep issue.
A quick question. Is it a normal practice to return a result in setsockopt() given that the optval parameter is supposed to be a const void *?
Signature · 2 lines
K. Poon [email protected]
Re: [PATCH v2 net-next 1/2] tcp: add TCP_ZEROCOPY_RECEIVE support for zerocopy receive
Quoted 3 lines · Ka-Cheong Poon
A quick question. Is it a normal practice to return a result in
setsockopt()given that the optval parameter is supposed to be a const void *?
Very good question.
Andy suggested an ioctl() or setsockopt(), and I chose setsockopt() but it looks like a better choice would have been getsockopt() indeed.
This might even allow future changes in "struct tcp_zerocopy_receive"
Willem suggested to add code in tcp_recvmsg() but I prefer to not bloat this already too complex function.
I will send a v3 using getsockopt() then, thanks !
Quoted 4 lines · Soheil Hassas Yeganeh
Thanks Soheil for reviewing.
I have changed setsockopt() to getsockopt() so chose to not carry your Acked-by
Please add it back if you agree, thanks !
At the risk of further muddying the waters, there's another minor tweak that could improve performance on certain workloads. Currently you mmap() a range for a given socket and then getsockopt() to receive. If you made it so you could mmap() something once for any number of sockets (by mmapping /dev/misc/tcp_zero_receive or whatever), then the performance of the getsockopt() bit would be identical, but you could release the mapping for many sockets at once with only a single flush. For some use cases, this could be a big win.
You could also add this later easily enough, too.
Quoted 11 lines · Andy Lutomirski
At the risk of further muddying the waters, there's another minor tweak that could improve performance on certain workloads. Currently you
mmap()a range for a given socket and thengetsockopt()to receive. If you made it so you couldmmap()something once for any number of sockets (by mmapping/dev/misc/tcp_zero_receiveor whatever), then the performance of thegetsockopt()bit would be identical, but you could release the mapping for many sockets at once with only a single flush. For some use cases, this could be a big win.You could also add this later easily enough, too.
I believe I implemented what you just described.
The getsockopt() call checks that the VMA was created by a mmap() to one TCP socket.
It does not check that the vma was created by mmap() on the same socket, because we do not need this extra check really.
So you presumably could use mmap() to grab 1GB of virtual space, then split it as you wish for different sockets.
Thanks.
kbuild test robot
build botRe: [PATCH v2 net-next 1/2] tcp: add TCP_ZEROCOPY_RECEIVE support for zerocopy receive
In reply to Eric DumazetThank you for the patch! Yet something to improve:
Hi Eric,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on net-next/master]
url: https://github.com/0day-ci/linux/commits/Eric-Dumazet/tcp-add-TCP_ZEROCOPY_RECEIVE-support-for-zerocopy-receive/20180427-122234
config: sh-rsk7269_defconfig (attached as .config)
compiler: sh4-linux-gnu-gcc (Debian 7.2.0-11) 7.2.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=sh
All errors (new ones prefixed by >>):
net/ipv4/tcp.o: In function `tcp_setsockopt':
>> tcp.c:(.text+0x3f80): undefined reference to `zap_page_range'
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Re: [PATCH v2 net-next 1/2] tcp: add TCP_ZEROCOPY_RECEIVE support for zerocopy receive
Quoted 7 lines · kbuild test robot
Hi Eric,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on net-next/master]
url:
Quoted 4 lines
config: sh-rsk7269_defconfig (attached as .config)
compiler: sh4-linux-gnu-gcc (Debian 7.2.0-11) 7.2.0 reproduce:
wget
https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
Quoted 8 lines
chmod +x ~/bin/make.cross
# save the attached .config to linux build tree make.cross ARCH=shAll errors (new ones prefixed by >>):
net/ipv4/tcp.o: In function `tcp_setsockopt':Quoted 1 line
Quoted 1 line
tcp.c:(.text+0x3f80): undefined reference to `zap_page_range'
I guess this tcp zerocopy stuff depends on CONFIG_MMU
Thanks.