Message ID | f896b8fd-50d2-2512-3966-3775245e9b96@sberdevices.ru |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1644792wru; Sun, 6 Nov 2022 11:37:41 -0800 (PST) X-Google-Smtp-Source: AMsMyM73xTEUplzjDQg99+upklI0D6c6cNeG37Qkt/3xNOWrMAVfasqmjUS+x+uKsI2XGQlVEiQT X-Received: by 2002:a17:90b:60e:b0:212:d5cd:4e58 with SMTP id gb14-20020a17090b060e00b00212d5cd4e58mr48286223pjb.165.1667763461034; Sun, 06 Nov 2022 11:37:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667763461; cv=none; d=google.com; s=arc-20160816; b=Uo+7pMha59qMGDF8gU4Zahy67Gz0mmcck0nWCgZc6C7Ery9ahiPX23U8LUeHhJeZBh +cRAe3Df7/Rt9XRyUgr/+wTpmtCcHuYnMISRQSxHGQaF29SvMhHXe44Ey1ru7PXnQQGg BEoz17XriVTBMJAFObzNAMeOpvRiDkZRfAUCD6SnGulH4DWpt6pp3SgtjeeSEkYWQXdv HKmLjbI/3NmyW9TWs+6fnszoFyUFx3vyZO1zabQceuqeZJMk6689TasiagAWFD1aFKOY /MA20PHBn8Sfx0yMFfNkRWhopZ/Bhk24T5bV+A3F5tTzDNaaKp+rDp4dxq0eBzouEbM7 y6gw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :content-id:content-language:accept-language:in-reply-to:message-id :date:thread-index:thread-topic:subject:cc:to:from:dkim-signature; bh=yQ6suEWJBzxoUrocwoSmUoXHvbi6PvhSnqawY+nZJZ8=; b=xXyzaxOM+10Qm85/Dqp+o5O0ntdswiWp8q4A2UtFhjYPVr4vo1tOpcVwOnIbq3HVv6 /J8TJmGE2CD8G22Dq18QERS3hJnFl96R0MDcWwg8Xo9Y4Tm99vmy3NHYn5DvmPj76lbQ U+fFZEWYZApD/u8PpkI/FFVJS2GynzzsI+iN6fqNXBag/uO2dule259ynyiBvOzQOkJD iyRk9cjL9gjR6EM7gYE4x9rxaX3zs1r4eo4VYMJUPeNbTI6rhDXXckNh9HG7ViTFyWRB RZ/xatN1rMY7djtSiNlY5SLnVoKvi0594QwlIv0lH/1zJVBcEJH4rmJwFxIUiU55lm9R cxuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sberdevices.ru header.s=mail header.b=YV1yrrDK; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sberdevices.ru Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k186-20020a6384c3000000b0047005e8d874si8093372pgd.9.2022.11.06.11.37.28; Sun, 06 Nov 2022 11:37:41 -0800 (PST) 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=@sberdevices.ru header.s=mail header.b=YV1yrrDK; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sberdevices.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230099AbiKFTgj (ORCPT <rfc822;hjfbswb@gmail.com> + 99 others); Sun, 6 Nov 2022 14:36:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229947AbiKFTgh (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sun, 6 Nov 2022 14:36:37 -0500 Received: from mx.sberdevices.ru (mx.sberdevices.ru [45.89.227.171]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0998BE084; Sun, 6 Nov 2022 11:36:35 -0800 (PST) Received: from s-lin-edge02.sberdevices.ru (localhost [127.0.0.1]) by mx.sberdevices.ru (Postfix) with ESMTP id 5438B5FD04; Sun, 6 Nov 2022 22:36:33 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sberdevices.ru; s=mail; t=1667763393; bh=yQ6suEWJBzxoUrocwoSmUoXHvbi6PvhSnqawY+nZJZ8=; h=From:To:Subject:Date:Message-ID:Content-Type:MIME-Version; b=YV1yrrDKglvFQLa34c6eqjzoqkTQQDdNQdyDxpaC6X/S6vOK5GLypskjvi+zp++Dm AhAfoS6eplCCgnaql70kq2zZIgZ4x20bZ15dsHACujm9LlO/N2UjuvrVMWWaIVbmy6 /q8GXlCZXIo9wDlCZKG/+dDz7dr2VwRtVrDXVsEl97SFZ3HreObFKRGRnZOBbn+P8v K9cSd98Nbz19+MBQq04/Y386P6CS1NzpglnFgdgaJwbgJFSzYpStKvR15+PLcozJUf E+6Mi/7mMASbAzeDOfaC75mlFgGA73t1xEFi0/+4kOB7mrjmx0Rl7/LceX84b6e8qq wlREl7z+nWDVw== Received: from S-MS-EXCH01.sberdevices.ru (S-MS-EXCH01.sberdevices.ru [172.16.1.4]) by mx.sberdevices.ru (Postfix) with ESMTP; Sun, 6 Nov 2022 22:36:33 +0300 (MSK) From: Arseniy Krasnov <AVKrasnov@sberdevices.ru> To: Stefano Garzarella <sgarzare@redhat.com>, Stefan Hajnoczi <stefanha@redhat.com>, "Michael S. Tsirkin" <mst@redhat.com>, Jason Wang <jasowang@redhat.com>, "David S. Miller" <davem@davemloft.net>, "edumazet@google.com" <edumazet@google.com>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>, Krasnov Arseniy <oxffffaa@gmail.com> CC: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>, "kvm@vger.kernel.org" <kvm@vger.kernel.org>, "virtualization@lists.linux-foundation.org" <virtualization@lists.linux-foundation.org>, "netdev@vger.kernel.org" <netdev@vger.kernel.org>, kernel <kernel@sberdevices.ru> Subject: [RFC PATCH v3 01/11] virtio/vsock: rework packet allocation logic Thread-Topic: [RFC PATCH v3 01/11] virtio/vsock: rework packet allocation logic Thread-Index: AQHY8hcBEbxDmf4aG021vuOLUMusCQ== Date: Sun, 6 Nov 2022 19:36:02 +0000 Message-ID: <f896b8fd-50d2-2512-3966-3775245e9b96@sberdevices.ru> In-Reply-To: <f60d7e94-795d-06fd-0321-6972533700c5@sberdevices.ru> Accept-Language: en-US, ru-RU Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [172.16.1.12] Content-Type: text/plain; charset="utf-8" Content-ID: <D9B2370EC071AE4DB1F3A0C4C342CA7C@sberdevices.ru> Content-Transfer-Encoding: base64 MIME-Version: 1.0 X-KSMG-Rule-ID: 4 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiPhishing: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 1.1.2.30, bases: 2022/11/06 12:52:00 #20573807 X-KSMG-AntiVirus-Status: Clean, skipped X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748776738480235184?= X-GMAIL-MSGID: =?utf-8?q?1748776738480235184?= |
Series |
virtio/vsock: experimental zerocopy receive
|
|
Commit Message
Arseniy Krasnov
Nov. 6, 2022, 7:36 p.m. UTC
To support zerocopy receive, packet's buffer allocation is changed: for
buffers which could be mapped to user's vma we can't use 'kmalloc()'(as
kernel restricts to map slab pages to user's vma) and raw buddy
allocator now called. But, for tx packets(such packets won't be mapped
to user), previous 'kmalloc()' way is used, but with special flag in
packet's structure which allows to distinguish between 'kmalloc()' and
raw pages buffers.
Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru>
---
drivers/vhost/vsock.c | 1 +
include/linux/virtio_vsock.h | 1 +
net/vmw_vsock/virtio_transport.c | 8 ++++++--
net/vmw_vsock/virtio_transport_common.c | 10 +++++++++-
4 files changed, 17 insertions(+), 3 deletions(-)
--
2.35.0
Comments
Le 06/11/2022 à 20:36, Arseniy Krasnov a écrit : > To support zerocopy receive, packet's buffer allocation is changed: for > buffers which could be mapped to user's vma we can't use 'kmalloc()'(as > kernel restricts to map slab pages to user's vma) and raw buddy > allocator now called. But, for tx packets(such packets won't be mapped > to user), previous 'kmalloc()' way is used, but with special flag in > packet's structure which allows to distinguish between 'kmalloc()' and > raw pages buffers. > > Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru> > --- > drivers/vhost/vsock.c | 1 + > include/linux/virtio_vsock.h | 1 + > net/vmw_vsock/virtio_transport.c | 8 ++++++-- > net/vmw_vsock/virtio_transport_common.c | 10 +++++++++- > 4 files changed, 17 insertions(+), 3 deletions(-) > > diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c > index 5703775af129..65475d128a1d 100644 > --- a/drivers/vhost/vsock.c > +++ b/drivers/vhost/vsock.c > @@ -399,6 +399,7 @@ vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq, > return NULL; > } > > + pkt->slab_buf = true; > pkt->buf_len = pkt->len; > > nbytes = copy_from_iter(pkt->buf, pkt->len, &iov_iter); > diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h > index 35d7eedb5e8e..d02cb7aa922f 100644 > --- a/include/linux/virtio_vsock.h > +++ b/include/linux/virtio_vsock.h > @@ -50,6 +50,7 @@ struct virtio_vsock_pkt { > u32 off; > bool reply; > bool tap_delivered; > + bool slab_buf; > }; > > struct virtio_vsock_pkt_info { > diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c > index ad64f403536a..19909c1e9ba3 100644 > --- a/net/vmw_vsock/virtio_transport.c > +++ b/net/vmw_vsock/virtio_transport.c > @@ -255,16 +255,20 @@ static void virtio_vsock_rx_fill(struct virtio_vsock *vsock) > vq = vsock->vqs[VSOCK_VQ_RX]; > > do { > + struct page *buf_page; > + > pkt = kzalloc(sizeof(*pkt), GFP_KERNEL); > if (!pkt) > break; > > - pkt->buf = kmalloc(buf_len, GFP_KERNEL); > - if (!pkt->buf) { > + buf_page = alloc_page(GFP_KERNEL); > + > + if (!buf_page) { > virtio_transport_free_pkt(pkt); > break; > } > > + pkt->buf = page_to_virt(buf_page); > pkt->buf_len = buf_len; > pkt->len = buf_len; > > diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c > index a9980e9b9304..37e8dbfe2f5d 100644 > --- a/net/vmw_vsock/virtio_transport_common.c > +++ b/net/vmw_vsock/virtio_transport_common.c > @@ -69,6 +69,7 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info, > if (!pkt->buf) > goto out_pkt; > > + pkt->slab_buf = true; > pkt->buf_len = len; > > err = memcpy_from_msg(pkt->buf, info->msg, len); > @@ -1339,7 +1340,14 @@ EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt); > > void virtio_transport_free_pkt(struct virtio_vsock_pkt *pkt) > { > - kvfree(pkt->buf); > + if (pkt->buf_len) { > + if (pkt->slab_buf) > + kvfree(pkt->buf); Hi, kfree()? (according to virtio_transport_alloc_pkt() in -next) or something else need to be changed. > + else > + free_pages((unsigned long)pkt->buf, > + get_order(pkt->buf_len)); In virtio_vsock_rx_fill(), only alloc_page() is used. Should this be alloc_pages(.., get_order(buf_len)) or free_page() (without an 's') here? Just my 2c, CJ > + } > + > kfree(pkt); > } > EXPORT_SYMBOL_GPL(virtio_transport_free_pkt);
On 06.11.2022 22:50, Christophe JAILLET wrote: > Le 06/11/2022 à 20:36, Arseniy Krasnov a écrit : >> To support zerocopy receive, packet's buffer allocation is changed: for >> buffers which could be mapped to user's vma we can't use 'kmalloc()'(as >> kernel restricts to map slab pages to user's vma) and raw buddy >> allocator now called. But, for tx packets(such packets won't be mapped >> to user), previous 'kmalloc()' way is used, but with special flag in >> packet's structure which allows to distinguish between 'kmalloc()' and >> raw pages buffers. >> >> Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru> >> --- >> drivers/vhost/vsock.c | 1 + >> include/linux/virtio_vsock.h | 1 + >> net/vmw_vsock/virtio_transport.c | 8 ++++++-- >> net/vmw_vsock/virtio_transport_common.c | 10 +++++++++- >> 4 files changed, 17 insertions(+), 3 deletions(-) >> >> diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c >> index 5703775af129..65475d128a1d 100644 >> --- a/drivers/vhost/vsock.c >> +++ b/drivers/vhost/vsock.c >> @@ -399,6 +399,7 @@ vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq, >> return NULL; >> } >> + pkt->slab_buf = true; >> pkt->buf_len = pkt->len; >> nbytes = copy_from_iter(pkt->buf, pkt->len, &iov_iter); >> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h >> index 35d7eedb5e8e..d02cb7aa922f 100644 >> --- a/include/linux/virtio_vsock.h >> +++ b/include/linux/virtio_vsock.h >> @@ -50,6 +50,7 @@ struct virtio_vsock_pkt { >> u32 off; >> bool reply; >> bool tap_delivered; >> + bool slab_buf; >> }; >> struct virtio_vsock_pkt_info { >> diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c >> index ad64f403536a..19909c1e9ba3 100644 >> --- a/net/vmw_vsock/virtio_transport.c >> +++ b/net/vmw_vsock/virtio_transport.c >> @@ -255,16 +255,20 @@ static void virtio_vsock_rx_fill(struct virtio_vsock *vsock) >> vq = vsock->vqs[VSOCK_VQ_RX]; >> do { >> + struct page *buf_page; >> + >> pkt = kzalloc(sizeof(*pkt), GFP_KERNEL); >> if (!pkt) >> break; >> - pkt->buf = kmalloc(buf_len, GFP_KERNEL); >> - if (!pkt->buf) { >> + buf_page = alloc_page(GFP_KERNEL); >> + >> + if (!buf_page) { >> virtio_transport_free_pkt(pkt); >> break; >> } >> + pkt->buf = page_to_virt(buf_page); >> pkt->buf_len = buf_len; >> pkt->len = buf_len; >> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c >> index a9980e9b9304..37e8dbfe2f5d 100644 >> --- a/net/vmw_vsock/virtio_transport_common.c >> +++ b/net/vmw_vsock/virtio_transport_common.c >> @@ -69,6 +69,7 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info, >> if (!pkt->buf) >> goto out_pkt; >> + pkt->slab_buf = true; >> pkt->buf_len = len; >> err = memcpy_from_msg(pkt->buf, info->msg, len); >> @@ -1339,7 +1340,14 @@ EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt); >> void virtio_transport_free_pkt(struct virtio_vsock_pkt *pkt) >> { >> - kvfree(pkt->buf); >> + if (pkt->buf_len) { >> + if (pkt->slab_buf) >> + kvfree(pkt->buf); > > Hi, > > kfree()? (according to virtio_transport_alloc_pkt() in -next) or something else need to be changed. > Hello Cristophe, I think, 'kvfree()' is still needed here, because buffer for packet could be allocated by 'kvmalloc()' in drivers/vhost/vsock.c. Correct me if i'm wrong. >> + else >> + free_pages((unsigned long)pkt->buf, >> + get_order(pkt->buf_len)); > > In virtio_vsock_rx_fill(), only alloc_page() is used. > > Should this be alloc_pages(.., get_order(buf_len)) or free_page() (without an 's') here? This function frees packets which were allocated in vhost path also. In vhost, for zerocopy packets alloc_pageS() is used. Thank You, Arseniy > > Just my 2c, > > CJ > >> + } >> + >> kfree(pkt); >> } >> EXPORT_SYMBOL_GPL(virtio_transport_free_pkt); >
Le 07/11/2022 à 06:23, Arseniy Krasnov a écrit : > On 06.11.2022 22:50, Christophe JAILLET wrote: >> Le 06/11/2022 à 20:36, Arseniy Krasnov a écrit : >>> To support zerocopy receive, packet's buffer allocation is changed: for >>> buffers which could be mapped to user's vma we can't use 'kmalloc()'(as >>> kernel restricts to map slab pages to user's vma) and raw buddy >>> allocator now called. But, for tx packets(such packets won't be mapped >>> to user), previous 'kmalloc()' way is used, but with special flag in >>> packet's structure which allows to distinguish between 'kmalloc()' and >>> raw pages buffers. >>> >>> Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru> >>> --- >>> drivers/vhost/vsock.c | 1 + >>> include/linux/virtio_vsock.h | 1 + >>> net/vmw_vsock/virtio_transport.c | 8 ++++++-- >>> net/vmw_vsock/virtio_transport_common.c | 10 +++++++++- >>> 4 files changed, 17 insertions(+), 3 deletions(-) >>> >>> diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c >>> index 5703775af129..65475d128a1d 100644 >>> --- a/drivers/vhost/vsock.c >>> +++ b/drivers/vhost/vsock.c >>> @@ -399,6 +399,7 @@ vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq, >>> return NULL; >>> } >>> + pkt->slab_buf = true; >>> pkt->buf_len = pkt->len; >>> nbytes = copy_from_iter(pkt->buf, pkt->len, &iov_iter); >>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h >>> index 35d7eedb5e8e..d02cb7aa922f 100644 >>> --- a/include/linux/virtio_vsock.h >>> +++ b/include/linux/virtio_vsock.h >>> @@ -50,6 +50,7 @@ struct virtio_vsock_pkt { >>> u32 off; >>> bool reply; >>> bool tap_delivered; >>> + bool slab_buf; >>> }; >>> struct virtio_vsock_pkt_info { >>> diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c >>> index ad64f403536a..19909c1e9ba3 100644 >>> --- a/net/vmw_vsock/virtio_transport.c >>> +++ b/net/vmw_vsock/virtio_transport.c >>> @@ -255,16 +255,20 @@ static void virtio_vsock_rx_fill(struct virtio_vsock *vsock) >>> vq = vsock->vqs[VSOCK_VQ_RX]; >>> do { >>> + struct page *buf_page; >>> + >>> pkt = kzalloc(sizeof(*pkt), GFP_KERNEL); >>> if (!pkt) >>> break; >>> - pkt->buf = kmalloc(buf_len, GFP_KERNEL); >>> - if (!pkt->buf) { >>> + buf_page = alloc_page(GFP_KERNEL); >>> + >>> + if (!buf_page) { >>> virtio_transport_free_pkt(pkt); >>> break; >>> } >>> + pkt->buf = page_to_virt(buf_page); >>> pkt->buf_len = buf_len; >>> pkt->len = buf_len; >>> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c >>> index a9980e9b9304..37e8dbfe2f5d 100644 >>> --- a/net/vmw_vsock/virtio_transport_common.c >>> +++ b/net/vmw_vsock/virtio_transport_common.c >>> @@ -69,6 +69,7 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info, >>> if (!pkt->buf) >>> goto out_pkt; >>> + pkt->slab_buf = true; >>> pkt->buf_len = len; >>> err = memcpy_from_msg(pkt->buf, info->msg, len); >>> @@ -1339,7 +1340,14 @@ EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt); >>> void virtio_transport_free_pkt(struct virtio_vsock_pkt *pkt) >>> { >>> - kvfree(pkt->buf); >>> + if (pkt->buf_len) { >>> + if (pkt->slab_buf) >>> + kvfree(pkt->buf); >> >> Hi, >> >> kfree()? (according to virtio_transport_alloc_pkt() in -next) or something else need to be changed. >> > Hello Cristophe, > > I think, 'kvfree()' is still needed here, because buffer for packet could be allocated by 'kvmalloc()' > in drivers/vhost/vsock.c. Correct me if i'm wrong. Agreed. > >>> + else >>> + free_pages((unsigned long)pkt->buf, >>> + get_order(pkt->buf_len)); >> >> In virtio_vsock_rx_fill(), only alloc_page() is used. >> >> Should this be alloc_pages(.., get_order(buf_len)) or free_page() (without an 's') here? > This function frees packets which were allocated in vhost path also. In vhost, for zerocopy > packets alloc_pageS() is used. Ok. Seen in patch 5/11. But wouldn't it be cleaner and future-proof to also have alloc_pageS() in virtio_vsock_rx_fill(), even if get_order(buf->len) is kwown to be 0? What, if for some reason a PAGE_SIZE was < 4kb for a given arch, or VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE increased? CJ > > Thank You, Arseniy >> >> Just my 2c, >> >> CJ >> >>> + } >>> + >>> kfree(pkt); >>> } >>> EXPORT_SYMBOL_GPL(virtio_transport_free_pkt); >> >
On 08.11.2022 00:24, Christophe JAILLET wrote: > Le 07/11/2022 à 06:23, Arseniy Krasnov a écrit : >> On 06.11.2022 22:50, Christophe JAILLET wrote: >>> Le 06/11/2022 à 20:36, Arseniy Krasnov a écrit : >>>> To support zerocopy receive, packet's buffer allocation is changed: for >>>> buffers which could be mapped to user's vma we can't use 'kmalloc()'(as >>>> kernel restricts to map slab pages to user's vma) and raw buddy >>>> allocator now called. But, for tx packets(such packets won't be mapped >>>> to user), previous 'kmalloc()' way is used, but with special flag in >>>> packet's structure which allows to distinguish between 'kmalloc()' and >>>> raw pages buffers. >>>> >>>> Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru> >>>> --- >>>> drivers/vhost/vsock.c | 1 + >>>> include/linux/virtio_vsock.h | 1 + >>>> net/vmw_vsock/virtio_transport.c | 8 ++++++-- >>>> net/vmw_vsock/virtio_transport_common.c | 10 +++++++++- >>>> 4 files changed, 17 insertions(+), 3 deletions(-) >>>> >>>> diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c >>>> index 5703775af129..65475d128a1d 100644 >>>> --- a/drivers/vhost/vsock.c >>>> +++ b/drivers/vhost/vsock.c >>>> @@ -399,6 +399,7 @@ vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq, >>>> return NULL; >>>> } >>>> + pkt->slab_buf = true; >>>> pkt->buf_len = pkt->len; >>>> nbytes = copy_from_iter(pkt->buf, pkt->len, &iov_iter); >>>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h >>>> index 35d7eedb5e8e..d02cb7aa922f 100644 >>>> --- a/include/linux/virtio_vsock.h >>>> +++ b/include/linux/virtio_vsock.h >>>> @@ -50,6 +50,7 @@ struct virtio_vsock_pkt { >>>> u32 off; >>>> bool reply; >>>> bool tap_delivered; >>>> + bool slab_buf; >>>> }; >>>> struct virtio_vsock_pkt_info { >>>> diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c >>>> index ad64f403536a..19909c1e9ba3 100644 >>>> --- a/net/vmw_vsock/virtio_transport.c >>>> +++ b/net/vmw_vsock/virtio_transport.c >>>> @@ -255,16 +255,20 @@ static void virtio_vsock_rx_fill(struct virtio_vsock *vsock) >>>> vq = vsock->vqs[VSOCK_VQ_RX]; >>>> do { >>>> + struct page *buf_page; >>>> + >>>> pkt = kzalloc(sizeof(*pkt), GFP_KERNEL); >>>> if (!pkt) >>>> break; >>>> - pkt->buf = kmalloc(buf_len, GFP_KERNEL); >>>> - if (!pkt->buf) { >>>> + buf_page = alloc_page(GFP_KERNEL); >>>> + >>>> + if (!buf_page) { >>>> virtio_transport_free_pkt(pkt); >>>> break; >>>> } >>>> + pkt->buf = page_to_virt(buf_page); >>>> pkt->buf_len = buf_len; >>>> pkt->len = buf_len; >>>> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c >>>> index a9980e9b9304..37e8dbfe2f5d 100644 >>>> --- a/net/vmw_vsock/virtio_transport_common.c >>>> +++ b/net/vmw_vsock/virtio_transport_common.c >>>> @@ -69,6 +69,7 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info, >>>> if (!pkt->buf) >>>> goto out_pkt; >>>> + pkt->slab_buf = true; >>>> pkt->buf_len = len; >>>> err = memcpy_from_msg(pkt->buf, info->msg, len); >>>> @@ -1339,7 +1340,14 @@ EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt); >>>> void virtio_transport_free_pkt(struct virtio_vsock_pkt *pkt) >>>> { >>>> - kvfree(pkt->buf); >>>> + if (pkt->buf_len) { >>>> + if (pkt->slab_buf) >>>> + kvfree(pkt->buf); >>> >>> Hi, >>> >>> kfree()? (according to virtio_transport_alloc_pkt() in -next) or something else need to be changed. >>> >> Hello Cristophe, >> >> I think, 'kvfree()' is still needed here, because buffer for packet could be allocated by 'kvmalloc()' >> in drivers/vhost/vsock.c. Correct me if i'm wrong. > > Agreed. > >> >>>> + else >>>> + free_pages((unsigned long)pkt->buf, >>>> + get_order(pkt->buf_len)); >>> >>> In virtio_vsock_rx_fill(), only alloc_page() is used. >>> >>> Should this be alloc_pages(.., get_order(buf_len)) or free_page() (without an 's') here? >> This function frees packets which were allocated in vhost path also. In vhost, for zerocopy >> packets alloc_pageS() is used. > > Ok. Seen in patch 5/11. > > But wouldn't it be cleaner and future-proof to also have alloc_pageS() in virtio_vsock_rx_fill(), even if get_order(buf->len) is kwown to be 0? > > What, if for some reason a PAGE_SIZE was < 4kb for a given arch, or VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE increased? Yes, i see. You're right. It will be correct to use alloc_pages(get_order(buf->len)), because in current version, real length of packet buffer(e.g. single page) is totally unrelated with VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE. I'll fix it in next version. Thank You > > CJ > >> >> Thank You, Arseniy >>> >>> Just my 2c, >>> >>> CJ >>> >>>> + } >>>> + >>>> kfree(pkt); >>>> } >>>> EXPORT_SYMBOL_GPL(virtio_transport_free_pkt); >>> >> >
On Sun, Nov 06, 2022 at 07:36:02PM +0000, Arseniy Krasnov wrote: > To support zerocopy receive, packet's buffer allocation is changed: for > buffers which could be mapped to user's vma we can't use 'kmalloc()'(as > kernel restricts to map slab pages to user's vma) and raw buddy > allocator now called. But, for tx packets(such packets won't be mapped > to user), previous 'kmalloc()' way is used, but with special flag in > packet's structure which allows to distinguish between 'kmalloc()' and > raw pages buffers. > > Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru> Hey Arseniy, great work here! > --- > drivers/vhost/vsock.c | 1 + > include/linux/virtio_vsock.h | 1 + > net/vmw_vsock/virtio_transport.c | 8 ++++++-- > net/vmw_vsock/virtio_transport_common.c | 10 +++++++++- > 4 files changed, 17 insertions(+), 3 deletions(-) > > diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c > index 5703775af129..65475d128a1d 100644 > --- a/drivers/vhost/vsock.c > +++ b/drivers/vhost/vsock.c > @@ -399,6 +399,7 @@ vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq, > return NULL; > } > > + pkt->slab_buf = true; > pkt->buf_len = pkt->len; > > nbytes = copy_from_iter(pkt->buf, pkt->len, &iov_iter); > diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h > index 35d7eedb5e8e..d02cb7aa922f 100644 > --- a/include/linux/virtio_vsock.h > +++ b/include/linux/virtio_vsock.h > @@ -50,6 +50,7 @@ struct virtio_vsock_pkt { > u32 off; > bool reply; > bool tap_delivered; > + bool slab_buf; > }; WRT the sk_buff series, I bet this bool will not be needed after the rebase. > > struct virtio_vsock_pkt_info { > diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c > index ad64f403536a..19909c1e9ba3 100644 > --- a/net/vmw_vsock/virtio_transport.c > +++ b/net/vmw_vsock/virtio_transport.c > @@ -255,16 +255,20 @@ static void virtio_vsock_rx_fill(struct virtio_vsock *vsock) > vq = vsock->vqs[VSOCK_VQ_RX]; > > do { > + struct page *buf_page; > + > pkt = kzalloc(sizeof(*pkt), GFP_KERNEL); > if (!pkt) > break; > > - pkt->buf = kmalloc(buf_len, GFP_KERNEL); > - if (!pkt->buf) { > + buf_page = alloc_page(GFP_KERNEL); > + > + if (!buf_page) { I think it should not be too hard to use build_skb() around the page here after rebasing onto the sk_buff series. > virtio_transport_free_pkt(pkt); > break; > } > > + pkt->buf = page_to_virt(buf_page); > pkt->buf_len = buf_len; > pkt->len = buf_len; > > diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c > index a9980e9b9304..37e8dbfe2f5d 100644 > --- a/net/vmw_vsock/virtio_transport_common.c > +++ b/net/vmw_vsock/virtio_transport_common.c > @@ -69,6 +69,7 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info, > if (!pkt->buf) > goto out_pkt; > > + pkt->slab_buf = true; > pkt->buf_len = len; > > err = memcpy_from_msg(pkt->buf, info->msg, len); > @@ -1339,7 +1340,14 @@ EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt); > > void virtio_transport_free_pkt(struct virtio_vsock_pkt *pkt) > { > - kvfree(pkt->buf); > + if (pkt->buf_len) { > + if (pkt->slab_buf) > + kvfree(pkt->buf); > + else > + free_pages((unsigned long)pkt->buf, > + get_order(pkt->buf_len)); > + } > + > kfree(pkt); > } > EXPORT_SYMBOL_GPL(virtio_transport_free_pkt); > -- > 2.35.0
diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index 5703775af129..65475d128a1d 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -399,6 +399,7 @@ vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq, return NULL; } + pkt->slab_buf = true; pkt->buf_len = pkt->len; nbytes = copy_from_iter(pkt->buf, pkt->len, &iov_iter); diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index 35d7eedb5e8e..d02cb7aa922f 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -50,6 +50,7 @@ struct virtio_vsock_pkt { u32 off; bool reply; bool tap_delivered; + bool slab_buf; }; struct virtio_vsock_pkt_info { diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c index ad64f403536a..19909c1e9ba3 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -255,16 +255,20 @@ static void virtio_vsock_rx_fill(struct virtio_vsock *vsock) vq = vsock->vqs[VSOCK_VQ_RX]; do { + struct page *buf_page; + pkt = kzalloc(sizeof(*pkt), GFP_KERNEL); if (!pkt) break; - pkt->buf = kmalloc(buf_len, GFP_KERNEL); - if (!pkt->buf) { + buf_page = alloc_page(GFP_KERNEL); + + if (!buf_page) { virtio_transport_free_pkt(pkt); break; } + pkt->buf = page_to_virt(buf_page); pkt->buf_len = buf_len; pkt->len = buf_len; diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c index a9980e9b9304..37e8dbfe2f5d 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -69,6 +69,7 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info, if (!pkt->buf) goto out_pkt; + pkt->slab_buf = true; pkt->buf_len = len; err = memcpy_from_msg(pkt->buf, info->msg, len); @@ -1339,7 +1340,14 @@ EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt); void virtio_transport_free_pkt(struct virtio_vsock_pkt *pkt) { - kvfree(pkt->buf); + if (pkt->buf_len) { + if (pkt->slab_buf) + kvfree(pkt->buf); + else + free_pages((unsigned long)pkt->buf, + get_order(pkt->buf_len)); + } + kfree(pkt); } EXPORT_SYMBOL_GPL(virtio_transport_free_pkt);