Received: by 10.192.165.148 with SMTP id m20csp1218810imm; Wed, 25 Apr 2018 14:45:02 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/2oH6tBy9yl+dHrJDh3CGX3h1CkNdtJd4BfbV2kwKzuNMHSJR3jOFxxyOT8Jd9HPSAEVGX X-Received: by 10.99.178.83 with SMTP id t19mr25513373pgo.212.1524692702633; Wed, 25 Apr 2018 14:45:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524692702; cv=none; d=google.com; s=arc-20160816; b=Msfc4we127h+cXgQ3Cb0cvir93yqew8yW03zwcWHL0KMESLnlOktEJXE0PVthFLmwX yiX3bjAy1QgyfRcUla0Hl+WGQgCiublY1O6vVDi+k+38v/151p+PhEG5SkCzurtrhxBw 4B0Whl64pLlLDKKuwC3nmfeHL9zQlaTBiiHkVo1TZkQuQ4kgvPrOKQOWy2cbjM2S96OU iD2PAOADb3ht99cclU2ueJFl6htcV9hlZmnjwl1+E5X9KZSs0s1ndBG5ASDCoHIk9O38 IVDBkgNUXSfZeJbKVkiyVgJCEV/nThQ1fbeF3xgnup2JF28rvxjbvwwnHdv1bgGRAUlK olIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=+15XHRYK430ByXmOgUPFKX2JoGE/fQbV1F0dvGKXG1Y=; b=BnzNelgUUZpoCN4uWm+OEymkJnlb05cNSAZfoeBYMHJf0va3PhGtdmjg35wIq3aQ2e N0a8SFNm0VMnrgtoQi1LnofYfdO0pseqS+aO3KEJ5LMllJGmS9g2EBXMnMbVLSTnHfhH gPQRrFBsRE6+XwJFe8+OrZl207739YWxTai47d2pxbCJWWMlINE4ujExz06luy1wy6k6 chHfUC9nfWc5HoPwj9T34Vh1m1dCshSSYj+bM+H5vm0FFw6dMTTT+yEMeDKMeCCKOXOd mdpYElc0lvAJCjTQqDYWMh+WoZXInVwhfjxrDdqO6bHks5jsFrxqM7cq3/9xIjGIJfgu pyzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=pCZb7KWb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x188si14604049pgb.453.2018.04.25.14.44.48; Wed, 25 Apr 2018 14:45:02 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=pCZb7KWb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753605AbeDYVnc (ORCPT + 99 others); Wed, 25 Apr 2018 17:43:32 -0400 Received: from mail-pg0-f47.google.com ([74.125.83.47]:37864 "EHLO mail-pg0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753377AbeDYVnU (ORCPT ); Wed, 25 Apr 2018 17:43:20 -0400 Received: by mail-pg0-f47.google.com with SMTP id a13so9807236pgu.4 for ; Wed, 25 Apr 2018 14:43:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=+15XHRYK430ByXmOgUPFKX2JoGE/fQbV1F0dvGKXG1Y=; b=pCZb7KWbE/fcDkNrdWWEes1Nr2Uw1hjDJTRvK1fNhxGPXzUdXw2zQ+F3pGYnAbrOpk YlyIgFeO2CSZ4344WZ5sa02vZ36yyOPVCjpjbmYLdZF5QCv3SGvokPlSVa8n46okHp0J A2wHCfsZjJzm6OhNSJb34OLeG1L8HlKDQ159vCPufkZlpZBocdoqy9btz2fTk4a/Dzv5 LJqkcFsyy4GFlpmGy973yniZMF5Eo+SwjCgCdKE+jGbIx5LRQcQUddYR9WpOtI8GAOfD ST5p81zFXs3n3q1WQMBOnAgVvvjv46ByHqLAOKnDnWIvpBwDc61DfoZbCTct5zmiz04+ 2D0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=+15XHRYK430ByXmOgUPFKX2JoGE/fQbV1F0dvGKXG1Y=; b=tf5IebuRiFPmsMZ/jmeJqY7JoGFfqUje3qEmlEAQzhJ73p0RP9ugb2rYV3dZcgMRkd nIH0rrs26HIQ75j2v3EhkE1QAgCWcwWEYxAmWG7T0Dw7bbGzxAHbgUl+M7KCfhSr1yC0 hsrQvts4+EEthHb/M0jjPK4kY9fK6v2qyGuxkmRG/vgE6FFt17o9S4yF3gdzAnmxFNNL 4SRrR4vQWtopubCneLaTMsgqGVv8wWQO8i1o6Uh7eSV8jq+sxgDou2p5cPtVB9mEnLzD TCEf6DgMN5AS043tWu+wbbj7zPDb5TkDr5IywTAaZ8tvMveqhOyaNPnVLZaIBF4y32aS ZnQQ== X-Gm-Message-State: ALQs6tA559aRO3sGjd3auxCyV9ug/LPwAYsJEMVUvPus5i7NXSvdM4XN mzKajM2yf0r/t2p2O8VZqHaxhw== X-Received: by 10.98.236.220 with SMTP id e89mr29707822pfm.33.1524692599232; Wed, 25 Apr 2018 14:43:19 -0700 (PDT) Received: from localhost ([2620:15c:2c4:201:f5a:7eca:440a:3ead]) by smtp.gmail.com with ESMTPSA id w1sm33616472pfd.36.2018.04.25.14.43.17 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 25 Apr 2018 14:43:17 -0700 (PDT) From: Eric Dumazet To: "David S . Miller" Cc: netdev , Andy Lutomirski , linux-kernel , linux-mm , Eric Dumazet , Eric Dumazet , Soheil Hassas Yeganeh Subject: [PATCH v2 net-next 1/2] tcp: add TCP_ZEROCOPY_RECEIVE support for zerocopy receive Date: Wed, 25 Apr 2018 14:43:06 -0700 Message-Id: <20180425214307.159264-2-edumazet@google.com> X-Mailer: git-send-email 2.17.0.441.gb46fe60e1d-goog In-Reply-To: <20180425214307.159264-1-edumazet@google.com> References: <20180425214307.159264-1-edumazet@google.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Fixes: 93ab6cc69162 ("tcp: implement mmap() for zero copy receive") Signed-off-by: Eric Dumazet Reported-by: syzbot Suggested-by: Andy Lutomirski Cc: linux-mm@kvack.org Cc: Soheil Hassas Yeganeh --- include/uapi/linux/tcp.h | 8 ++ net/ipv4/tcp.c | 189 ++++++++++++++++++++------------------- 2 files changed, 106 insertions(+), 91 deletions(-) diff --git a/include/uapi/linux/tcp.h b/include/uapi/linux/tcp.h index 379b08700a542d49bbce9b4b49b17879d00b69bb..e9e8373b34b9ddc735329341b91f455bf5c0b17c 100644 --- a/include/uapi/linux/tcp.h +++ b/include/uapi/linux/tcp.h @@ -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 35 struct 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 */ diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index dfd090ea54ad47112fc23c61180b5bf8edd2c736..9b9cbb837ff8d6cdd85515429f699e113c55b37b 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -1726,118 +1726,111 @@ int tcp_set_rcvlowat(struct sock *sk, int val) } EXPORT_SYMBOL(tcp_set_rcvlowat); -/* When user wants to mmap X pages, we first need to perform the mapping - * before freeing any skbs in receive queue, otherwise user would be unable - * to fallback to standard recvmsg(). This happens if some data in the - * requested block is not exactly fitting in a page. - * - * We only support order-0 pages for the moment. - * mmap() on TCP is very strict, there is no point - * trying to accommodate with pathological layouts. - */ +static const struct vm_operations_struct tcp_vm_ops = { +}; + int tcp_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma) { - unsigned long size = vma->vm_end - vma->vm_start; - unsigned int nr_pages = size >> PAGE_SHIFT; - struct page **pages_array = NULL; - u32 seq, len, offset, nr = 0; - struct sock *sk = sock->sk; - const skb_frag_t *frags; + if (vma->vm_flags & (VM_WRITE | VM_EXEC)) + return -EPERM; + vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC); + + /* Instruct vm_insert_page() to not down_read(mmap_sem) */ + vma->vm_flags |= VM_MIXEDMAP; + + vma->vm_ops = &tcp_vm_ops; + return 0; +} +EXPORT_SYMBOL(tcp_mmap); + +static int tcp_zerocopy_receive(struct sock *sk, + struct tcp_zerocopy_receive *zc) +{ + unsigned long address = (unsigned long)zc->address; + const skb_frag_t *frags = NULL; + u32 length = 0, seq, offset; + struct vm_area_struct *vma; + struct sk_buff *skb = NULL; struct tcp_sock *tp; - struct sk_buff *skb; int ret; - if (vma->vm_pgoff || !nr_pages) + if (address & (PAGE_SIZE - 1) || address != zc->address) return -EINVAL; - if (vma->vm_flags & VM_WRITE) - return -EPERM; - /* 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; + return -ENOTCONN; sock_rps_record_flow(sk); - if (tcp_inq(sk) < size) { - ret = sock_flag(sk, SOCK_DONE) ? -EIO : -EAGAIN; + down_read(¤t->mm->mmap_sem); + + ret = -EINVAL; + vma = find_vma(current->mm, address); + if (!vma || vma->vm_start > address || vma->vm_ops != &tcp_vm_ops) goto out; - } + zc->length = min_t(unsigned long, zc->length, vma->vm_end - address); + tp = tcp_sk(sk); seq = tp->copied_seq; - /* Abort if urgent data is in the area */ - if (unlikely(tp->urg_data)) { - u32 urg_offset = tp->urg_seq - seq; + zc->length = min_t(u32, zc->length, tcp_inq(sk)); + zc->length &= ~(PAGE_SIZE - 1); - ret = -EINVAL; - if (urg_offset < size) - goto out; - } - ret = -ENOMEM; - pages_array = kvmalloc_array(nr_pages, sizeof(struct page *), - GFP_KERNEL); - if (!pages_array) - goto out; - skb = tcp_recv_skb(sk, seq, &offset); - ret = -EINVAL; -skb_start: - /* We do not support anything not in page frags */ - offset -= skb_headlen(skb); - if ((int)offset < 0) - goto out; - if (skb_has_frag_list(skb)) - goto out; - len = skb->data_len - offset; - frags = skb_shinfo(skb)->frags; - while (offset) { - if (frags->size > offset) - goto out; - offset -= frags->size; - frags++; - } - while (nr < nr_pages) { - if (len) { - if (len < PAGE_SIZE) - goto out; - if (frags->size != PAGE_SIZE || frags->page_offset) - goto out; - pages_array[nr++] = skb_frag_page(frags); - frags++; - len -= PAGE_SIZE; - seq += PAGE_SIZE; - continue; - } - skb = skb->next; - offset = seq - TCP_SKB_CB(skb)->seq; - goto skb_start; - } - /* OK, we have a full set of pages ready to be inserted into vma */ - for (nr = 0; nr < nr_pages; nr++) { - ret = vm_insert_page(vma, vma->vm_start + (nr << PAGE_SHIFT), - pages_array[nr]); - if (ret) - goto out; - } - /* operation is complete, we can 'consume' all skbs */ - tp->copied_seq = seq; - tcp_rcv_space_adjust(sk); - - /* Clean up data we have read: This will do ACK frames. */ - tcp_recv_skb(sk, seq, &offset); - tcp_cleanup_rbuf(sk, size); + zap_page_range(vma, address, zc->length); + zc->recv_skip_hint = 0; ret = 0; + while (length + PAGE_SIZE <= zc->length) { + if (zc->recv_skip_hint < PAGE_SIZE) { + if (skb) { + skb = skb->next; + offset = seq - TCP_SKB_CB(skb)->seq; + } else { + skb = tcp_recv_skb(sk, seq, &offset); + } + + zc->recv_skip_hint = skb->len - offset; + offset -= skb_headlen(skb); + if ((int)offset < 0 || skb_has_frag_list(skb)) + break; + frags = skb_shinfo(skb)->frags; + while (offset) { + if (frags->size > offset) + goto out; + offset -= frags->size; + frags++; + } + } + if (frags->size != PAGE_SIZE || frags->page_offset) + break; + ret = vm_insert_page(vma, address + length, + skb_frag_page(frags)); + if (ret) + break; + length += PAGE_SIZE; + seq += PAGE_SIZE; + zc->recv_skip_hint -= PAGE_SIZE; + frags++; + } out: - release_sock(sk); - kvfree(pages_array); + up_read(¤t->mm->mmap_sem); + if (length) { + tp->copied_seq = seq; + tcp_rcv_space_adjust(sk); + + /* Clean up data we have read: This will do ACK frames. */ + tcp_recv_skb(sk, seq, &offset); + tcp_cleanup_rbuf(sk, length); + ret = 0; + if (length == zc->length) + zc->recv_skip_hint = 0; + } else { + if (!zc->recv_skip_hint && sock_flag(sk, SOCK_DONE)) + ret = -EIO; + } + zc->length = length; return ret; } -EXPORT_SYMBOL(tcp_mmap); static void tcp_update_recv_tstamps(struct sk_buff *skb, struct scm_timestamping *tss) @@ -2738,6 +2731,20 @@ static int do_tcp_setsockopt(struct sock *sk, int level, return tcp_fastopen_reset_cipher(net, sk, key, sizeof(key)); } + case TCP_ZEROCOPY_RECEIVE: { + struct tcp_zerocopy_receive zc; + + if (optlen != sizeof(zc)) + return -EINVAL; + if (copy_from_user(&zc, optval, optlen)) + return -EFAULT; + lock_sock(sk); + err = tcp_zerocopy_receive(sk, &zc); + release_sock(sk); + if (!err && copy_to_user(optval, &zc, optlen)) + err = -EFAULT; + return err; + } default: /* fallthru */ break; -- 2.17.0.441.gb46fe60e1d-goog