2023-03-21 15:06:52

by Arseniy Krasnov

[permalink] [raw]
Subject: [RFC PATCH v4] virtio/vsock: allocate multiple skbuffs on tx

This adds small optimization for tx path: instead of allocating single
skbuff on every call to transport, allocate multiple skbuff's until
credit space allows, thus trying to send as much as possible data without
return to af_vsock.c.

Signed-off-by: Arseniy Krasnov <[email protected]>
---
Link to v1:
https://lore.kernel.org/netdev/[email protected]/
Link to v2:
https://lore.kernel.org/netdev/[email protected]/
Link to v3:
https://lore.kernel.org/netdev/[email protected]/

Changelog:
v1 -> v2:
- If sent something, return number of bytes sent (even in
case of error). Return error only if failed to sent first
skbuff.

v2 -> v3:
- Handle case when transport callback returns unexpected value which
is not equal to 'skb->len'. Break loop.
- Don't check for zero value of 'rest_len' before calling
'virtio_transport_put_credit()'. Decided to add this check directly
to 'virtio_transport_put_credit()' in separate patch.

v3 -> v4:
- Use WARN_ONCE() to handle case when transport callback returns
unexpected value.
- Remove useless 'ret = -EFAULT;' assignment for case above.

net/vmw_vsock/virtio_transport_common.c | 59 +++++++++++++++++++------
1 file changed, 45 insertions(+), 14 deletions(-)

diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index 6564192e7f20..a300f25749ea 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -196,7 +196,8 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
const struct virtio_transport *t_ops;
struct virtio_vsock_sock *vvs;
u32 pkt_len = info->pkt_len;
- struct sk_buff *skb;
+ u32 rest_len;
+ int ret;

info->type = virtio_transport_get_type(sk_vsock(vsk));

@@ -216,10 +217,6 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,

vvs = vsk->trans;

- /* we can send less than pkt_len bytes */
- if (pkt_len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE)
- pkt_len = VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
-
/* virtio_transport_get_credit might return less than pkt_len credit */
pkt_len = virtio_transport_get_credit(vvs, pkt_len);

@@ -227,17 +224,51 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
if (pkt_len == 0 && info->op == VIRTIO_VSOCK_OP_RW)
return pkt_len;

- skb = virtio_transport_alloc_skb(info, pkt_len,
- src_cid, src_port,
- dst_cid, dst_port);
- if (!skb) {
- virtio_transport_put_credit(vvs, pkt_len);
- return -ENOMEM;
- }
+ ret = 0;
+ rest_len = pkt_len;
+
+ do {
+ struct sk_buff *skb;
+ size_t skb_len;
+
+ skb_len = min_t(u32, VIRTIO_VSOCK_MAX_PKT_BUF_SIZE, rest_len);
+
+ skb = virtio_transport_alloc_skb(info, skb_len,
+ src_cid, src_port,
+ dst_cid, dst_port);
+ if (!skb) {
+ ret = -ENOMEM;
+ break;
+ }

- virtio_transport_inc_tx_pkt(vvs, skb);
+ virtio_transport_inc_tx_pkt(vvs, skb);

- return t_ops->send_pkt(skb);
+ ret = t_ops->send_pkt(skb);
+
+ if (ret < 0)
+ break;
+
+ /* Both virtio and vhost 'send_pkt()' returns 'skb_len',
+ * but for reliability use 'ret' instead of 'skb_len'.
+ * Also if partial send happens (e.g. 'ret' != 'skb_len')
+ * somehow, we break this loop, but account such returned
+ * value in 'virtio_transport_put_credit()'.
+ */
+ rest_len -= ret;
+
+ if (WARN_ONCE(ret != skb_len,
+ "'send_pkt()' returns %i, but %zu expected\n",
+ ret, skb_len))
+ break;
+ } while (rest_len);
+
+ virtio_transport_put_credit(vvs, rest_len);
+
+ /* Return number of bytes, if any data has been sent. */
+ if (rest_len != pkt_len)
+ ret = pkt_len - rest_len;
+
+ return ret;
}

static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs,
--
2.25.1


2023-03-22 14:44:31

by Stefano Garzarella

[permalink] [raw]
Subject: Re: [RFC PATCH v4] virtio/vsock: allocate multiple skbuffs on tx

