* [RESEND PATCH v3] virtio_net: Fix mismatched buf address when unmapping for small packets
@ 2024-09-19 3:52 Wenbo Li
2024-09-19 7:09 ` Xuan Zhuo
0 siblings, 1 reply; 6+ messages in thread
From: Wenbo Li @ 2024-09-19 3:52 UTC (permalink / raw)
To: mst, jasowang, xuanzhuo, eperezma, davem, edumazet, kuba, pabeni
Cc: virtualization, netdev, linux-kernel, Wenbo Li, Jiahui Cen,
Ying Fang
Currently, the virtio-net driver will perform a pre-dma-mapping for
small or mergeable RX buffer. But for small packets, a mismatched address
without VIRTNET_RX_PAD and xdp_headroom is used for unmapping.
That will result in unsynchronized buffers when SWIOTLB is enabled, for
example, when running as a TDX guest.
This patch unifies the address passed to the virtio core into the address
of the virtnet header and fixes the mismatched buffer address.
Changes from v2: unify the buf that passed to the virtio core in small
and merge mode.
Changes from v1: Use ctx to get xdp_headroom.
Fixes: 295525e29a5b ("virtio_net: merge dma operations when filling mergeable buffers")
Signed-off-by: Wenbo Li <liwenbo.martin@bytedance.com>
Signed-off-by: Jiahui Cen <cenjiahui@bytedance.com>
Signed-off-by: Ying Fang <fangying.tommy@bytedance.com>
---
drivers/net/virtio_net.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 6f4781ec2b36..9446666c84aa 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -1804,9 +1804,15 @@ static struct sk_buff *receive_small(struct net_device *dev,
struct virtnet_rq_stats *stats)
{
unsigned int xdp_headroom = (unsigned long)ctx;
- struct page *page = virt_to_head_page(buf);
+ struct page *page;
struct sk_buff *skb;
+ // We passed the address of virtnet header to virtio-core,
+ // so truncate the padding.
+ buf -= VIRTNET_RX_PAD + xdp_headroom;
+
+ page = virt_to_head_page(buf);
+
len -= vi->hdr_len;
u64_stats_add(&stats->bytes, len);
@@ -2422,8 +2428,9 @@ static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
if (unlikely(!buf))
return -ENOMEM;
- virtnet_rq_init_one_sg(rq, buf + VIRTNET_RX_PAD + xdp_headroom,
- vi->hdr_len + GOOD_PACKET_LEN);
+ buf += VIRTNET_RX_PAD + xdp_headroom;
+
+ virtnet_rq_init_one_sg(rq, buf, vi->hdr_len + GOOD_PACKET_LEN);
err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
if (err < 0) {
--
2.20.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [RESEND PATCH v3] virtio_net: Fix mismatched buf address when unmapping for small packets
2024-09-19 3:52 Wenbo Li
@ 2024-09-19 7:09 ` Xuan Zhuo
0 siblings, 0 replies; 6+ messages in thread
From: Xuan Zhuo @ 2024-09-19 7:09 UTC (permalink / raw)
To: Wenbo Li
Cc: virtualization, netdev, linux-kernel, Wenbo Li, Jiahui Cen,
Ying Fang, mst, jasowang, eperezma, davem, edumazet, kuba, pabeni
On Thu, 19 Sep 2024 11:52:14 +0800, Wenbo Li <liwenbo.martin@bytedance.com> wrote:
> Currently, the virtio-net driver will perform a pre-dma-mapping for
> small or mergeable RX buffer. But for small packets, a mismatched address
> without VIRTNET_RX_PAD and xdp_headroom is used for unmapping.
>
> That will result in unsynchronized buffers when SWIOTLB is enabled, for
> example, when running as a TDX guest.
>
> This patch unifies the address passed to the virtio core into the address
> of the virtnet header and fixes the mismatched buffer address.
>
> Changes from v2: unify the buf that passed to the virtio core in small
> and merge mode.
> Changes from v1: Use ctx to get xdp_headroom.
>
> Fixes: 295525e29a5b ("virtio_net: merge dma operations when filling mergeable buffers")
> Signed-off-by: Wenbo Li <liwenbo.martin@bytedance.com>
> Signed-off-by: Jiahui Cen <cenjiahui@bytedance.com>
> Signed-off-by: Ying Fang <fangying.tommy@bytedance.com>
> ---
> drivers/net/virtio_net.c | 13 ++++++++++---
> 1 file changed, 10 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 6f4781ec2b36..9446666c84aa 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
> @@ -1804,9 +1804,15 @@ static struct sk_buff *receive_small(struct net_device *dev,
> struct virtnet_rq_stats *stats)
> {
> unsigned int xdp_headroom = (unsigned long)ctx;
> - struct page *page = virt_to_head_page(buf);
> + struct page *page;
Because that here is head page, so you can keep the original code.
> struct sk_buff *skb;
>
> + // We passed the address of virtnet header to virtio-core,
> + // so truncate the padding.
Please check the kernel code style.
Thanks.
> + buf -= VIRTNET_RX_PAD + xdp_headroom;
> +
> + page = virt_to_head_page(buf);
> +
> len -= vi->hdr_len;
> u64_stats_add(&stats->bytes, len);
>
> @@ -2422,8 +2428,9 @@ static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
> if (unlikely(!buf))
> return -ENOMEM;
>
> - virtnet_rq_init_one_sg(rq, buf + VIRTNET_RX_PAD + xdp_headroom,
> - vi->hdr_len + GOOD_PACKET_LEN);
> + buf += VIRTNET_RX_PAD + xdp_headroom;
> +
> + virtnet_rq_init_one_sg(rq, buf, vi->hdr_len + GOOD_PACKET_LEN);
>
> err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
> if (err < 0) {
> --
> 2.20.1
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* [RESEND PATCH v3] virtio_net: Fix mismatched buf address when unmapping for small packets
@ 2024-09-19 8:13 Wenbo Li
2024-09-19 8:32 ` Xuan Zhuo
2024-09-26 8:40 ` patchwork-bot+netdevbpf
0 siblings, 2 replies; 6+ messages in thread
From: Wenbo Li @ 2024-09-19 8:13 UTC (permalink / raw)
To: mst, jasowang, xuanzhuo, eperezma, davem, edumazet, kuba, pabeni
Cc: virtualization, netdev, linux-kernel, Wenbo Li, Jiahui Cen,
Ying Fang
Currently, the virtio-net driver will perform a pre-dma-mapping for
small or mergeable RX buffer. But for small packets, a mismatched address
without VIRTNET_RX_PAD and xdp_headroom is used for unmapping.
That will result in unsynchronized buffers when SWIOTLB is enabled, for
example, when running as a TDX guest.
This patch unifies the address passed to the virtio core as the address of
the virtnet header and fixes the mismatched buffer address.
Changes from v2: unify the buf that passed to the virtio core in small
and merge mode.
Changes from v1: Use ctx to get xdp_headroom.
Fixes: 295525e29a5b ("virtio_net: merge dma operations when filling mergeable buffers")
Signed-off-by: Wenbo Li <liwenbo.martin@bytedance.com>
Signed-off-by: Jiahui Cen <cenjiahui@bytedance.com>
Signed-off-by: Ying Fang <fangying.tommy@bytedance.com>
---
drivers/net/virtio_net.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 6f4781ec2b36..f8131f92a392 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -1807,6 +1807,11 @@ static struct sk_buff *receive_small(struct net_device *dev,
struct page *page = virt_to_head_page(buf);
struct sk_buff *skb;
+ /* We passed the address of virtnet header to virtio-core,
+ * so truncate the padding.
+ */
+ buf -= VIRTNET_RX_PAD + xdp_headroom;
+
len -= vi->hdr_len;
u64_stats_add(&stats->bytes, len);
@@ -2422,8 +2427,9 @@ static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
if (unlikely(!buf))
return -ENOMEM;
- virtnet_rq_init_one_sg(rq, buf + VIRTNET_RX_PAD + xdp_headroom,
- vi->hdr_len + GOOD_PACKET_LEN);
+ buf += VIRTNET_RX_PAD + xdp_headroom;
+
+ virtnet_rq_init_one_sg(rq, buf, vi->hdr_len + GOOD_PACKET_LEN);
err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
if (err < 0) {
--
2.20.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [RESEND PATCH v3] virtio_net: Fix mismatched buf address when unmapping for small packets
2024-09-19 8:13 [RESEND PATCH v3] virtio_net: Fix mismatched buf address when unmapping for small packets Wenbo Li
@ 2024-09-19 8:32 ` Xuan Zhuo
2024-09-19 8:33 ` Xuan Zhuo
2024-09-26 8:40 ` patchwork-bot+netdevbpf
1 sibling, 1 reply; 6+ messages in thread
From: Xuan Zhuo @ 2024-09-19 8:32 UTC (permalink / raw)
To: Wenbo Li
Cc: virtualization, netdev, linux-kernel, Wenbo Li, Jiahui Cen,
Ying Fang, mst, jasowang, eperezma, davem, edumazet, kuba, pabeni
On Thu, 19 Sep 2024 16:13:51 +0800, Wenbo Li <liwenbo.martin@bytedance.com> wrote:
> Currently, the virtio-net driver will perform a pre-dma-mapping for
> small or mergeable RX buffer. But for small packets, a mismatched address
> without VIRTNET_RX_PAD and xdp_headroom is used for unmapping.
>
> That will result in unsynchronized buffers when SWIOTLB is enabled, for
> example, when running as a TDX guest.
>
> This patch unifies the address passed to the virtio core as the address of
> the virtnet header and fixes the mismatched buffer address.
>
> Changes from v2: unify the buf that passed to the virtio core in small
> and merge mode.
> Changes from v1: Use ctx to get xdp_headroom.
>
> Fixes: 295525e29a5b ("virtio_net: merge dma operations when filling mergeable buffers")
> Signed-off-by: Wenbo Li <liwenbo.martin@bytedance.com>
> Signed-off-by: Jiahui Cen <cenjiahui@bytedance.com>
> Signed-off-by: Ying Fang <fangying.tommy@bytedance.com>
Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Thanks.
> ---
> drivers/net/virtio_net.c | 10 ++++++++--
> 1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 6f4781ec2b36..f8131f92a392 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
> @@ -1807,6 +1807,11 @@ static struct sk_buff *receive_small(struct net_device *dev,
> struct page *page = virt_to_head_page(buf);
> struct sk_buff *skb;
>
> + /* We passed the address of virtnet header to virtio-core,
> + * so truncate the padding.
> + */
> + buf -= VIRTNET_RX_PAD + xdp_headroom;
> +
> len -= vi->hdr_len;
> u64_stats_add(&stats->bytes, len);
>
> @@ -2422,8 +2427,9 @@ static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
> if (unlikely(!buf))
> return -ENOMEM;
>
> - virtnet_rq_init_one_sg(rq, buf + VIRTNET_RX_PAD + xdp_headroom,
> - vi->hdr_len + GOOD_PACKET_LEN);
> + buf += VIRTNET_RX_PAD + xdp_headroom;
> +
> + virtnet_rq_init_one_sg(rq, buf, vi->hdr_len + GOOD_PACKET_LEN);
>
> err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
> if (err < 0) {
> --
> 2.20.1
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [RESEND PATCH v3] virtio_net: Fix mismatched buf address when unmapping for small packets
2024-09-19 8:32 ` Xuan Zhuo
@ 2024-09-19 8:33 ` Xuan Zhuo
0 siblings, 0 replies; 6+ messages in thread
From: Xuan Zhuo @ 2024-09-19 8:33 UTC (permalink / raw)
To: Xuan Zhuo
Cc: virtualization, netdev, linux-kernel, Wenbo Li, Jiahui Cen,
Ying Fang, mst, jasowang, eperezma, davem, edumazet, kuba, pabeni,
Wenbo Li
On Thu, 19 Sep 2024 16:32:45 +0800, Xuan Zhuo <xuanzhuo@linux.alibaba.com> wrote:
> On Thu, 19 Sep 2024 16:13:51 +0800, Wenbo Li <liwenbo.martin@bytedance.com> wrote:
> > Currently, the virtio-net driver will perform a pre-dma-mapping for
> > small or mergeable RX buffer. But for small packets, a mismatched address
> > without VIRTNET_RX_PAD and xdp_headroom is used for unmapping.
> >
> > That will result in unsynchronized buffers when SWIOTLB is enabled, for
> > example, when running as a TDX guest.
> >
> > This patch unifies the address passed to the virtio core as the address of
> > the virtnet header and fixes the mismatched buffer address.
> >
> > Changes from v2: unify the buf that passed to the virtio core in small
> > and merge mode.
> > Changes from v1: Use ctx to get xdp_headroom.
> >
> > Fixes: 295525e29a5b ("virtio_net: merge dma operations when filling mergeable buffers")
> > Signed-off-by: Wenbo Li <liwenbo.martin@bytedance.com>
> > Signed-off-by: Jiahui Cen <cenjiahui@bytedance.com>
> > Signed-off-by: Ying Fang <fangying.tommy@bytedance.com>
>
> Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
For net/virtio maintainers:
Because the premapped mode is closed by default for virtio-net rx. So this bug
will not be triggered. DO NOT worry about it.
So that is ok if we merge it into next version. (The merge window is closed).
Thanks.
>
> Thanks.
>
> > ---
> > drivers/net/virtio_net.c | 10 ++++++++--
> > 1 file changed, 8 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> > index 6f4781ec2b36..f8131f92a392 100644
> > --- a/drivers/net/virtio_net.c
> > +++ b/drivers/net/virtio_net.c
> > @@ -1807,6 +1807,11 @@ static struct sk_buff *receive_small(struct net_device *dev,
> > struct page *page = virt_to_head_page(buf);
> > struct sk_buff *skb;
> >
> > + /* We passed the address of virtnet header to virtio-core,
> > + * so truncate the padding.
> > + */
> > + buf -= VIRTNET_RX_PAD + xdp_headroom;
> > +
> > len -= vi->hdr_len;
> > u64_stats_add(&stats->bytes, len);
> >
> > @@ -2422,8 +2427,9 @@ static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
> > if (unlikely(!buf))
> > return -ENOMEM;
> >
> > - virtnet_rq_init_one_sg(rq, buf + VIRTNET_RX_PAD + xdp_headroom,
> > - vi->hdr_len + GOOD_PACKET_LEN);
> > + buf += VIRTNET_RX_PAD + xdp_headroom;
> > +
> > + virtnet_rq_init_one_sg(rq, buf, vi->hdr_len + GOOD_PACKET_LEN);
> >
> > err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
> > if (err < 0) {
> > --
> > 2.20.1
> >
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [RESEND PATCH v3] virtio_net: Fix mismatched buf address when unmapping for small packets
2024-09-19 8:13 [RESEND PATCH v3] virtio_net: Fix mismatched buf address when unmapping for small packets Wenbo Li
2024-09-19 8:32 ` Xuan Zhuo
@ 2024-09-26 8:40 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 6+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-09-26 8:40 UTC (permalink / raw)
To: Wenbo Li
Cc: mst, jasowang, xuanzhuo, eperezma, davem, edumazet, kuba, pabeni,
virtualization, netdev, linux-kernel, cenjiahui, fangying.tommy
Hello:
This patch was applied to netdev/net.git (main)
by Paolo Abeni <pabeni@redhat.com>:
On Thu, 19 Sep 2024 16:13:51 +0800 you wrote:
> Currently, the virtio-net driver will perform a pre-dma-mapping for
> small or mergeable RX buffer. But for small packets, a mismatched address
> without VIRTNET_RX_PAD and xdp_headroom is used for unmapping.
>
> That will result in unsynchronized buffers when SWIOTLB is enabled, for
> example, when running as a TDX guest.
>
> [...]
Here is the summary with links:
- [RESEND,v3] virtio_net: Fix mismatched buf address when unmapping for small packets
https://git.kernel.org/netdev/net/c/c11a49d58ad2
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-09-26 8:40 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-19 8:13 [RESEND PATCH v3] virtio_net: Fix mismatched buf address when unmapping for small packets Wenbo Li
2024-09-19 8:32 ` Xuan Zhuo
2024-09-19 8:33 ` Xuan Zhuo
2024-09-26 8:40 ` patchwork-bot+netdevbpf
-- strict thread matches above, loose matches on Subject: below --
2024-09-19 3:52 Wenbo Li
2024-09-19 7:09 ` Xuan Zhuo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox