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=-5.1 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=no 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 BAD05C388F9 for ; Wed, 11 Nov 2020 18:53:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 499EB206FB for ; Wed, 11 Nov 2020 18:53:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mRu0fgSJ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727475AbgKKSx5 (ORCPT ); Wed, 11 Nov 2020 13:53:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727013AbgKKSx5 (ORCPT ); Wed, 11 Nov 2020 13:53:57 -0500 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12AB7C0613D1 for ; Wed, 11 Nov 2020 10:53:57 -0800 (PST) Received: by mail-wr1-x429.google.com with SMTP id l1so3541132wrb.9 for ; Wed, 11 Nov 2020 10:53:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:cc:references:from:autocrypt:subject:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=WrWPo1Dmel0ma582T2tEY6TXI1DN5hsrXHlAWrFyQLM=; b=mRu0fgSJKvqv0n006Z88fd060hCQOJQH6alzaKcly3Kn/Ifo54kBxsx75Ggsk4r1lE f9mu5wIh+d0VZ3LUCTqQaql5BVVPtS63vuEoTCgxjVCyOXWRX8PdVaBRL098YRZcEeEQ HtVQHwWsq2AUXLRTELbhea2MAyYCt9MDECrBSorlDlcoK51op+Vr3H5Wp0d0TYqCBflS yAf5VgiOsfw2MZl/KrTCzXnV/NHJAf7kmOx41uYozhPaxHLQEuBRIdfDCTMTHHtswQF7 mrVNHmqFKmkNvclt5cZF/R1JmR34FxGTLw98hh9UFsraNQutUZGvfzVVpSDoFVYzVT4H cgHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:cc:references:from:autocrypt:subject :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=WrWPo1Dmel0ma582T2tEY6TXI1DN5hsrXHlAWrFyQLM=; b=POl+Jrc5+11/4CI6I1gMj+uxG3XjKI5zc/JA2ZJWhhpQ2sOs7xSj+GvFPiuw0rd3m6 NtO0XSLwZzKXlHIuA9k+5qcaTMSbI767VAGcTOEIho1sG8V0GHZ/pggF2gEIVrEq2hw+ yJTeDHZXp/uWqpUME/etmcwCAnFx90NH2a4qvIioOJLmvpFXVwnFfRruAumQKL70KVVH D4ESlmKyMngFA9GfyZ/M/HCWzt9wtWFLGZO3mlZYn3N2wVjdjmEWUFSU9Mtxpv61wkKq HrpXhIeamoVFkprsxp7i4x6oU+KmylGgbt1Gv0xF3E6rWVIP+wltV4GcxbjnmFtxkMoh mRpA== X-Gm-Message-State: AOAM530wag+6TpGgrkvKkXbcGeCXB0NX+JxYOTtkIt8+xD65vS3ui7wS BrnB07x+6nD10FYJ0dO9dj5Vl333T8UppQ== X-Google-Smtp-Source: ABdhPJwxZShjpmCv1ucH8GmMZAdkryH3c16xYmxI51Wleb187qEyasdXg5ffoTMlv8Sd5yywLRKXLw== X-Received: by 2002:adf:fa82:: with SMTP id h2mr10633808wrr.24.1605120835332; Wed, 11 Nov 2020 10:53:55 -0800 (PST) Received: from [192.168.1.96] (host109-152-100-164.range109-152.btcentralplus.com. [109.152.100.164]) by smtp.gmail.com with ESMTPSA id c8sm3392545wrv.26.2020.11.11.10.53.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 11 Nov 2020 10:53:54 -0800 (PST) To: Victor Stewart Cc: io-uring References: <3913bbb5-50ec-6ad9-13c9-d49a8b7f7e89@gmail.com> From: Pavel Begunkov Autocrypt: addr=asml.silence@gmail.com; prefer-encrypt=mutual; keydata= mQINBFmKBOQBEAC76ZFxLAKpDw0bKQ8CEiYJRGn8MHTUhURL02/7n1t0HkKQx2K1fCXClbps bdwSHrhOWdW61pmfMbDYbTj6ZvGRvhoLWfGkzujB2wjNcbNTXIoOzJEGISHaPf6E2IQx1ik9 6uqVkK1OMb7qRvKH0i7HYP4WJzYbEWVyLiAxUj611mC9tgd73oqZ2pLYzGTqF2j6a/obaqha +hXuWTvpDQXqcOZJXIW43atprH03G1tQs7VwR21Q1eq6Yvy2ESLdc38EqCszBfQRMmKy+cfp W3U9Mb1w0L680pXrONcnlDBCN7/sghGeMHjGKfNANjPc+0hzz3rApPxpoE7HC1uRiwC4et83 CKnncH1l7zgeBT9Oa3qEiBlaa1ZCBqrA4dY+z5fWJYjMpwI1SNp37RtF8fKXbKQg+JuUjAa9 Y6oXeyEvDHMyJYMcinl6xCqCBAXPHnHmawkMMgjr3BBRzODmMr+CPVvnYe7BFYfoajzqzq+h EyXSl3aBf0IDPTqSUrhbmjj5OEOYgRW5p+mdYtY1cXeK8copmd+fd/eTkghok5li58AojCba jRjp7zVOLOjDlpxxiKhuFmpV4yWNh5JJaTbwCRSd04sCcDNlJj+TehTr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6rpodi4Dkdli8HylM5iq4ooeb5VkQ7UZxbCWt UVMKkOCdFhutRmYp0mbv2e87IK4erwNHQRkHUkzbsuym8RVpAZbLzLPIYK/J3RTErL6Z99N2 m3J6pjwSJY/zNwuFPs9zGEnRO4g0BUbwGdbuvDzaq6/3OJLKohr5eLXNU3JkT+3HezydWm3W OPhauth7W0db74Qd49HXK0xe/aPrK+Cp+kU1HRactyNtF8jZQbhMCC8vMGukZtWaAwpjWiiH bA== Subject: Re: io_uring-only sendmsg + recvmsg zerocopy Message-ID: <7e1c3ecd-fe8c-9c1f-e344-974de9cc2b02@gmail.com> Date: Wed, 11 Nov 2020 18:50:50 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org On 11/11/2020 16:49, Victor Stewart wrote: > On Wed, Nov 11, 2020 at 1:00 AM Pavel Begunkov wrote: >> On 11/11/2020 00:07, Victor Stewart wrote: >>> On Tue, Nov 10, 2020 at 11:26 PM Pavel Begunkov wrote: >>>>> NIC ACKs, instead of finding the socket's error queue and putting the >>>>> completion there like MSG_ZEROCOPY, the kernel would find the io_uring >>>>> instance the socket is registered to and call into an io_uring >>>>> sendmsg_zerocopy_completion function. Then the cqe would get pushed >>>>> onto the completion queue.> >>>>> the "recvmsg zerocopy" is straight forward enough. mimicking >>>>> TCP_ZEROCOPY_RECEIVE, i'll go into specifics next time. >>>> >>>> Receive side is inherently messed up. IIRC, TCP_ZEROCOPY_RECEIVE just >>>> maps skbuffs into userspace, and in general unless there is a better >>>> suited protocol (e.g. infiniband with richier src/dst tagging) or a very >>>> very smart NIC, "true zerocopy" is not possible without breaking >>>> multiplexing. >>>> >>>> For registered buffers you still need to copy skbuff, at least because >>>> of security implications. >>> >>> we can actually just force those buffers to be mmap-ed, and then when >>> packets arrive use vm_insert_pin or remap_pfn_range to change the >>> physical pages backing the virtual memory pages submmited for reading >>> via msg_iov. so it's transparent to userspace but still zerocopy. >>> (might require the user to notify io_uring when reading is >>> completed... but no matter). >> >> Yes, with io_uring zerocopy-recv may be done better than >> TCP_ZEROCOPY_RECEIVE but >> 1) it's still a remap. Yes, zerocopy, but not ideal >> 2) won't work with registered buffers, which is basically a set >> of pinned pages that have a userspace mapping. After such remap >> that mapping wouldn't be in sync and that gets messy. > > well unless we can alleviate all copies, then there isn’t any point > because it isn’t zerocopy. > > so in my server, i have a ceiling on the number of clients, > preallocate them, and mmap anonymous noreserve read + write buffers > for each. > > so say, 150,000 clients x (2MB * 2). which is 585GB. way more than the > physical memory of my machine. (and have 10 instance of it per > machine, so ~6TB lol). but at any one time probably 0.01% of that > memory is in usage. and i just MADV_COLD the pages after consumption. > > this provides a persistent “vmem contiguous” stream buffer per client. > which has a litany of benefits. but if we persistently pin pages, this > ceases to work, because pin pages require persistent physical memory > backing pages. > > But on the send side, if you don’t pin persistently, you’d have to pin > on demand, which costs more than it’s worth for sends less than ~10KB. having it non-contiguous and do round-robin IMHO would be a better shot > And I guess there’s no way to avoid pinning and maintain kernel > integrity. Maybe we could erase those userspace -> physical page > mappings, then recreate them once the operation completes, but 1) that > would require page aligned sends so that you could keep writing and > sending while you waited for completions and 2) beyond being > nonstandard and possibly unsafe, who says that would even cost less > than pinning, definitely costs something. Might cost more because > you’d have to get locks to the page table? > > So essentially on the send side the only way to zerocopy for free is > to persistently pin (and give up my per client stream buffers). > > On the receive side actually the only way to realistically do zerocopy > is to somehow pin a NIC RX queue to a process, and then persistently > map the queue into the process’s memory as read only. That’s a > security absurdly in the general case, but it could be root only > usage. Then you’d recvmsg with a NULL msg_iov[0].iov_base, and have > the packet buffer location and length written in. Might require driver > buy-in, so might be impractical, but unsure. https://blogs.oracle.com/linux/zero-copy-networking-in-uek6 scroll to AF_XDP > > Otherwise the only option is an even worse nightmare how > TCP_ZEROCOPY_RECEIVE works, and ridiculously impractical for general > purpose… Well, that's not so bad, API with io_uring might be much better, but still would require unmap. However, depending on a use case overhead for small packets and/or shared b/w many thread mm can potentially be a deal breaker. > “Mapping of memory into a process's address space is done on a > per-page granularity; there is no way to map a fraction of a page. So > inbound network data must be both page-aligned and page-sized when it > ends up in the receive buffer, or it will not be possible to map it > into user space. Alignment can be a bit tricky because the packets > coming out of the interface start with the protocol headers, not the > data the receiving process is interested in. It is the data that must > be aligned, not the headers. Achieving this alignment is possible, but > it requires cooperation from the network interface should support scatter-gather in other words > > It is also necessary to ensure that the data arrives in chunks that > are a multiple of the system's page size, or partial pages of data > will result. That can be done by setting the maximum transfer unit > (MTU) size properly on the interface. That, in turn, can require > knowledge of exactly what the incoming packets will look like; in a > test program posted with the patch set, Dumazet sets the MTU to > 61,512. That turns out to be space for fifteen 4096-byte pages of > data, plus 40 bytes for the IPv6 header and 32 bytes for the TCP > header.” > > https://lwn.net/Articles/752188/ > > Either receive case also makes my persistent per client stream buffer > zerocopy impossible lol. it depends > > in short, zerocopy sendmsg with persistently pinned buffers is > definitely possible and we should do that. (I'll just make it work on > my end). > > recvmsg i'll have to do more research into the practicality of what I > proposed above. 1. NIC is smart enough and can locate the end (userspace) buffer and DMA there directly. That requires parsing TCP/UDP headers, etc., or having a more versatile API like infiniband. + extra NIC features. 2. map skbufs into the userspace as TCP_ZEROCOPY_RECEIVE does. -- Pavel Begunkov