On Tue, Mar 21, 2023 at 06:03:14PM +0300, Arseniy Krasnov wrote:
>This adds small optimization for tx path: instead of allocating single
>skbuff on every call to transport, allocate multiple skbuff's until
>credit space allows, thus trying to send as much as possible data without
>return to af_vsock.c.
>
>Signed-off-by: Arseniy Krasnov <[email protected]>
>---
> Link to v1:
> https://lore.kernel.org/netdev/[email protected]/
> Link to v2:
> https://lore.kernel.org/netdev/[email protected]/
> Link to v3:
> https://lore.kernel.org/netdev/[email protected]/
>
> Changelog:
> v1 -> v2:
> - If sent something, return number of bytes sent (even in
> case of error). Return error only if failed to sent first
> skbuff.
>
> v2 -> v3:
> - Handle case when transport callback returns unexpected value which
> is not equal to 'skb->len'. Break loop.
> - Don't check for zero value of 'rest_len' before calling
> 'virtio_transport_put_credit()'. Decided to add this check directly
> to 'virtio_transport_put_credit()' in separate patch.
>
> v3 -> v4:
> - Use WARN_ONCE() to handle case when transport callback returns
> unexpected value.
> - Remove useless 'ret = -EFAULT;' assignment for case above.
>
> net/vmw_vsock/virtio_transport_common.c | 59 +++++++++++++++++++------
> 1 file changed, 45 insertions(+), 14 deletions(-)
>
>diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>index 6564192e7f20..a300f25749ea 100644
>--- a/net/vmw_vsock/virtio_transport_common.c
>+++ b/net/vmw_vsock/virtio_transport_common.c
>@@ -196,7 +196,8 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
> const struct virtio_transport *t_ops;
> struct virtio_vsock_sock *vvs;
> u32 pkt_len = info->pkt_len;
>- struct sk_buff *skb;
>+ u32 rest_len;
>+ int ret;
>
> info->type = virtio_transport_get_type(sk_vsock(vsk));
>
>@@ -216,10 +217,6 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
>
> vvs = vsk->trans;
>
>- /* we can send less than pkt_len bytes */
>- if (pkt_len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE)
>- pkt_len = VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
>-
> /* virtio_transport_get_credit might return less than pkt_len credit */
> pkt_len = virtio_transport_get_credit(vvs, pkt_len);
>
>@@ -227,17 +224,51 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
> if (pkt_len == 0 && info->op == VIRTIO_VSOCK_OP_RW)
> return pkt_len;
>
>- skb = virtio_transport_alloc_skb(info, pkt_len,
>- src_cid, src_port,
>- dst_cid, dst_port);
>- if (!skb) {
>- virtio_transport_put_credit(vvs, pkt_len);
>- return -ENOMEM;
>- }
>+ ret = 0;

nit: this initialization seems superfluous since `ret` is
overwritten later ...

>+ rest_len = pkt_len;
>+
>+ do {
>+ struct sk_buff *skb;
>+ size_t skb_len;
>+
>+ skb_len = min_t(u32, VIRTIO_VSOCK_MAX_PKT_BUF_SIZE, rest_len);
>+
>+ skb = virtio_transport_alloc_skb(info, skb_len,
>+ src_cid, src_port,
>+ dst_cid, dst_port);
>+ if (!skb) {
>+ ret = -ENOMEM;
>+ break;
>+ }
>
>- virtio_transport_inc_tx_pkt(vvs, skb);
>+ virtio_transport_inc_tx_pkt(vvs, skb);
>
>- return t_ops->send_pkt(skb);
>+ ret = t_ops->send_pkt(skb);

... here.

>+

nit: we can remove this extra line

>+ if (ret < 0)
>+ break;
>+
>+ /* Both virtio and vhost 'send_pkt()' returns 'skb_len',
>+ * but for reliability use 'ret' instead of 'skb_len'.
>+ * Also if partial send happens (e.g. 'ret' != 'skb_len')
>+ * somehow, we break this loop, but account such returned
>+ * value in 'virtio_transport_put_credit()'.
>+ */
>+ rest_len -= ret;
>+
>+ if (WARN_ONCE(ret != skb_len,
>+ "'send_pkt()' returns %i, but %zu expected\n",
>+ ret, skb_len))
>+ break;
>+ } while (rest_len);
>+
>+ virtio_transport_put_credit(vvs, rest_len);
>+
>+ /* Return number of bytes, if any data has been sent. */
>+ if (rest_len != pkt_len)
>+ ret = pkt_len - rest_len;
>+
>+ return ret;
> }
>
> static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs,
>-- 2.25.1
>

The patch LGTM:

Reviewed-by: Stefano Garzarella <[email protected]>

Anyway, feel free to include in the same series or as separate patch
also the changes to avoid useless lock in virtio_transport_put_credit()
and virtio_transport_get_credit().

I would include it in this series, because before these changes, we
used to call virtio_transport_put_credit() only in the error path,
while now we always call it, even when rest_len is 0.

Thanks,
Stefano

2023-03-22 18:44:20

by Arseniy Krasnov

[permalink] [raw]
Subject: Re: [RFC PATCH v4] virtio/vsock: allocate multiple skbuffs on tx



