Skip to contentSkip to conversation

xen/netback: fix issue introduced recently

PATCH Applied Juergen Gross 4 participants 11 messages Mar 27, 2023 – Mar 28, 2023

Subsystems (4)

v1 of 2 Applied 2/2 Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover xen/netback: fix issue introduced recently

The fix for XSA-423 introduced a bug which resulted in loss of network connection in some configurations.

The first patch is fixing the issue, while the second one is removing a test which isn't needed.

Juergen Gross (2):
xen/netback: don't do grant copy across page boundary xen/netback: remove not needed test in xenvif_tx_build_gops()

 drivers/net/xen-netback/common.h  |  2 +-
 drivers/net/xen-netback/netback.c | 29 +++++++++++++++++++++++------
 2 files changed, 24 insertions(+), 7 deletions(-)
Signature · 1 line
2.35.3
#1 lore raw

PATCH 1/2 xen/netback: don't do grant copy across page boundary

Fix xenvif_get_requests() not to do grant copy operations across local page boundaries. This requires to double the maximum number of copy operations per queue, as each copy could now be split into 2.

Make sure that struct xenvif_tx_cb doesn't grow too large.

Fixes ad7f402ae4f4 ("xen/netback: Ensure protocol headers don't fall in the non-linear area")
---
 drivers/net/xen-netback/common.h  |  2 +-
 drivers/net/xen-netback/netback.c | 25 +++++++++++++++++++++++--
 2 files changed, 24 insertions(+), 3 deletions(-)
