Received: by 2002:a5d:925a:0:0:0:0:0 with SMTP id e26csp210797iol; Thu, 9 Jun 2022 02:13:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyEXnLlhfacLaNCc0Xb26FsJkq+ydiXF2y72wzScoxw2XYnc03FDZLlenSj7Sj8eHoiBafc X-Received: by 2002:a05:6402:e9f:b0:41c:df21:b113 with SMTP id h31-20020a0564020e9f00b0041cdf21b113mr43862049eda.217.1654765983021; Thu, 09 Jun 2022 02:13:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654765983; cv=none; d=google.com; s=arc-20160816; b=CGjU0JU55yWDOXeyxlcUmIN3s/yxXe4CLTiGssDt7TZozfqUNT+zwTWkNemZrXm0rQ cCFLc5ovY2VoJCeT1os5FWb2zAdnYuuHQRNeO0F/qlB38E1URAO1Clooht6Gryaux0Uz IExStVWhVUM/PM4eTausot72Huy7Pm3uphqvd+9B4Nj6MQZSFSN64dHQqQkxRNoqBCe9 2VVVCCTTbi8vdwg6apCt450unQ5fJOsfDrgQjFgKWKq5gGvRXoMvC0Rtplc4j31G54uF Mi+n9sqYkpHlq/FmpVBs4kNMMDztWliw8zUCFsWeG1nxd0fzfaRLcEl+f+y8XSjAP4Gw YsaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=tvWtgkYBzt9DbATf2rlAmzgDtHttB4fvGNf4mSgVDKk=; b=JoWTBQdLX0xGML7Nhz77ecLTXhhvqnd1dVSjC1pnJ95ym+TknTAqCskGH+iqA9Z/iu ibYI7Z3Q2P0/45lNQ4OJqUDYRmtIvWbQiUUVPOID+Co8UspuOYb/emlYU5Ci75KSt9jO fuVb8HpeKOxqWMf1Ekn/+3TzFLZojshzJN6i4tQ0JsJ0fDypy/bAVHz6dIMfSyF24srw qnLwqGGM9PzUHje6itD2hAUYT7y5ZzupnL6e0MmlM8oMG0U96FYr8KBThw8drhEMUmVm aYSIlRdPIrT7LSjzdTnguuUpO42EsGn/qdefpr3dYJPBYT6QielBl8yBGDWNa0BPUHt2 5xSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SD6xeiMH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nc1-20020a1709071c0100b006fee2aa8759si110218ejc.797.2022.06.09.02.12.37; Thu, 09 Jun 2022 02:13:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SD6xeiMH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242775AbiFII4Y (ORCPT + 99 others); Thu, 9 Jun 2022 04:56:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238084AbiFII4O (ORCPT ); Thu, 9 Jun 2022 04:56:14 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0C42F11462 for ; Thu, 9 Jun 2022 01:54:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1654764878; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=tvWtgkYBzt9DbATf2rlAmzgDtHttB4fvGNf4mSgVDKk=; b=SD6xeiMHcx2NrcrocJGhoPPgamJ/nFOkT3OaD0fKeZQ5dZsWOoQ5gXLMiZwi5qY3xlPfya hXS6ms9HPdo/e7MEisaV7ob5/CRgOd+HiiBAJTvZ9626Ai6mBYddmErVvnuVEE45kzs492 a/obPMHzNu95lY2a4WbX8wh4xezLwTE= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-539-K6aVV1WrOo2NRoowoubRYg-1; Thu, 09 Jun 2022 04:54:36 -0400 X-MC-Unique: K6aVV1WrOo2NRoowoubRYg-1 Received: by mail-wr1-f72.google.com with SMTP id r13-20020adff10d000000b002160e9d64f8so3845412wro.0 for ; Thu, 09 Jun 2022 01:54:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=tvWtgkYBzt9DbATf2rlAmzgDtHttB4fvGNf4mSgVDKk=; b=LwJXFatPQAaDHaI5Bmv5lNfiJVx8GZM0JLygUdTwnxPFwrRr76lm9ldPzDaJaHQSpP bCSqHYw3aS0xvPbfuwq0nSvkvfZL8JBKExULaPJn3Y9g0vIkRH5bdhzlhlQuFLAU3GUj ved0whUZc8uZuNdRQma/SgrrlMhb/ngdjdK90BvWapHluzUwcHANuD6ZVEetVYE2zGyw blhWPD85gT11DrTRr2gdkOsJjKwQGskcIOnOdAId0oW+n+j2uOEts2Yh4icawc92e4SR HNBCMGB4SFs5NF2r/Mes8yYkQcaa72BTfD8K24Z7qsupl53XF9gOYLLY9OW6g/RYgfup nKBQ== X-Gm-Message-State: AOAM531coSf6XZoaFzpLRUqCk63AYd+ORyW85jfnzth7HUTI5ZQVMfKl gEYzf8riV/Vq5w+xTkax0i/QQ9CBKjXAIzHxTofuUEnaWrmjpvGVkmK+VAbkgFOZ65VNtYv83OP HS4aEgLtVurURdv40akgfsYzu X-Received: by 2002:adf:e5d0:0:b0:210:313a:e4dc with SMTP id a16-20020adfe5d0000000b00210313ae4dcmr36674907wrn.152.1654764875097; Thu, 09 Jun 2022 01:54:35 -0700 (PDT) X-Received: by 2002:adf:e5d0:0:b0:210:313a:e4dc with SMTP id a16-20020adfe5d0000000b00210313ae4dcmr36674875wrn.152.1654764874842; Thu, 09 Jun 2022 01:54:34 -0700 (PDT) Received: from sgarzare-redhat (host-79-46-200-40.retail.telecomitalia.it. [79.46.200.40]) by smtp.gmail.com with ESMTPSA id s19-20020a1cf213000000b0039c4945c753sm15291073wmc.39.2022.06.09.01.54.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jun 2022 01:54:33 -0700 (PDT) Date: Thu, 9 Jun 2022 10:54:28 +0200 From: Stefano Garzarella To: Arseniy Krasnov Cc: Stefan Hajnoczi , "Michael S. Tsirkin" , Jason Wang , "David S. Miller" , Jakub Kicinski , Paolo Abeni , "linux-kernel@vger.kernel.org" , "kvm@vger.kernel.org" , "virtualization@lists.linux-foundation.org" , "netdev@vger.kernel.org" , kernel , Krasnov Arseniy Subject: Re: [RFC PATCH v2 0/8] virtio/vsock: experimental zerocopy receive Message-ID: <20220609085428.idi4qzydhdpzszzw@sgarzare-redhat> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-3.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Arseniy, I left some comments in the patches, and I'm adding something also here: On Fri, Jun 03, 2022 at 05:27:56AM +0000, Arseniy Krasnov wrote: > INTRODUCTION > > Hello, this is experimental implementation of virtio vsock zerocopy >receive. It was inspired by TCP zerocopy receive by Eric Dumazet. This API uses >same idea: call 'mmap()' on socket's descriptor, then every 'getsockopt()' will >fill provided vma area with pages of virtio RX buffers. After received data was >processed by user, pages must be freed by 'madvise()' call with MADV_DONTNEED >flag set(if user won't call 'madvise()', next 'getsockopt()' will >fail). If it is not too time-consuming, can we have a table/list to compare this and the TCP zerocopy? > > DETAILS > > Here is how mapping with mapped pages looks exactly: first page mapping >contains array of trimmed virtio vsock packet headers (in contains only length >of data on the corresponding page and 'flags' field): > > struct virtio_vsock_usr_hdr { > uint32_t length; > uint32_t flags; > uint32_t copy_len; > }; > >Field 'length' allows user to know exact size of payload within each sequence >of pages and 'flags' allows user to handle SOCK_SEQPACKET flags(such as message >bounds or record bounds). Field 'copy_len' is described below in 'v1->v2' part. >All other pages are data pages from RX queue. > > Page 0 Page 1 Page N > > [ hdr1 .. hdrN ][ data ] .. [ data ] > | | ^ ^ > | | | | > | *-------------------* > | | > | | > *----------------* > > Of course, single header could represent array of pages (when packet's >buffer is bigger than one page).So here is example of detailed mapping layout >for some set of packages. Lets consider that we have the following sequence of >packages: 56 bytes, 4096 bytes and 8200 bytes. All pages: 0,1,2,3,4 and 5 will >be inserted to user's vma(vma is large enough). In order to have a "userspace polling-friendly approach" and reduce number of syscall, can we allow for example the userspace to mmap at least the first header before packets arrive. Then the userspace can poll a flag or other fields in the header to understand that there are new packets. That would be cool, but in the meantime it would be nice to behave similarly to TCP, which is why the comparison table I mentioned earlier would be useful. > > Page 0: [[ hdr0 ][ hdr 1 ][ hdr 2 ][ hdr 3 ] ... ] > Page 1: [ 56 ] > Page 2: [ 4096 ] > Page 3: [ 4096 ] > Page 4: [ 4096 ] > Page 5: [ 8 ] > > Page 0 contains only array of headers: > 'hdr0' has 56 in length field. > 'hdr1' has 4096 in length field. > 'hdr2' has 8200 in length field. > 'hdr3' has 0 in length field(this is end of data marker). > > Page 1 corresponds to 'hdr0' and has only 56 bytes of data. > Page 2 corresponds to 'hdr1' and filled with data. > Page 3 corresponds to 'hdr2' and filled with data. > Page 4 corresponds to 'hdr2' and filled with data. > Page 5 corresponds to 'hdr2' and has only 8 bytes of data. > > This patchset also changes packets allocation way: today implementation >uses only 'kmalloc()' to create data buffer. Problem happens when we try to map >such buffers to user's vma - kernel forbids to map slab pages to user's vma(as >pages of "not large" 'kmalloc()' allocations are marked with PageSlab flag and >"not large" could be bigger than one page). So to avoid this, data buffers now >allocated using 'alloc_pages()' call. > > TESTS > > This patchset updates 'vsock_test' utility: two tests for new feature >were added. First test covers invalid cases. Second checks valid transmission >case. > > BENCHMARKING > > For benchmakring I've added small utility 'rx_zerocopy'. It works in >client/server mode. When client connects to server, server starts sending exact >amount of data to client(amount is set as input argument).Client reads data and >waits for next portion of it. Client works in two modes: copy and zero-copy. In >copy mode client uses 'read()' call while in zerocopy mode sequence of 'mmap()' >/'getsockopt()'/'madvise()' are used. Smaller amount of time for transmission >is better. For server, we can set size of tx buffer and for client we can set >size of rx buffer or rx mapping size(in zerocopy mode). Usage of this utility >is quiet simple: > >For client mode: > >./rx_zerocopy --mode client [--zerocopy] [--rx] > >For server mode: > >./rx_zerocopy --mode server [--mb] [--tx] > >[--mb] sets number of megabytes to transfer. >[--rx] sets size of receive buffer/mapping in pages. >[--tx] sets size of transmit buffer in pages. > >I checked for transmission of 4000mb of data. Here are some results: > > size of rx/tx buffers in pages > *---------------------------------------------------* > | 8 | 32 | 64 | 256 | 512 | >*--------------*--------*----------*---------*----------*----------* >| zerocopy | 24 | 10.6 | 12.2 | 23.6 | 21 | secs to >*--------------*---------------------------------------------------- process >| non-zerocopy | 13 | 16.4 | 24.7 | 27.2 | 23.9 | 4000 mb >*--------------*---------------------------------------------------- > >Result in first column(where non-zerocopy works better than zerocopy) happens >because time, spent in 'read()' system call is smaller that time in 'getsockopt' >+ 'madvise'. I've checked that. > >I think, that results are not so impressive, but at least it is not worse than >copy mode and there is no need to allocate memory for processing date. > > PROBLEMS > > Updated packet's allocation logic creates some problem: when host gets >data from guest(in vhost-vsock), it allocates at least one page for each packet >(even if packet has 1 byte payload). I think this could be resolved in several >ways: > 1) Make zerocopy rx mode disabled by default, so if user didn't enable >it, current 'kmalloc()' way will be used. <<<<<<< (IMPLEMENTED IN V2) Yep, but I think we should not allow to change it while we are connected (see comments in the patches.) > 2) Use 'kmalloc()' for "small" packets, else call page allocator. But >in this case, we have mix of packets, allocated in two different ways thus >during zerocopying to user(e.g. mapping pages to vma), such small packets will >be handled in some stupid way: we need to allocate one page for user, copy data >to it and then insert page to user's vma. > >v1 -> v2: > 1) Zerocopy receive mode could be enabled/disabled(disabled by default). I > didn't use generic SO_ZEROCOPY flag, because in virtio-vsock case this > feature depends on transport support. Instead of SO_ZEROCOPY, AF_VSOCK > layer flag was added: SO_VM_SOCKETS_ZEROCOPY, while previous meaning of > SO_VM_SOCKETS_ZEROCOPY(insert receive buffers to user's vm area) now > renamed to SO_VM_SOCKETS_MAP_RX. > 2) Packet header which is exported to user now get new field: 'copy_len'. > This field handles special case: user reads data from socket in non > zerocopy way(with disabled zerocopy) and then enables zerocopy feature. > In this case vhost part will switch data buffer allocation logic from > 'kmalloc()' to direct calls for buddy allocator. But, there could be > some pending 'kmalloc()' allocated packets in socket's rx list, and then > user tries to read such packets in zerocopy way, dequeue will fail, > because SLAB pages could not be inserted to user's vm area. So when such > packet is found during zerocopy dequeue, dequeue loop will break and > 'copy_len' will show size of such "bad" packet. After user detects this > case, it must use 'read()/recv()' calls to dequeue such packet. > 3) Also may be move this features under config option? Do you mean a build config like CONFIG_VSOCK_ZERO_COPY? I'm not sure it's needed. Thanks, Stefano