Skip to contentSkip to conversation

virtio-net: make copy len check in xdp_linearize_page

PATCH In review [email protected] 3 participants 6 messages Dec 13, 2021 – Dec 14, 2021

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio-net: make copy len check in xdp_linearize_page

From: mengensun <[email protected]>

xdp_linearize_page asume ring elem size is smaller then page size when copy the first ring elem, but, there may be a elem size bigger then page size.

add_recvbuf_mergeable may add a hole to ring elem, the hole size is not sure, according EWMA.

so, fix it by check copy len,if checked failed, just dropped the whole frame, not make the memory dirty after the page.

---
 drivers/net/virtio_net.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +5 -1
index 36a4b7c195d5..844bdbd67ff7 100644
@@ -662,8 +662,12 @@ static struct page *xdp_linearize_page(struct receive_queue *rq,
 				       int page_off,
 				       unsigned int *len)
 {
-	struct page *page = alloc_page(GFP_ATOMIC);
+	struct page *page;
 
+	if (*len > PAGE_SIZE - page_off)
+		return NULL;
+
+	page = alloc_page(GFP_ATOMIC);
 	if (!page)
 		return NULL;
 
Signature · 1 line
2.27.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · On Mon, 13 Dec 2021 12:50:12 +0800, [email protected] wrote:

From: mengensun <[email protected]>

xdp_linearize_page asume ring elem size is smaller then page size when copy the first ring elem, but, there may be a elem size bigger then page size.

add_recvbuf_mergeable may add a hole to ring elem, the hole size is not sure, according EWMA.

Quoted 6 lines

so, fix it by check copy len,if checked failed, just dropped the whole frame, not make the memory dirty after the page.

LGTM

Quoted 23 lines
---
 drivers/net/virtio_net.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +5 -2
index 36a4b7c195d5..844bdbd67ff7 100644
@@ -662,8 +662,12 @@ static struct page *xdp_linearize_page(struct receive_queue *rq,
 				       int page_off,
 				       unsigned int *len)
 {
-	struct page *page = alloc_page(GFP_ATOMIC);
+	struct page *page;
 
+	if (*len > PAGE_SIZE - page_off)
+		return NULL;
+
+	page = alloc_page(GFP_ATOMIC);
 	if (!page)
 		return NULL;
 
--
Quoted 2 lines

2.27.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#2 lore raw
In reply to [email protected]
Quoted 9 lines · On Mon, Dec 13, 2021 at 12:50 PM <[email protected]> wrote:

From: mengensun <[email protected]>

xdp_linearize_page asume ring elem size is smaller then page size when copy the first ring elem, but, there may be a elem size bigger then page size.

add_recvbuf_mergeable may add a hole to ring elem, the hole size is not sure, according EWMA.

The logic is to try to avoid dropping packets in this case, so I wonder if it's better to "fix" the add_recvbuf_mergeable().

Or another idea is to switch to use XDP generic here where we can use skb_linearize() which should be more robust and we can drop the xdp_linearize_page() logic completely.

Thanks

Quoted 32 lines

so, fix it by check copy len,if checked failed, just dropped the whole frame, not make the memory dirty after the page.

---
 drivers/net/virtio_net.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +5 -2
index 36a4b7c195d5..844bdbd67ff7 100644
@@ -662,8 +662,12 @@ static struct page *xdp_linearize_page(struct receive_queue *rq,
                                       int page_off,
                                       unsigned int *len)
 {
-       struct page *page = alloc_page(GFP_ATOMIC);
+       struct page *page;
 
+       if (*len > PAGE_SIZE - page_off)
+               return NULL;
+
+       page = alloc_page(GFP_ATOMIC);
        if (!page)
                return NULL;
 
--

2.27.0

#3 lore raw
Quoted 11 lines · 孙蒙恩
Quoted 9 lines · Jason Wang <[email protected]> 于2021年12月13日周一 15:49写道:
Quoted 7 lines · On Mon, Dec 13, 2021 at 12:50 PM <[email protected]> wrote:

From: mengensun <[email protected]>

xdp_linearize_page asume ring elem size is smaller then page size when copy the first ring elem, but, there may be a elem size bigger then page size.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

add_recvbuf_mergeable may add a hole to ring elem, the hole size is not sure, according EWMA.

The logic is to try to avoid dropping packets in this case, so I wonder if it's better to "fix" the add_recvbuf_mergeable().

Adding lists back.

Quoted 9 lines

turn to XDP generic is so difficulty for me, here can "fix" the add_recvbuf_mergeable link follow:

drivers/net/virtio_net.c +0 -0
drivers/net/virtio_net.c +0 -0
index 36a4b7c195d5..06ce8bb10b47 100644

drivers/net/virtio_net.c static int add_recvbuf_mergeable(struct virtnet_info *vi, · line 1319

Quoted 5 lines
drivers/net/virtio_net.c +1 -0
@@ -1315,6 +1315,7 @@ static int add_recvbuf_mergeable(struct virtnet_info *vi,
                alloc_frag->offset += hole;
        }
+       len = min(len, PAGE_SIZE - room);
        sg_init_one(rq->sg, buf, len);
        ctx = mergeable_len_to_ctx(len, headroom);

Then the truesize here is wrong.

Quoted 4 lines
drivers/net/virtio_net.c +0 -0
        err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
 

it seems a rule that, length of elem giving to vring is away smaller or equall then PAGE_SIZE

It aims to be consistent to what EWMA tries to do:

len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
rq->min_buf_len, PAGE_SIZE - hdr_len);

Thanks

Quoted 42 lines
Quoted 41 lines

Or another idea is to switch to use XDP generic here where we can use skb_linearize() which should be more robust and we can drop the xdp_linearize_page() logic completely.

Thanks

Quoted 32 lines

so, fix it by check copy len,if checked failed, just dropped the whole frame, not make the memory dirty after the page.

---
 drivers/net/virtio_net.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +5 -2
index 36a4b7c195d5..844bdbd67ff7 100644
@@ -662,8 +662,12 @@ static struct page *xdp_linearize_page(struct receive_queue *rq,
                                       int page_off,
                                       unsigned int *len)
 {
-       struct page *page = alloc_page(GFP_ATOMIC);
+       struct page *page;
 
+       if (*len > PAGE_SIZE - page_off)
+               return NULL;
+
+       page = alloc_page(GFP_ATOMIC);
        if (!page)
                return NULL;
 
--

2.27.0

author
#4 lore raw
Quoted 22 lines · Jason Wang <[email protected]> 于2021年12月14日周二 11:13写道:
Quoted 11 lines · 孙蒙恩
Quoted 9 lines · Jason Wang <[email protected]> 于2021年12月13日周一 15:49写道:
Quoted 7 lines · On Mon, Dec 13, 2021 at 12:50 PM <[email protected]> wrote:

From: mengensun <[email protected]>

xdp_linearize_page asume ring elem size is smaller then page size when copy the first ring elem, but, there may be a elem size bigger then page size.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

add_recvbuf_mergeable may add a hole to ring elem, the hole size is not sure, according EWMA.

The logic is to try to avoid dropping packets in this case, so I wonder if it's better to "fix" the add_recvbuf_mergeable().

Adding lists back.

Quoted 16 lines
Quoted 9 lines

turn to XDP generic is so difficulty for me, here can "fix" the add_recvbuf_mergeable link follow:

drivers/net/virtio_net.c +0 -0
drivers/net/virtio_net.c +0 -0
index 36a4b7c195d5..06ce8bb10b47 100644

drivers/net/virtio_net.c static int add_recvbuf_mergeable(struct virtnet_info *vi, · line 1319

Quoted 5 lines
drivers/net/virtio_net.c +1 -0
@@ -1315,6 +1315,7 @@ static int add_recvbuf_mergeable(struct virtnet_info *vi,
                alloc_frag->offset += hole;
        }
+       len = min(len, PAGE_SIZE - room);
        sg_init_one(rq->sg, buf, len);
        ctx = mergeable_len_to_ctx(len, headroom);

Then the truesize here is wrong.

many thanks!! i have known i'm wrong immediately after click the "send" botton , now, this problem seem not only about the *hole* but the EWMA, EWMA will give buff len bewteen min_buf_len and PAGE_SIZE, while swith from no-attach-xdp to attach xdp, there may be some buff already in ring and filled before xdp attach. xdp_linearize_page assume buf size is PAGE_SIZE - VIRTIO_XDP_HEADROOM, and coped "len" from the buff, while the buff may be **PAGE_SIZE**

because we have no idear when the user attach xdp prog, so, i have no idear except make all the buff have a "header hole" len of VIRTIO_XDP_HEADROOM(128), but it seem so expensive for no-xdp-attach virtio eth to aways leave 128 byte not used at all.

this problem is found by review code, in really test, it seemed not so may big frame. so here we can just "fix" the xdp_linearize_page, make it trying best to not drop frames for now?

btw, it seem no way to fix this thoroughly, except we drained all the buff in the ring, and flush it all to "xdp buff" when attaching xdp prog.

is that some mistake i have makeed again? #^_^

Quoted 57 lines
Quoted 6 lines
drivers/net/virtio_net.c +0 -0
        err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
 

it seems a rule that, length of elem giving to vring is away smaller or equall then PAGE_SIZE

It aims to be consistent to what EWMA tries to do:

len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
rq->min_buf_len, PAGE_SIZE - hdr_len);

Thanks

Quoted 42 lines
Quoted 41 lines

Or another idea is to switch to use XDP generic here where we can use skb_linearize() which should be more robust and we can drop the xdp_linearize_page() logic completely.

Thanks

Quoted 32 lines

so, fix it by check copy len,if checked failed, just dropped the whole frame, not make the memory dirty after the page.

---
 drivers/net/virtio_net.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +5 -2
index 36a4b7c195d5..844bdbd67ff7 100644
@@ -662,8 +662,12 @@ static struct page *xdp_linearize_page(struct receive_queue *rq,
                                       int page_off,
                                       unsigned int *len)
 {
-       struct page *page = alloc_page(GFP_ATOMIC);
+       struct page *page;
 
+       if (*len > PAGE_SIZE - page_off)
+               return NULL;
+
+       page = alloc_page(GFP_ATOMIC);
        if (!page)
                return NULL;
 
--

2.27.0

#5 lore raw
Quoted 40 lines · 孙蒙恩
Quoted 22 lines · Jason Wang <[email protected]> 于2021年12月14日周二 11:13写道:
Quoted 11 lines · 孙蒙恩
Quoted 9 lines · Jason Wang <[email protected]> 于2021年12月13日周一 15:49写道:
Quoted 7 lines · On Mon, Dec 13, 2021 at 12:50 PM <[email protected]> wrote:

From: mengensun <[email protected]>

xdp_linearize_page asume ring elem size is smaller then page size when copy the first ring elem, but, there may be a elem size bigger then page size.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

add_recvbuf_mergeable may add a hole to ring elem, the hole size is not sure, according EWMA.

The logic is to try to avoid dropping packets in this case, so I wonder if it's better to "fix" the add_recvbuf_mergeable().

Adding lists back.

Quoted 16 lines
Quoted 9 lines

turn to XDP generic is so difficulty for me, here can "fix" the add_recvbuf_mergeable link follow:

drivers/net/virtio_net.c +0 -0
drivers/net/virtio_net.c +0 -0
index 36a4b7c195d5..06ce8bb10b47 100644

drivers/net/virtio_net.c static int add_recvbuf_mergeable(struct virtnet_info *vi, · line 1319

Quoted 5 lines
drivers/net/virtio_net.c +1 -0
@@ -1315,6 +1315,7 @@ static int add_recvbuf_mergeable(struct virtnet_info *vi,
                alloc_frag->offset += hole;
        }
+       len = min(len, PAGE_SIZE - room);
        sg_init_one(rq->sg, buf, len);
        ctx = mergeable_len_to_ctx(len, headroom);

Then the truesize here is wrong.

Quoted 7 lines

many thanks!! i have known i'm wrong immediately after click the "send" botton , now, this problem seem not only about the *hole* but the EWMA, EWMA will give buff len bewteen min_buf_len and PAGE_SIZE, while swith from no-attach-xdp to attach xdp, there may be some buff already in ring and filled before xdp attach. xdp_linearize_page assume buf size is PAGE_SIZE - VIRTIO_XDP_HEADROOM, and coped "len" from the buff, while the buff may be **PAGE_SIZE**

So the issue I see is that though add_recvbuf_mergeable() tries to make the buffer size is PAGE_SIZE, XDP might requires more on the header which makes a single page is not sufficient.

Quoted 5 lines

because we have no idear when the user attach xdp prog, so, i have no idear except make all the buff have a "header hole" len of VIRTIO_XDP_HEADROOM(128), but it seem so expensive for no-xdp-attach virtio eth to aways leave 128 byte not used at all.

That's an requirement for XDP header adjustment so far.

Quoted 4 lines

this problem is found by review code, in really test, it seemed not so may big frame. so here we can just "fix" the xdp_linearize_page, make it trying best to not drop frames for now?

I think you can reproduce the issue by keeping sending large frames to guest and try to attach XDP in the middle.

Quoted 6 lines

btw, it seem no way to fix this thoroughly, except we drained all the buff in the ring, and flush it all to "xdp buff" when attaching xdp prog.

is that some mistake i have makeed again? #^_^

I see two ways to solve this:

1) reverse more room (but not headerroom) to make sure PAGE_SIZE can work in the case of linearizing
2) switch to use XDP genreic

2) looks much more easier, you may refer tun_xdp_one() to see how it works, it's as simple as call do_xdp_generic()

Thanks

Quoted 59 lines
Quoted 58 lines
Quoted 7 lines
drivers/net/virtio_net.c +0 -0
        err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
 

it seems a rule that, length of elem giving to vring is away smaller or equall then PAGE_SIZE

It aims to be consistent to what EWMA tries to do:

len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
rq->min_buf_len, PAGE_SIZE - hdr_len);

Thanks

Quoted 42 lines
Quoted 41 lines

Or another idea is to switch to use XDP generic here where we can use skb_linearize() which should be more robust and we can drop the xdp_linearize_page() logic completely.

Thanks

Quoted 32 lines

so, fix it by check copy len,if checked failed, just dropped the whole frame, not make the memory dirty after the page.

---
 drivers/net/virtio_net.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +5 -2
index 36a4b7c195d5..844bdbd67ff7 100644
@@ -662,8 +662,12 @@ static struct page *xdp_linearize_page(struct receive_queue *rq,
                                       int page_off,
                                       unsigned int *len)
 {
-       struct page *page = alloc_page(GFP_ATOMIC);
+       struct page *page;
 
+       if (*len > PAGE_SIZE - page_off)
+               return NULL;
+
+       page = alloc_page(GFP_ATOMIC);
        if (!page)
                return NULL;
 
--

2.27.0