drivers/net/xen-netback/common.h +1 -1
index 3dbfc8a6924e..1fcbd83f7ff2 100644
@@ -166,7 +166,7 @@ struct xenvif_queue { /* Per-queue data for xenvif */
 	struct pending_tx_info pending_tx_info[MAX_PENDING_REQS];
 	grant_handle_t grant_tx_handle[MAX_PENDING_REQS];
 
-	struct gnttab_copy tx_copy_ops[MAX_PENDING_REQS];
+	struct gnttab_copy tx_copy_ops[2 * MAX_PENDING_REQS];
 	struct gnttab_map_grant_ref tx_map_ops[MAX_PENDING_REQS];
 	struct gnttab_unmap_grant_ref tx_unmap_ops[MAX_PENDING_REQS];
 	/* passed to gnttab_[un]map_refs with pages under (un)mapping */
drivers/net/xen-netback/netback.c +23 -2
index 1b42676ca141..111c179f161b 100644
@@ -334,6 +334,7 @@ static int xenvif_count_requests(struct xenvif_queue *queue,
 struct xenvif_tx_cb {
 	u16 copy_pending_idx[XEN_NETBK_LEGACY_SLOTS_MAX + 1];
 	u8 copy_count;
+	u32 split_mask;
 };
 
 #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
@@ -361,6 +362,8 @@ static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
 	struct sk_buff *skb =
 		alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
 			  GFP_ATOMIC | __GFP_NOWARN);
+
+	BUILD_BUG_ON(sizeof(*XENVIF_TX_CB(skb)) > sizeof(skb->cb));
 	if (unlikely(skb == NULL))
 		return NULL;
 
@@ -396,11 +399,13 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
 	nr_slots = shinfo->nr_frags + 1;
 
 	copy_count(skb) = 0;
+	XENVIF_TX_CB(skb)->split_mask = 0;
 
 	/* Create copy ops for exactly data_len bytes into the skb head. */
 	__skb_put(skb, data_len);
 	while (data_len > 0) {
 		int amount = data_len > txp->size ? txp->size : data_len;
+		bool split = false;
 
 		cop->source.u.ref = txp->gref;
 		cop->source.domid = queue->vif->domid;
@@ -413,6 +418,13 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
 		cop->dest.u.gmfn = virt_to_gfn(skb->data + skb_headlen(skb)
 				               - data_len);
 
+		/* Don't cross local page boundary! */
+		if (cop->dest.offset + amount > XEN_PAGE_SIZE) {
+			amount = XEN_PAGE_SIZE - cop->dest.offset;
+			XENVIF_TX_CB(skb)->split_mask |= 1U << copy_count(skb);
+			split = true;
+		}
+
 		cop->len = amount;
 		cop->flags = GNTCOPY_source_gref;
 
@@ -420,7 +432,8 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
 		pending_idx = queue->pending_ring[index];
 		callback_param(queue, pending_idx).ctx = NULL;
 		copy_pending_idx(skb, copy_count(skb)) = pending_idx;
-		copy_count(skb)++;
+		if (!split)
+			copy_count(skb)++;
 
 		cop++;
 		data_len -= amount;
@@ -441,7 +454,8 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
 			nr_slots--;
 		} else {
 			/* The copy op partially covered the tx_request.
-			 * The remainder will be mapped.
+			 * The remainder will be mapped or copied in the next
+			 * iteration.
 			 */
 			txp->offset += amount;
 			txp->size -= amount;
@@ -539,6 +553,13 @@ static int xenvif_tx_check_gop(struct xenvif_queue *queue,
 		pending_idx = copy_pending_idx(skb, i);
 
 		newerr = (*gopp_copy)->status;
+
+		/* Split copies need to be handled together. */
+		if (XENVIF_TX_CB(skb)->split_mask & (1U << i)) {
+			(*gopp_copy)++;
+			if (!newerr)
+				newerr = (*gopp_copy)->status;
+		}
 		if (likely(!newerr)) {
 			/* The first frag might still have this slot mapped */
 			if (i < copy_count(skb) - 1 || !sharedslot)
Signature · 1 line
2.35.3
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] xen/netback: don't do grant copy across page boundary

Quoted 6 lines · Juergen Gross

Fix xenvif_get_requests() not to do grant copy operations across local page boundaries. This requires to double the maximum number of copy operations per queue, as each copy could now be split into 2.

Make sure that struct xenvif_tx_cb doesn't grow too large.

Quoted 8 lines
Fixes ad7f402ae4f4 ("xen/netback: Ensure protocol headers don't fall in the non-linear area")
---
  drivers/net/xen-netback/common.h  |  2 +-
  drivers/net/xen-netback/netback.c | 25 +++++++++++++++++++++++--
  2 files changed, 24 insertions(+), 3 deletions(-)
#3 lore raw

Re: [PATCH 1/2] xen/netback: don't do grant copy across page boundary

In reply to Juergen Gross
Quoted 63 lines · Juergen Gross

Fix xenvif_get_requests() not to do grant copy operations across local page boundaries. This requires to double the maximum number of copy operations per queue, as each copy could now be split into 2.

Make sure that struct xenvif_tx_cb doesn't grow too large.

Fixes ad7f402ae4f4 ("xen/netback: Ensure protocol headers don't fall in the non-linear area")
---
 drivers/net/xen-netback/common.h  |  2 +-
 drivers/net/xen-netback/netback.c | 25 +++++++++++++++++++++++--
 2 files changed, 24 insertions(+), 3 deletions(-)
drivers/net/xen-netback/common.h +1 -1
index 3dbfc8a6924e..1fcbd83f7ff2 100644
@@ -166,7 +166,7 @@ struct xenvif_queue { /* Per-queue data for xenvif */
 	struct pending_tx_info pending_tx_info[MAX_PENDING_REQS];
 	grant_handle_t grant_tx_handle[MAX_PENDING_REQS];
 
-	struct gnttab_copy tx_copy_ops[MAX_PENDING_REQS];
+	struct gnttab_copy tx_copy_ops[2 * MAX_PENDING_REQS];
 	struct gnttab_map_grant_ref tx_map_ops[MAX_PENDING_REQS];
 	struct gnttab_unmap_grant_ref tx_unmap_ops[MAX_PENDING_REQS];
 	/* passed to gnttab_[un]map_refs with pages under (un)mapping */
drivers/net/xen-netback/netback.c +5 -0
index 1b42676ca141..111c179f161b 100644
@@ -334,6 +334,7 @@ static int xenvif_count_requests(struct xenvif_queue *queue,
 struct xenvif_tx_cb {
 	u16 copy_pending_idx[XEN_NETBK_LEGACY_SLOTS_MAX + 1];
 	u8 copy_count;
+	u32 split_mask;
 };
 
 #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
@@ -361,6 +362,8 @@ static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
 	struct sk_buff *skb =
 		alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
 			  GFP_ATOMIC | __GFP_NOWARN);
+
+	BUILD_BUG_ON(sizeof(*XENVIF_TX_CB(skb)) > sizeof(skb->cb));
 	if (unlikely(skb == NULL))
 		return NULL;
 
@@ -396,11 +399,13 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
 	nr_slots = shinfo->nr_frags + 1;
 
 	copy_count(skb) = 0;
+	XENVIF_TX_CB(skb)->split_mask = 0;
 
 	/* Create copy ops for exactly data_len bytes into the skb head. */
 	__skb_put(skb, data_len);
 	while (data_len > 0) {
 		int amount = data_len > txp->size ? txp->size : data_len;
+		bool split = false;
 
 		cop->source.u.ref = txp->gref;
 		cop->source.domid = queue->vif->domid;

drivers/net/xen-netback/netback.c static void xenvif_get_requests(struct xenvif_queue *queue, · line 424

Quoted 7 lines
drivers/net/xen-netback/netback.c +4 -0
@@ -413,6 +418,13 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
 		cop->dest.u.gmfn = virt_to_gfn(skb->data + skb_headlen(skb)
 				               - data_len);
 
+		/* Don't cross local page boundary! */
+		if (cop->dest.offset + amount > XEN_PAGE_SIZE) {
+			amount = XEN_PAGE_SIZE - cop->dest.offset;
+			XENVIF_TX_CB(skb)->split_mask |= 1U << copy_count(skb);

Maybe worthwhile to add a BUILD_BUG_ON() somewhere to make sure this shift won't grow too large a shift count. The number of slots accepted could conceivably be grown past XEN_NETBK_LEGACY_SLOTS_MAX (i.e. XEN_NETIF_NR_SLOTS_MIN) at some point.

Quoted 27 lines
drivers/net/xen-netback/netback.c +7 -2
+			split = true;
+		}
+
 		cop->len = amount;
 		cop->flags = GNTCOPY_source_gref;
 
@@ -420,7 +432,8 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
 		pending_idx = queue->pending_ring[index];
 		callback_param(queue, pending_idx).ctx = NULL;
 		copy_pending_idx(skb, copy_count(skb)) = pending_idx;
-		copy_count(skb)++;
+		if (!split)
+			copy_count(skb)++;
 
 		cop++;
 		data_len -= amount;
@@ -441,7 +454,8 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
 			nr_slots--;
 		} else {
 			/* The copy op partially covered the tx_request.
-			 * The remainder will be mapped.
+			 * The remainder will be mapped or copied in the next
+			 * iteration.
 			 */
 			txp->offset += amount;
 			txp->size -= amount;

drivers/net/xen-netback/netback.c static int xenvif_tx_check_gop(struct xenvif_queue *queue, · line 562

Quoted 10 lines
drivers/net/xen-netback/netback.c +7 -0
@@ -539,6 +553,13 @@ static int xenvif_tx_check_gop(struct xenvif_queue *queue,
 		pending_idx = copy_pending_idx(skb, i);
 
 		newerr = (*gopp_copy)->status;
+
+		/* Split copies need to be handled together. */
+		if (XENVIF_TX_CB(skb)->split_mask & (1U << i)) {
+			(*gopp_copy)++;
+			if (!newerr)
+				newerr = (*gopp_copy)->status;
+		}

It isn't guaranteed that a slot may be split only once, is it? Assuming a near-64k packet with all tiny non-primary slots, that'll cause those tiny slots to all be mapped, but due to

		if (ret >= XEN_NETBK_LEGACY_SLOTS_MAX - 1 && data_len < txreq.size)
			data_len = txreq.size;

will, afaict, cause a lot of copying for the primary slot. Therefore I think you need a loop here, not just an if(). Plus tx_copy_ops[]'es dimension also looks to need further growing to accommodate this. Or maybe not - at least the extreme example given would still be fine; more generally packets being limited to below 64k means 2*16 slots would suffice at one end of the scale, while 2*MAX_PENDING_REQS would at the other end (all tiny, including the primary slot). What I haven't fully convinced myself of is whether there might be cases in the middle which are yet worse.

As I've been struggling with the code fragment quoted above already in the patch originally introducing it, I'd like to see that relaxed. Can't we avoid excessive copying by suitably growing tx_map_ops[] and then deleting that bumping of data_len? Then there also wouldn't be the risk multiple splits per copy anymore.

Alternatively to all of the above: Am I overlooking a check somewhere which would also constrain the primary slot (or more precisely its residual) to within a single page (along the lines of the check for non-primary slots in xenvif_count_requests())?

Jan

#4 lore raw

Re: [PATCH 1/2] xen/netback: don't do grant copy across page boundary

Quoted 67 lines · Jan Beulich
Quoted 66 lines · Juergen Gross

Fix xenvif_get_requests() not to do grant copy operations across local page boundaries. This requires to double the maximum number of copy operations per queue, as each copy could now be split into 2.

Make sure that struct xenvif_tx_cb doesn't grow too large.

Fixes ad7f402ae4f4 ("xen/netback: Ensure protocol headers don't fall in the non-linear area")
---
  drivers/net/xen-netback/common.h  |  2 +-
  drivers/net/xen-netback/netback.c | 25 +++++++++++++++++++++++--
  2 files changed, 24 insertions(+), 3 deletions(-)
drivers/net/xen-netback/common.h +1 -1
index 3dbfc8a6924e..1fcbd83f7ff2 100644
@@ -166,7 +166,7 @@ struct xenvif_queue { /* Per-queue data for xenvif */
  	struct pending_tx_info pending_tx_info[MAX_PENDING_REQS];
  	grant_handle_t grant_tx_handle[MAX_PENDING_REQS];
 
-	struct gnttab_copy tx_copy_ops[MAX_PENDING_REQS];
+	struct gnttab_copy tx_copy_ops[2 * MAX_PENDING_REQS];
  	struct gnttab_map_grant_ref tx_map_ops[MAX_PENDING_REQS];
  	struct gnttab_unmap_grant_ref tx_unmap_ops[MAX_PENDING_REQS];
  	/* passed to gnttab_[un]map_refs with pages under (un)mapping */
drivers/net/xen-netback/netback.c +5 -0
index 1b42676ca141..111c179f161b 100644
@@ -334,6 +334,7 @@ static int xenvif_count_requests(struct xenvif_queue *queue,
  struct xenvif_tx_cb {
  	u16 copy_pending_idx[XEN_NETBK_LEGACY_SLOTS_MAX + 1];
  	u8 copy_count;
+	u32 split_mask;
  };
 
  #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
@@ -361,6 +362,8 @@ static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
  	struct sk_buff *skb =
  		alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
  			  GFP_ATOMIC | __GFP_NOWARN);
+
+	BUILD_BUG_ON(sizeof(*XENVIF_TX_CB(skb)) > sizeof(skb->cb));
  	if (unlikely(skb == NULL))
  		return NULL;
 
@@ -396,11 +399,13 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
  	nr_slots = shinfo->nr_frags + 1;
 
  	copy_count(skb) = 0;
+	XENVIF_TX_CB(skb)->split_mask = 0;
 
  	/* Create copy ops for exactly data_len bytes into the skb head. */
  	__skb_put(skb, data_len);
  	while (data_len > 0) {
  		int amount = data_len > txp->size ? txp->size : data_len;
+		bool split = false;
 
  		cop->source.u.ref = txp->gref;
  		cop->source.domid = queue->vif->domid;
@@ -413,6 +418,13 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
  		cop->dest.u.gmfn = virt_to_gfn(skb->data + skb_headlen(skb)
  				               - data_len);
 
Quoted 9 lines

drivers/net/xen-netback/netback.c static void xenvif_get_requests(struct xenvif_queue *queue, · line 424

Quoted 4 lines
drivers/net/xen-netback/netback.c +4 -0
+		/* Don't cross local page boundary! */
+		if (cop->dest.offset + amount > XEN_PAGE_SIZE) {
+			amount = XEN_PAGE_SIZE - cop->dest.offset;
+			XENVIF_TX_CB(skb)->split_mask |= 1U << copy_count(skb);

Maybe worthwhile to add a BUILD_BUG_ON() somewhere to make sure this shift won't grow too large a shift count. The number of slots accepted could conceivably be grown past XEN_NETBK_LEGACY_SLOTS_MAX (i.e. XEN_NETIF_NR_SLOTS_MIN) at some point.

This is basically impossible due to the size restriction of struct xenvif_tx_cb.

Quoted 30 lines
Quoted 30 lines
drivers/net/xen-netback/netback.c +7 -2
+			split = true;
+		}
+
  		cop->len = amount;
  		cop->flags = GNTCOPY_source_gref;
 
@@ -420,7 +432,8 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
  		pending_idx = queue->pending_ring[index];
  		callback_param(queue, pending_idx).ctx = NULL;
  		copy_pending_idx(skb, copy_count(skb)) = pending_idx;
-		copy_count(skb)++;
+		if (!split)
+			copy_count(skb)++;
 
  		cop++;
  		data_len -= amount;
@@ -441,7 +454,8 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
  			nr_slots--;
  		} else {
  			/* The copy op partially covered the tx_request.
-			 * The remainder will be mapped.
+			 * The remainder will be mapped or copied in the next
+			 * iteration.
  			 */
  			txp->offset += amount;
  			txp->size -= amount;
@@ -539,6 +553,13 @@ static int xenvif_tx_check_gop(struct xenvif_queue *queue,
  		pending_idx = copy_pending_idx(skb, i);
 
Quoted 10 lines

drivers/net/xen-netback/netback.c static int xenvif_tx_check_gop(struct xenvif_queue *queue, · line 562

Quoted 8 lines
drivers/net/xen-netback/netback.c +7 -0
  		newerr = (*gopp_copy)->status;
+
+		/* Split copies need to be handled together. */
+		if (XENVIF_TX_CB(skb)->split_mask & (1U << i)) {
+			(*gopp_copy)++;
+			if (!newerr)
+				newerr = (*gopp_copy)->status;
+		}

It isn't guaranteed that a slot may be split only once, is it? Assuming a

I think it is guaranteed.

No slot can cover more than XEN_PAGE_SIZE bytes due to the grants being restricted to that size. There is no way how such a data packet could cross 2 page boundaries.

In the end the problem isn't the copies for the linear area not crossing multiple page boundaries, but the copies for a single request slot not doing so. And this can't happen IMO.

Quoted 6 lines

near-64k packet with all tiny non-primary slots, that'll cause those tiny slots to all be mapped, but due to

		if (ret >= XEN_NETBK_LEGACY_SLOTS_MAX - 1 && data_len < txreq.size)
			data_len = txreq.size;
Quoted 9 lines

will, afaict, cause a lot of copying for the primary slot. Therefore I think you need a loop here, not just an if(). Plus tx_copy_ops[]'es dimension also looks to need further growing to accommodate this. Or maybe not - at least the extreme example given would still be fine; more generally packets being limited to below 64k means 2*16 slots would suffice at one end of the scale, while 2*MAX_PENDING_REQS would at the other end (all tiny, including the primary slot). What I haven't fully convinced myself of is whether there might be cases in the middle which are yet worse.

See above reasoning. I think it is okay, but maybe I'm missing something.

Juergen

#5 lore raw

Re: [PATCH 1/2] xen/netback: don't do grant copy across page boundary

Quoted 16 lines · Juergen Gross
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Juergen Gross
drivers/net/xen-netback/netback.c +0 -0
@@ -413,6 +418,13 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
  		cop->dest.u.gmfn = virt_to_gfn(skb->data + skb_headlen(skb)
  				               - data_len);
 
Quoted 9 lines

drivers/net/xen-netback/netback.c static void xenvif_get_requests(struct xenvif_queue *queue, · line 424

Quoted 4 lines
drivers/net/xen-netback/netback.c +4 -0
+		/* Don't cross local page boundary! */
+		if (cop->dest.offset + amount > XEN_PAGE_SIZE) {
+			amount = XEN_PAGE_SIZE - cop->dest.offset;
+			XENVIF_TX_CB(skb)->split_mask |= 1U << copy_count(skb);

Maybe worthwhile to add a BUILD_BUG_ON() somewhere to make sure this shift won't grow too large a shift count. The number of slots accepted could conceivably be grown past XEN_NETBK_LEGACY_SLOTS_MAX (i.e. XEN_NETIF_NR_SLOTS_MIN) at some point.

Quoted 2 lines

This is basically impossible due to the size restriction of struct xenvif_tx_cb.

If its size became a problem, it might simply take a level of indirection to overcome the limitation.

Quoted 36 lines
Quoted 23 lines
Quoted 23 lines
drivers/net/xen-netback/netback.c +4 -2
@@ -420,7 +432,8 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
  		pending_idx = queue->pending_ring[index];
  		callback_param(queue, pending_idx).ctx = NULL;
  		copy_pending_idx(skb, copy_count(skb)) = pending_idx;
-		copy_count(skb)++;
+		if (!split)
+			copy_count(skb)++;
 
  		cop++;
  		data_len -= amount;
@@ -441,7 +454,8 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
  			nr_slots--;
  		} else {
  			/* The copy op partially covered the tx_request.
-			 * The remainder will be mapped.
+			 * The remainder will be mapped or copied in the next
+			 * iteration.
  			 */
  			txp->offset += amount;
  			txp->size -= amount;
@@ -539,6 +553,13 @@ static int xenvif_tx_check_gop(struct xenvif_queue *queue,
  		pending_idx = copy_pending_idx(skb, i);
 
Quoted 10 lines

drivers/net/xen-netback/netback.c static int xenvif_tx_check_gop(struct xenvif_queue *queue, · line 562

Quoted 8 lines
drivers/net/xen-netback/netback.c +7 -0
  		newerr = (*gopp_copy)->status;
+
+		/* Split copies need to be handled together. */
+		if (XENVIF_TX_CB(skb)->split_mask & (1U << i)) {
+			(*gopp_copy)++;
+			if (!newerr)
+				newerr = (*gopp_copy)->status;
+		}

It isn't guaranteed that a slot may be split only once, is it? Assuming a

I think it is guaranteed.

Quoted 7 lines

No slot can cover more than XEN_PAGE_SIZE bytes due to the grants being restricted to that size. There is no way how such a data packet could cross 2 page boundaries.

In the end the problem isn't the copies for the linear area not crossing multiple page boundaries, but the copies for a single request slot not doing so. And this can't happen IMO.

You're thinking of only well-formed requests. What about said request providing a large size with only tiny fragments? xenvif_get_requests() will happily process such, creating bogus grant-copy ops. But them failing once submitted to Xen will be only after damage may already have occurred (from bogus updates of internal state; the logic altogether is too involved for me to be convinced that nothing bad can happen).

Interestingly (as I realize now) the shifts you add are not be at risk of turning UB in this case, as the shift count won't go beyond 16.

Quoted 16 lines
Quoted 6 lines

near-64k packet with all tiny non-primary slots, that'll cause those tiny slots to all be mapped, but due to

		if (ret >= XEN_NETBK_LEGACY_SLOTS_MAX - 1 && data_len < txreq.size)
			data_len = txreq.size;
Quoted 9 lines

will, afaict, cause a lot of copying for the primary slot. Therefore I think you need a loop here, not just an if(). Plus tx_copy_ops[]'es dimension also looks to need further growing to accommodate this. Or maybe not - at least the extreme example given would still be fine; more generally packets being limited to below 64k means 2*16 slots would suffice at one end of the scale, while 2*MAX_PENDING_REQS would at the other end (all tiny, including the primary slot). What I haven't fully convinced myself of is whether there might be cases in the middle which are yet worse.

Quoted 1 line

See above reasoning. I think it is okay, but maybe I'm missing something.

Well, the main thing I'm missing is a "primary request fits in a page" check, even more so with the new copying logic that the commit referenced by Fixes: introduced into xenvif_get_requests().

Jan

#6 lore raw

Re: [PATCH 1/2] xen/netback: don't do grant copy across page boundary

Quoted 17 lines · Jan Beulich
Quoted 16 lines · Juergen Gross
Quoted 14 lines · Jan Beulich

drivers/net/xen-netback/netback.c static void xenvif_get_requests(struct xenvif_queue *queue, · line 424

Quoted 8 lines · Juergen Gross
drivers/net/xen-netback/netback.c +4 -0
@@ -413,6 +418,13 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
   		cop->dest.u.gmfn = virt_to_gfn(skb->data + skb_headlen(skb)
   				               - data_len);
 
+		/* Don't cross local page boundary! */
+		if (cop->dest.offset + amount > XEN_PAGE_SIZE) {
+			amount = XEN_PAGE_SIZE - cop->dest.offset;
+			XENVIF_TX_CB(skb)->split_mask |= 1U << copy_count(skb);

Maybe worthwhile to add a BUILD_BUG_ON() somewhere to make sure this shift won't grow too large a shift count. The number of slots accepted could conceivably be grown past XEN_NETBK_LEGACY_SLOTS_MAX (i.e. XEN_NETIF_NR_SLOTS_MIN) at some point.

Quoted 5 lines
Quoted 2 lines

This is basically impossible due to the size restriction of struct xenvif_tx_cb.

If its size became a problem, it might simply take a level of indirection to overcome the limitation.

Maybe.

OTOH this would require some rework, which should take such problems into consideration.

In the end I'd be fine to add such a BUILD_BUG_ON(), as the code is complicated enough already.

Quoted 45 lines
Quoted 37 lines
Quoted 24 lines
Quoted 24 lines
drivers/net/xen-netback/netback.c +4 -2
@@ -420,7 +432,8 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
   		pending_idx = queue->pending_ring[index];
   		callback_param(queue, pending_idx).ctx = NULL;
   		copy_pending_idx(skb, copy_count(skb)) = pending_idx;
-		copy_count(skb)++;
+		if (!split)
+			copy_count(skb)++;
 
   		cop++;
   		data_len -= amount;
@@ -441,7 +454,8 @@ static void xenvif_get_requests(struct xenvif_queue *queue,
   			nr_slots--;
   		} else {
   			/* The copy op partially covered the tx_request.
-			 * The remainder will be mapped.
+			 * The remainder will be mapped or copied in the next
+			 * iteration.
   			 */
   			txp->offset += amount;
   			txp->size -= amount;
@@ -539,6 +553,13 @@ static int xenvif_tx_check_gop(struct xenvif_queue *queue,
   		pending_idx = copy_pending_idx(skb, i);
 
Quoted 10 lines

drivers/net/xen-netback/netback.c static int xenvif_tx_check_gop(struct xenvif_queue *queue, · line 562

Quoted 8 lines
drivers/net/xen-netback/netback.c +7 -0
   		newerr = (*gopp_copy)->status;
+
+		/* Split copies need to be handled together. */
+		if (XENVIF_TX_CB(skb)->split_mask & (1U << i)) {
+			(*gopp_copy)++;
+			if (!newerr)
+				newerr = (*gopp_copy)->status;
+		}

It isn't guaranteed that a slot may be split only once, is it? Assuming a

I think it is guaranteed.

Quoted 7 lines

No slot can cover more than XEN_PAGE_SIZE bytes due to the grants being restricted to that size. There is no way how such a data packet could cross 2 page boundaries.

In the end the problem isn't the copies for the linear area not crossing multiple page boundaries, but the copies for a single request slot not doing so. And this can't happen IMO.

Quoted 6 lines

You're thinking of only well-formed requests. What about said request providing a large size with only tiny fragments? xenvif_get_requests() will happily process such, creating bogus grant-copy ops. But them failing once submitted to Xen will be only after damage may already have occurred (from bogus updates of internal state; the logic altogether is too involved for me to be convinced that nothing bad can happen).

There are sanity checks after each relevant RING_COPY_REQUEST() call, which will bail out if "(txp->offset + txp->size) > XEN_PAGE_SIZE" (the first one is after the call of xenvif_count_requests(), as this call will decrease the size of the request, the other check is in xenvif_count_requests()).

Quoted 19 lines

Interestingly (as I realize now) the shifts you add are not be at risk of turning UB in this case, as the shift count won't go beyond 16.

Quoted 16 lines
Quoted 15 lines

near-64k packet with all tiny non-primary slots, that'll cause those tiny slots to all be mapped, but due to

		if (ret >= XEN_NETBK_LEGACY_SLOTS_MAX - 1 && data_len < txreq.size)
			data_len = txreq.size;

will, afaict, cause a lot of copying for the primary slot. Therefore I think you need a loop here, not just an if(). Plus tx_copy_ops[]'es dimension also looks to need further growing to accommodate this. Or maybe not - at least the extreme example given would still be fine; more generally packets being limited to below 64k means 2*16 slots would suffice at one end of the scale, while 2*MAX_PENDING_REQS would at the other end (all tiny, including the primary slot). What I haven't fully convinced myself of is whether there might be cases in the middle which are yet worse.

Quoted 5 lines
Quoted 1 line

See above reasoning. I think it is okay, but maybe I'm missing something.

Well, the main thing I'm missing is a "primary request fits in a page" check, even more so with the new copying logic that the commit referenced by Fixes: introduced into xenvif_get_requests().

When xenvif_get_requests() gets called, all requests are sanity checked already (note that xenvif_get_requests() is working on the local copies of the requests).

Juergen

#7 lore raw

Re: [PATCH 1/2] xen/netback: don't do grant copy across page boundary

Quoted 35 lines · Juergen Gross
Quoted 27 lines · Jan Beulich
Quoted 18 lines · Juergen Gross
Quoted 4 lines · Jan Beulich
Quoted 3 lines · Juergen Gross
drivers/net/xen-netback/netback.c +0 -0
@@ -539,6 +553,13 @@ static int xenvif_tx_check_gop(struct xenvif_queue *queue,
   		pending_idx = copy_pending_idx(skb, i);
 
Quoted 10 lines

drivers/net/xen-netback/netback.c static int xenvif_tx_check_gop(struct xenvif_queue *queue, · line 562

Quoted 8 lines
drivers/net/xen-netback/netback.c +7 -0
   		newerr = (*gopp_copy)->status;
+
+		/* Split copies need to be handled together. */
+		if (XENVIF_TX_CB(skb)->split_mask & (1U << i)) {
+			(*gopp_copy)++;
+			if (!newerr)
+				newerr = (*gopp_copy)->status;
+		}

It isn't guaranteed that a slot may be split only once, is it? Assuming a

I think it is guaranteed.

Quoted 7 lines

No slot can cover more than XEN_PAGE_SIZE bytes due to the grants being restricted to that size. There is no way how such a data packet could cross 2 page boundaries.

In the end the problem isn't the copies for the linear area not crossing multiple page boundaries, but the copies for a single request slot not doing so. And this can't happen IMO.

Quoted 6 lines

You're thinking of only well-formed requests. What about said request providing a large size with only tiny fragments? xenvif_get_requests() will happily process such, creating bogus grant-copy ops. But them failing once submitted to Xen will be only after damage may already have occurred (from bogus updates of internal state; the logic altogether is too involved for me to be convinced that nothing bad can happen).

Quoted 4 lines

There are sanity checks after each relevant RING_COPY_REQUEST() call, which will bail out if "(txp->offset + txp->size) > XEN_PAGE_SIZE" (the first one is after the call of xenvif_count_requests(), as this call will decrease the size of the request, the other check is in xenvif_count_requests()).

Oh, indeed - that's the check I've been overlooking. (The messages logged there could do with also mentioning "Cross page boundary", like the one in xenvif_count_requests() does.)

Jan

#8 lore raw

PATCH 2/2 xen/netback: remove not needed test in xenvif_tx_build_gops()

In reply to Juergen Gross

The tests for the number of grant mapping or copy operations reaching the array size of the operations buffer at the end of the main loop in xenvif_tx_build_gops() isn't needed.

The loop can handle at maximum MAX_PENDING_REQS transfer requests, as XEN_RING_NR_UNCONSUMED_REQUESTS() is taking unsent responses into consideration, too.

Remove the tests.

---
 drivers/net/xen-netback/netback.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/net/xen-netback/netback.c +0 -4
index 111c179f161b..4943be4fd99d 100644
@@ -1082,10 +1082,6 @@ static void xenvif_tx_build_gops(struct xenvif_queue *queue,
 		__skb_queue_tail(&queue->tx_queue, skb);
 
 		queue->tx.req_cons = idx;
-
-		if ((*map_ops >= ARRAY_SIZE(queue->tx_map_ops)) ||
-		    (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
-			break;
 	}
 
 	return;
Signature · 1 line
2.35.3
Reviewed-by on v1 2/2
#9 lore raw

Re: [PATCH 2/2] xen/netback: remove not needed test in xenvif_tx_build_gops()

Quoted 8 lines · Juergen Gross

The tests for the number of grant mapping or copy operations reaching the array size of the operations buffer at the end of the main loop in xenvif_tx_build_gops() isn't needed.

The loop can handle at maximum MAX_PENDING_REQS transfer requests, as XEN_RING_NR_UNCONSUMED_REQUESTS() is taking unsent responses into consideration, too.

Quoted 8 lines

Remove the tests.

---
  drivers/net/xen-netback/netback.c | 4 ----
  1 file changed, 4 deletions(-)
#10 lore raw
In reply to Juergen Gross
This series was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:

Quoted 11 lines · On Mon, 27 Mar 2023 10:36:44 +0200 you wrote:

The fix for XSA-423 introduced a bug which resulted in loss of network connection in some configurations.

The first patch is fixing the issue, while the second one is removing a test which isn't needed.

Juergen Gross (2):
xen/netback: don't do grant copy across page boundary xen/netback: remove not needed test in xenvif_tx_build_gops()

[...]

Here is the summary with links:
- [1/2] xen/netback: don't do grant copy across page boundary

    https://git.kernel.org/netdev/net/c/05310f31ca74
  - [2/2] xen/netback: remove not needed test in xenvif_tx_build_gops()
    https://git.kernel.org/netdev/net/c/8fb8ebf94877

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html