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>,
"Jakub Kicinski" <kuba@kernel.org>,
Paolo Abeni <pabeni@redhat.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>,
Krasnov Arseniy <oxffffaa@gmail.com>
Subject: [RFC PATCH v2 2/8] vhost/vsock: rework packet allocation logic
Date: Fri, 3 Jun 2022 05:33:04 +0000 [thread overview]
Message-ID: <72ae7f76-ffee-3e64-d445-7a0f4261d891@sberdevices.ru> (raw)
In-Reply-To: <e37fdf9b-be80-35e1-ae7b-c9dfeae3e3db@sberdevices.ru>
For packets received from virtio RX queue, use buddy
allocator instead of 'kmalloc()' to be able to insert
such pages to user provided vma. Single call to
'copy_from_iter()' replaced with per-page loop.
Signed-off-by: Arseniy Krasnov <AVKrasnov@sberdevices.ru>
---
drivers/vhost/vsock.c | 81 ++++++++++++++++++++++++++++++++++++-------
1 file changed, 69 insertions(+), 12 deletions(-)
diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index e6c9d41db1de..0dc2229f18f7 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -58,6 +58,7 @@ struct vhost_vsock {
u32 guest_cid;
bool seqpacket_allow;
+ bool zerocopy_rx_on;
};
static u32 vhost_transport_get_local_cid(void)
@@ -357,6 +358,7 @@ vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
unsigned int out, unsigned int in)
{
struct virtio_vsock_pkt *pkt;
+ struct vhost_vsock *vsock;
struct iov_iter iov_iter;
size_t nbytes;
size_t len;
@@ -393,20 +395,75 @@ vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
return NULL;
}
- pkt->buf = kmalloc(pkt->len, GFP_KERNEL);
- if (!pkt->buf) {
- kfree(pkt);
- return NULL;
- }
-
pkt->buf_len = pkt->len;
+ vsock = container_of(vq->dev, struct vhost_vsock, dev);
- nbytes = copy_from_iter(pkt->buf, pkt->len, &iov_iter);
- if (nbytes != pkt->len) {
- vq_err(vq, "Expected %u byte payload, got %zu bytes\n",
- pkt->len, nbytes);
- virtio_transport_free_pkt(pkt);
- return NULL;
+ if (!vsock->zerocopy_rx_on) {
+ pkt->buf = kmalloc(pkt->len, GFP_KERNEL);
+
+ if (!pkt->buf) {
+ kfree(pkt);
+ return NULL;
+ }
+
+ pkt->slab_buf = true;
+ nbytes = copy_from_iter(pkt->buf, pkt->len, &iov_iter);
+ if (nbytes != pkt->len) {
+ vq_err(vq, "Expected %u byte payload, got %zu bytes\n",
+ pkt->len, nbytes);
+ virtio_transport_free_pkt(pkt);
+ return NULL;
+ }
+ } else {
+ struct page *buf_page;
+ ssize_t pkt_len;
+ int page_idx;
+
+ /* This creates memory overrun, as we allocate
+ * at least one page for each packet.
+ */
+ buf_page = alloc_pages(GFP_KERNEL, get_order(pkt->len));
+
+ if (buf_page == NULL) {
+ kfree(pkt);
+ return NULL;
+ }
+
+ pkt->buf = page_to_virt(buf_page);
+
+ page_idx = 0;
+ pkt_len = pkt->len;
+
+ /* As allocated pages are not mapped, process
+ * pages one by one.
+ */
+ while (pkt_len > 0) {
+ void *mapped;
+ size_t to_copy;
+
+ mapped = kmap(buf_page + page_idx);
+
+ if (mapped == NULL) {
+ virtio_transport_free_pkt(pkt);
+ return NULL;
+ }
+
+ to_copy = min(pkt_len, ((ssize_t)PAGE_SIZE));
+
+ nbytes = copy_from_iter(mapped, to_copy, &iov_iter);
+ if (nbytes != to_copy) {
+ vq_err(vq, "Expected %zu byte payload, got %zu bytes\n",
+ to_copy, nbytes);
+ kunmap(mapped);
+ virtio_transport_free_pkt(pkt);
+ return NULL;
+ }
+
+ kunmap(mapped);
+
+ pkt_len -= to_copy;
+ page_idx++;
+ }
}
return pkt;
--
2.25.1
next prev parent reply other threads:[~2022-06-03 5:33 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-03 5:27 [RFC PATCH v2 0/8] virtio/vsock: experimental zerocopy receive Arseniy Krasnov
2022-06-03 5:31 ` [RFC PATCH v2 1/8] virtio/vsock: rework packet allocation logic Arseniy Krasnov
2022-06-09 8:37 ` Stefano Garzarella
2022-06-03 5:33 ` Arseniy Krasnov [this message]
2022-06-09 8:38 ` [RFC PATCH v2 2/8] vhost/vsock: " Stefano Garzarella
2022-06-09 12:09 ` Arseniy Krasnov
2022-06-03 5:35 ` [RFC PATCH v2 3/8] af_vsock: add zerocopy receive logic Arseniy Krasnov
2022-06-09 8:39 ` Stefano Garzarella
2022-06-09 12:20 ` Arseniy Krasnov
2022-06-13 8:50 ` Stefano Garzarella
2022-06-03 5:37 ` [RFC PATCH v2 4/8] virtio/vsock: add transport zerocopy callback Arseniy Krasnov
2022-06-09 8:41 ` Stefano Garzarella
2022-06-09 12:23 ` Arseniy Krasnov
2022-06-03 5:39 ` [RFC PATCH v2 5/8] vhost/vsock: enable " Arseniy Krasnov
2022-06-03 5:41 ` [RFC PATCH v2 6/8] virtio/vsock: " Arseniy Krasnov
2022-06-03 5:43 ` [RFC PATCH v2 7/8] test/vsock: add receive zerocopy tests Arseniy Krasnov
2022-06-03 5:44 ` [RFC PATCH v2 8/8] test/vsock: vsock rx zerocopy utility Arseniy Krasnov
2022-06-08 15:59 ` [RFC PATCH v2 0/8] virtio/vsock: experimental zerocopy receive Zhao Liu
2022-06-09 8:54 ` Stefano Garzarella
2022-06-09 12:33 ` Arseniy Krasnov
2022-06-13 8:54 ` Stefano Garzarella
2022-06-14 4:23 ` Arseniy Krasnov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=72ae7f76-ffee-3e64-d445-7a0f4261d891@sberdevices.ru \
--to=avkrasnov@sberdevices.ru \
--cc=davem@davemloft.net \
--cc=jasowang@redhat.com \
--cc=kernel@sberdevices.ru \
--cc=kuba@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mst@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=oxffffaa@gmail.com \
--cc=pabeni@redhat.com \
--cc=sgarzare@redhat.com \
--cc=stefanha@redhat.com \
--cc=virtualization@lists.linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox