From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.3 required=3.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D3A46C33CA4 for ; Sat, 11 Jan 2020 00:29:25 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8A9492082E for ; Sat, 11 Jan 2020 00:29:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="oitzAvxb" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8A9492082E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 0CBA38E0008; Fri, 10 Jan 2020 19:29:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 054528E0001; Fri, 10 Jan 2020 19:29:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E5F5D8E0008; Fri, 10 Jan 2020 19:29:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0076.hostedemail.com [216.40.44.76]) by kanga.kvack.org (Postfix) with ESMTP id CEFD68E0001 for ; Fri, 10 Jan 2020 19:29:24 -0500 (EST) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with SMTP id 72DEE181AEF00 for ; Sat, 11 Jan 2020 00:29:24 +0000 (UTC) X-FDA: 76363469448.19.beam72_521239a127231 X-HE-Tag: beam72_521239a127231 X-Filterd-Recvd-Size: 7144 Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) by imf31.hostedemail.com (Postfix) with ESMTP for ; Sat, 11 Jan 2020 00:29:23 +0000 (UTC) Received: by mail-pg1-f193.google.com with SMTP id x7so1744677pgl.11 for ; Fri, 10 Jan 2020 16:29:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=1LDI4J9RjcFLtSkqnZ1wNc4xIuHBN5WiIppT7x73FiY=; b=oitzAvxblJcT6HGYXN37tqOcTuVllVo4oE3LuXvven6wfJ8p78mTXgRCOKE6vhpIrI 5gpd2HOJZTNUmqtS8pKj9qXOsbypMvTLm/krIkJTC6XR7NRJhSBiApNG5QMUtofSvGDC 0+U8Bpm5nyTcHp9nckqbASiV/Wpyw1oNXghPmIoGVh9ylTdiilfHVQSeqSVFzorNop26 TaGUXEa4OyIU1ERTjy8aEcnBGJGhp21SWtKWbqhVZGnjYkw3l2U1htDRjopxKLrkwfCU fsL5KkFLXNYoQn06r3Ph4OOFZFQvKG7I9SYNX3v8E+H/kQnkW5j9OgjW3Iq4ouHE82EX 1OiA== 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:mime-version:content-transfer-encoding; bh=1LDI4J9RjcFLtSkqnZ1wNc4xIuHBN5WiIppT7x73FiY=; b=tjBGDTfwsvsGTqhEHLgSDKgqhfQBHfAEfCsQThEoKSwAZVdm51yCxaM9aT1oU2Dwr1 d0nepNV1jrkLjn0OYgbY4PuLkuLTgfLfogApScpe+4pXPM7zXg9KIDOHMCjVb9e+Om+2 4lpfrmzTMOBLb4CM1QC/5ueaXOxieeN5W10MZ8laDAS5c3yA6TfUUbVm5qCNUDjLpoa0 2MjZFr8SuwS7MLm5y/Vs4Cd3XzJfs+ktmrvPUUXMgapCEFGhjrda+55rTF80lG/jlvPu iDtgOyCni8Qfxm9cE2q8wQBlR8Xc9j78qrIkw7jwkYI2arsZimNcHHwiG0rF/ZfmzuTS 6V6w== X-Gm-Message-State: APjAAAXAw9Bl7xtpLlUxtrtH6gZWMxval57eA+RqtfwHjAcu4FBjIuoU qnfYsBOMAlqp+KczRGSu4+Y= X-Google-Smtp-Source: APXvYqynZPWxev5HGGPaDMT4SeA3bIUYAeFJGYDi8krQcaLK/IaE5Dvc8g5CvUIMBFJWYPXZuzz2rQ== X-Received: by 2002:a63:fe50:: with SMTP id x16mr7557165pgj.31.1578702563076; Fri, 10 Jan 2020 16:29:23 -0800 (PST) Received: from phantasmagoria.svl.corp.google.com ([2620:15c:2c4:201:2b0a:8c1:6a84:1aa0]) by smtp.gmail.com with ESMTPSA id h126sm4567066pfe.19.2020.01.10.16.29.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jan 2020 16:29:22 -0800 (PST) From: Arjun Roy To: davem@davemloft.net, netdev@vger.kernel.org, akpm@linux-foundation.org, linux-mm@kvack.org Cc: arjunroy@google.com, Arjun Roy , Eric Dumazet , Soheil Hassas Yeganeh Subject: [PATCH mm,net-next 3/3] net-zerocopy: Use vm_insert_pages() for tcp rcv zerocopy. Date: Fri, 10 Jan 2020 16:28:49 -0800 Message-Id: <20200111002849.252704-3-arjunroy.kdev@gmail.com> X-Mailer: git-send-email 2.25.0.rc1.283.g88dfdc4193-goog In-Reply-To: <20200111002849.252704-1-arjunroy.kdev@gmail.com> References: <20200111002849.252704-1-arjunroy.kdev@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Use vm_insert_pages() for tcp receive zerocopy. Spin lock cycles (as reported by perf) drop from a couple of percentage points to a fraction of a percent. This results in a roughly 6% increase in efficiency, measured roughly as zerocopy receive count divided by CPU utilization. The intention of this patch-set is to reduce atomic ops for tcp zerocopy receives, which normally hits the same spinlock multiple times consecutively. Signed-off-by: Arjun Roy Signed-off-by: Eric Dumazet Signed-off-by: Soheil Hassas Yeganeh --- net/ipv4/tcp.c | 67 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 34490d972758..52f96c3ceab3 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -1861,14 +1861,48 @@ int tcp_mmap(struct file *file, struct socket *so= ck, } EXPORT_SYMBOL(tcp_mmap); =20 +static int tcp_zerocopy_vm_insert_batch(struct vm_area_struct *vma, + struct page **pages, + unsigned long pages_to_map, + unsigned long *insert_addr, + u32 *length_with_pending, + u32 *seq, + struct tcp_zerocopy_receive *zc) +{ + unsigned long pages_remaining =3D pages_to_map; + int bytes_mapped; + int ret; + + ret =3D vm_insert_pages(vma, *insert_addr, pages, &pages_remaining); + bytes_mapped =3D PAGE_SIZE * (pages_to_map - pages_remaining); + /* Even if vm_insert_pages fails, it may have partially succeeded in + * mapping (some but not all of the pages). + */ + *seq +=3D bytes_mapped; + *insert_addr +=3D bytes_mapped; + if (ret) { + /* But if vm_insert_pages did fail, we have to unroll some state + * we speculatively touched before. + */ + const int bytes_not_mapped =3D PAGE_SIZE * pages_remaining; + *length_with_pending -=3D bytes_not_mapped; + zc->recv_skip_hint +=3D bytes_not_mapped; + } + return ret; +} + static int tcp_zerocopy_receive(struct sock *sk, struct tcp_zerocopy_receive *zc) { unsigned long address =3D (unsigned long)zc->address; u32 length =3D 0, seq, offset, zap_len; + #define PAGE_BATCH_SIZE 8 + struct page *pages[PAGE_BATCH_SIZE]; const skb_frag_t *frags =3D NULL; struct vm_area_struct *vma; struct sk_buff *skb =3D NULL; + unsigned long pg_idx =3D 0; + unsigned long curr_addr; struct tcp_sock *tp; int inq; int ret; @@ -1901,15 +1935,26 @@ static int tcp_zerocopy_receive(struct sock *sk, zc->recv_skip_hint =3D zc->length; } ret =3D 0; + curr_addr =3D address; while (length + PAGE_SIZE <=3D zc->length) { if (zc->recv_skip_hint < PAGE_SIZE) { + /* If we're here, finish the current batch. */ + if (pg_idx) { + ret =3D tcp_zerocopy_vm_insert_batch(vma, pages, + pg_idx, + &curr_addr, + &length, + &seq, zc); + if (ret) + goto out; + pg_idx =3D 0; + } if (skb) { skb =3D skb->next; offset =3D seq - TCP_SKB_CB(skb)->seq; } else { skb =3D tcp_recv_skb(sk, seq, &offset); } - zc->recv_skip_hint =3D skb->len - offset; offset -=3D skb_headlen(skb); if ((int)offset < 0 || skb_has_frag_list(skb)) @@ -1933,14 +1978,24 @@ static int tcp_zerocopy_receive(struct sock *sk, zc->recv_skip_hint -=3D remaining; break; } - ret =3D vm_insert_page(vma, address + length, - skb_frag_page(frags)); - if (ret) - break; + pages[pg_idx] =3D skb_frag_page(frags); + pg_idx++; length +=3D PAGE_SIZE; - seq +=3D PAGE_SIZE; zc->recv_skip_hint -=3D PAGE_SIZE; frags++; + if (pg_idx =3D=3D PAGE_BATCH_SIZE) { + ret =3D tcp_zerocopy_vm_insert_batch(vma, pages, pg_idx, + &curr_addr, &length, + &seq, zc); + if (ret) + goto out; + pg_idx =3D 0; + } + } + if (pg_idx) { + ret =3D tcp_zerocopy_vm_insert_batch(vma, pages, pg_idx, + &curr_addr, &length, &seq, + zc); } out: up_read(¤t->mm->mmap_sem); --=20 2.25.0.rc1.283.g88dfdc4193-goog