On 22.03.2023 17:41, Stefano Garzarella wrote:
> On Tue, Mar 21, 2023 at 06:03:14PM +0300, Arseniy Krasnov wrote:
>> This adds small optimization for tx path: instead of allocating single
>> skbuff on every call to transport, allocate multiple skbuff's until
>> credit space allows, thus trying to send as much as possible data without
>> return to af_vsock.c.
>>
>> Signed-off-by: Arseniy Krasnov <[email protected]>
>> ---
>> Link to v1:
>> https://lore.kernel.org/netdev/[email protected]/
>> Link to v2:
>> https://lore.kernel.org/netdev/[email protected]/
>> Link to v3:
>> https://lore.kernel.org/netdev/[email protected]/
>>
>> Changelog:
>> v1 -> v2:
>> - If sent something, return number of bytes sent (even in
>>   case of error). Return error only if failed to sent first
>>   skbuff.
>>
>> v2 -> v3:
>> - Handle case when transport callback returns unexpected value which
>>   is not equal to 'skb->len'. Break loop.
>> - Don't check for zero value of 'rest_len' before calling
>>   'virtio_transport_put_credit()'. Decided to add this check directly
>>   to 'virtio_transport_put_credit()' in separate patch.
>>
>> v3 -> v4:
>> - Use WARN_ONCE() to handle case when transport callback returns
>>   unexpected value.
>> - Remove useless 'ret = -EFAULT;' assignment for case above.
>>
>> net/vmw_vsock/virtio_transport_common.c | 59 +++++++++++++++++++------
>> 1 file changed, 45 insertions(+), 14 deletions(-)
>>
>> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>> index 6564192e7f20..a300f25749ea 100644
>> --- a/net/vmw_vsock/virtio_transport_common.c
>> +++ b/net/vmw_vsock/virtio_transport_common.c
>> @@ -196,7 +196,8 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
>>     const struct virtio_transport *t_ops;
>>     struct virtio_vsock_sock *vvs;
>>     u32 pkt_len = info->pkt_len;
>> -    struct sk_buff *skb;
>> +    u32 rest_len;
>> +    int ret;
>>
>>     info->type = virtio_transport_get_type(sk_vsock(vsk));
>>
>> @@ -216,10 +217,6 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
>>
>>     vvs = vsk->trans;
>>
>> -    /* we can send less than pkt_len bytes */
>> -    if (pkt_len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE)
>> -        pkt_len = VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
>> -
>>     /* virtio_transport_get_credit might return less than pkt_len credit */
>>     pkt_len = virtio_transport_get_credit(vvs, pkt_len);
>>
>> @@ -227,17 +224,51 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
>>     if (pkt_len == 0 && info->op == VIRTIO_VSOCK_OP_RW)
>>         return pkt_len;
>>
>> -    skb = virtio_transport_alloc_skb(info, pkt_len,
>> -                     src_cid, src_port,
>> -                     dst_cid, dst_port);
>> -    if (!skb) {
>> -        virtio_transport_put_credit(vvs, pkt_len);
>> -        return -ENOMEM;
>> -    }
>> +    ret = 0;
>
> nit: this initialization seems superfluous since `ret` is
> overwritten later ...
>
>> +    rest_len = pkt_len;
>> +
>> +    do {
>> +        struct sk_buff *skb;
>> +        size_t skb_len;
>> +
>> +        skb_len = min_t(u32, VIRTIO_VSOCK_MAX_PKT_BUF_SIZE, rest_len);
>> +
>> +        skb = virtio_transport_alloc_skb(info, skb_len,
>> +                         src_cid, src_port,
>> +                         dst_cid, dst_port);
>> +        if (!skb) {
>> +            ret = -ENOMEM;
>> +            break;
>> +        }
>>
>> -    virtio_transport_inc_tx_pkt(vvs, skb);
>> +        virtio_transport_inc_tx_pkt(vvs, skb);
>>
>> -    return t_ops->send_pkt(skb);
>> +        ret = t_ops->send_pkt(skb);
>
> ... here.
>
>> +
>
> nit: we can remove this extra line
>
>> +        if (ret < 0)
>> +            break;
>> +
>> +        /* Both virtio and vhost 'send_pkt()' returns 'skb_len',
>> +         * but for reliability use 'ret' instead of 'skb_len'.
>> +         * Also if partial send happens (e.g. 'ret' != 'skb_len')
>> +         * somehow, we break this loop, but account such returned
>> +         * value in 'virtio_transport_put_credit()'.
>> +         */
>> +        rest_len -= ret;
>> +
>> +        if (WARN_ONCE(ret != skb_len,
>> +                  "'send_pkt()' returns %i, but %zu expected\n",
>> +                  ret, skb_len))
>> +            break;
>> +    } while (rest_len);
>> +
>> +    virtio_transport_put_credit(vvs, rest_len);
>> +
>> +    /* Return number of bytes, if any data has been sent. */
>> +    if (rest_len != pkt_len)
>> +        ret = pkt_len - rest_len;
>> +
>> +    return ret;
>> }
>>
>> static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs,
>> -- 2.25.1
>>
>
> The patch LGTM:
>
> Reviewed-by: Stefano Garzarella <[email protected]>
>
> Anyway, feel free to include in the same series or as separate patch
> also the changes to avoid useless lock in virtio_transport_put_credit()
> and virtio_transport_get_credit().
>
> I would include it in this series, because before these changes, we
> used to call virtio_transport_put_credit() only in the error path,
> while now we always call it, even when rest_len is 0.

Thanks for review, done!
https://lore.kernel.org/netdev/[email protected]/
Added it as second patch to the series

Thanks, Arseniy
>
> Thanks,
> Stefano
>