Skip to contentSkip to conversation

xen-netback: fix ethtool stats and memory

PATCH In review David Vrabel 5 participants 17 messages Mar 3, 2015 – Mar 4, 2015

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

cover xen-netback: fix ethtool stats and memory

A couple of bug fixes for netback:
- make ethool stats to report the correct values.
- don't leak 1 MiB every time a VIF is destroyed.

David

#1 lore raw

PATCH 1/2 xen-netback: return correct ethtool stats

Use correct pointer arithmetic to get the pointer to each stat.

---
 drivers/net/xen-netback/interface.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/xen-netback/interface.c +1 -2
index f38227a..3aa8648 100644
@@ -340,12 +340,11 @@ static void xenvif_get_ethtool_stats(struct net_device *dev,
 	unsigned int num_queues = vif->num_queues;
 	int i;
 	unsigned int queue_index;
-	struct xenvif_stats *vif_stats;
 
 	for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) {
 		unsigned long accum = 0;
 		for (queue_index = 0; queue_index < num_queues; ++queue_index) {
-			vif_stats = &vif->queues[queue_index].stats;
+			void *vif_stats = &vif->queues[queue_index].stats;
 			accum += *(unsigned long *)(vif_stats + xenvif_stats[i].offset);
 		}
 		data[i] = accum;
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCHv1 1/2] xen-netback: return correct ethtool stats

Hello.

Quoted 12 lines · David Vrabel

Use correct pointer arithmetic to get the pointer to each stat.

---
  drivers/net/xen-netback/interface.c |    3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/xen-netback/interface.c +0 -0
index f38227a..3aa8648 100644

drivers/net/xen-netback/interface.c static void xenvif_get_ethtool_stats(struct net_device *dev, · line 347

Quoted 10 lines
drivers/net/xen-netback/interface.c +1 -2
@@ -340,12 +340,11 @@ static void xenvif_get_ethtool_stats(struct net_device *dev,
  	unsigned int num_queues = vif->num_queues;
  	int i;
  	unsigned int queue_index;
-	struct xenvif_stats *vif_stats;
 
  	for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) {
  		unsigned long accum = 0;
  		for (queue_index = 0; queue_index < num_queues; ++queue_index) {
-			vif_stats = &vif->queues[queue_index].stats;
+			void *vif_stats = &vif->queues[queue_index].stats;

Need empty line after declaration; checkpatch.pl should have complained here.

drivers/net/xen-netback/interface.c static void xenvif_get_ethtool_stats(struct net_device *dev, · line 350

Quoted 3 lines
drivers/net/xen-netback/interface.c +0 -0
  			accum += *(unsigned long *)(vif_stats + xenvif_stats[i].offset);
  		}
  		data[i] = accum;

WBR, Sergei

#3 lore raw

Re: [PATCHv1 1/2] xen-netback: return correct ethtool stats

In reply to David Vrabel

Hello.

Quoted 12 lines · David Vrabel

Use correct pointer arithmetic to get the pointer to each stat.

---
  drivers/net/xen-netback/interface.c |    3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/xen-netback/interface.c +0 -0
index f38227a..3aa8648 100644

drivers/net/xen-netback/interface.c static void xenvif_get_ethtool_stats(struct net_device *dev, · line 347

Quoted 10 lines
drivers/net/xen-netback/interface.c +1 -2
@@ -340,12 +340,11 @@ static void xenvif_get_ethtool_stats(struct net_device *dev,
  	unsigned int num_queues = vif->num_queues;
  	int i;
  	unsigned int queue_index;
-	struct xenvif_stats *vif_stats;
 
  	for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) {
  		unsigned long accum = 0;
  		for (queue_index = 0; queue_index < num_queues; ++queue_index) {
-			vif_stats = &vif->queues[queue_index].stats;
+			void *vif_stats = &vif->queues[queue_index].stats;

Need empty line after declaration; checkpatch.pl should have complained here.

drivers/net/xen-netback/interface.c static void xenvif_get_ethtool_stats(struct net_device *dev, · line 350

Quoted 3 lines
drivers/net/xen-netback/interface.c +0 -0
  			accum += *(unsigned long *)(vif_stats + xenvif_stats[i].offset);
  		}
  		data[i] = accum;

WBR, Sergei

#4 lore raw

PATCH 1/2 xen-netback: return correct ethtool stats

In reply to David Vrabel

Use correct pointer arithmetic to get the pointer to each stat.

---
 drivers/net/xen-netback/interface.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/xen-netback/interface.c +1 -2
index f38227a..3aa8648 100644
@@ -340,12 +340,11 @@ static void xenvif_get_ethtool_stats(struct net_device *dev,
 	unsigned int num_queues = vif->num_queues;
 	int i;
 	unsigned int queue_index;
-	struct xenvif_stats *vif_stats;
 
 	for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) {
 		unsigned long accum = 0;
 		for (queue_index = 0; queue_index < num_queues; ++queue_index) {
-			vif_stats = &vif->queues[queue_index].stats;
+			void *vif_stats = &vif->queues[queue_index].stats;
 			accum += *(unsigned long *)(vif_stats + xenvif_stats[i].offset);
 		}
 		data[i] = accum;
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH 2/2 xen-netback: unref frags when handling a from-guest skb with a frag list

In reply to David Vrabel

Every time a VIF is destroyed up-to 256 pages may be leaked if packets with more than MAX_SKB_FRAGS frags where transmitted from the guest. Even worse, if another user of ballooned pages allocated one of these ballooned pages it would not handle the the unexpectedly non-zero page count (e.g., gntdev would deadlock when unmapping a grant because the page count would never reach 1).

When handling a from-guest skb with a frag list, unref the frags before releasing them so they are freed correctly when the VIF is destroyed.

Also swap over to the new (local) frags /after/ calling the skb destructor. This isn't strictly necessary but it's less confusing.

---
 drivers/net/xen-netback/netback.c |   43 +++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 21 deletions(-)
drivers/net/xen-netback/netback.c +22 -21
index f7a31d2..3d06eeb 100644
@@ -1343,7 +1343,7 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
 {
 	unsigned int offset = skb_headlen(skb);
 	skb_frag_t frags[MAX_SKB_FRAGS];
-	int i;
+	int i, nr_frags;
 	struct ubuf_info *uarg;
 	struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
 
@@ -1357,17 +1357,16 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
 	skb->data_len += nskb->len;
 
 	/* create a brand new frags array and coalesce there */
-	for (i = 0; offset < skb->len; i++) {
+	for (nr_frags = 0; offset < skb->len; nr_frags++) {
 		struct page *page;
 		unsigned int len;
 
-		BUG_ON(i >= MAX_SKB_FRAGS);
+		BUG_ON(nr_frags >= MAX_SKB_FRAGS);
 		page = alloc_page(GFP_ATOMIC);
 		if (!page) {
-			int j;
 			skb->truesize += skb->data_len;
-			for (j = 0; j < i; j++)
-				put_page(frags[j].page.p);
+			for (i = 0; i < nr_frags; i++)
+				put_page(frags[i].page.p);
 			return -ENOMEM;
 		}
 
@@ -1379,27 +1378,29 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
 			BUG();
 
 		offset += len;
-		frags[i].page.p = page;
-		frags[i].page_offset = 0;
-		skb_frag_size_set(&frags[i], len);
+		frags[nr_frags].page.p = page;
+		frags[nr_frags].page_offset = 0;
+		skb_frag_size_set(&frags[nr_frags], len);
 	}
-	/* swap out with old one */
-	memcpy(skb_shinfo(skb)->frags,
-	       frags,
-	       i * sizeof(skb_frag_t));
-	skb_shinfo(skb)->nr_frags = i;
-	skb->truesize += i * PAGE_SIZE;
-
-	/* remove traces of mapped pages and frag_list */
+
+	/* Copied all the bits from the frag list -- free it. */
 	skb_frag_list_init(skb);
-	uarg = skb_shinfo(skb)->destructor_arg;
-	/* increase inflight counter to offset decrement in callback */
+	xenvif_skb_zerocopy_prepare(queue, nskb);
+	kfree_skb(nskb);
+
+	/* Release all the original (foreign) frags. */
+	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
+		skb_frag_unref(skb, i);
 	atomic_inc(&queue->inflight_packets);
+	uarg = skb_shinfo(skb)->destructor_arg;
 	uarg->callback(uarg, true);
 	skb_shinfo(skb)->destructor_arg = NULL;
 
-	xenvif_skb_zerocopy_prepare(queue, nskb);
-	kfree_skb(nskb);
+	/* Fill the skb with the new (local) frags. */
+	memcpy(skb_shinfo(skb)->frags, frags,
+	       nr_frags * sizeof(skb_frag_t));
+	skb_shinfo(skb)->nr_frags = nr_frags;
+	skb->truesize += nr_frags * PAGE_SIZE;
 
 	return 0;
 }
Signature · 1 line
1.7.10.4
Reviewed-by on v1 2/2
#6 lore raw

Re: [Xen-devel] [PATCHv1 2/2] xen-netback: unref frags when handling a from-guest skb with a frag list

Quoted 1 line · David Vrabel

Every time a VIF is destroyed up-to 256 pages may be leaked if packets

"up to" (and maybe a comma before it?)

Quoted 1 line

with more than MAX_SKB_FRAGS frags where transmitted from the guest.

were

Quoted 2 lines

Even worse, if another user of ballooned pages allocated one of these ballooned pages it would not handle the the unexpectedly non-zero page

                                          ^^^

One too many "the"

Quoted 11 lines

count (e.g., gntdev would deadlock when unmapping a grant because the page count would never reach 1).

When handling a from-guest skb with a frag list, unref the frags before releasing them so they are freed correctly when the VIF is destroyed.

Also swap over to the new (local) frags /after/ calling the skb destructor. This isn't strictly necessary but it's less confusing.

Quoted 85 lines
---
  drivers/net/xen-netback/netback.c |   43 +++++++++++++++++++------------------
  1 file changed, 22 insertions(+), 21 deletions(-)
drivers/net/xen-netback/netback.c +22 -21
index f7a31d2..3d06eeb 100644
@@ -1343,7 +1343,7 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
  {
  	unsigned int offset = skb_headlen(skb);
  	skb_frag_t frags[MAX_SKB_FRAGS];
-	int i;
+	int i, nr_frags;
  	struct ubuf_info *uarg;
  	struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
 
@@ -1357,17 +1357,16 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
  	skb->data_len += nskb->len;
 
  	/* create a brand new frags array and coalesce there */
-	for (i = 0; offset < skb->len; i++) {
+	for (nr_frags = 0; offset < skb->len; nr_frags++) {
  		struct page *page;
  		unsigned int len;
 
-		BUG_ON(i >= MAX_SKB_FRAGS);
+		BUG_ON(nr_frags >= MAX_SKB_FRAGS);
  		page = alloc_page(GFP_ATOMIC);
  		if (!page) {
-			int j;
  			skb->truesize += skb->data_len;
-			for (j = 0; j < i; j++)
-				put_page(frags[j].page.p);
+			for (i = 0; i < nr_frags; i++)
+				put_page(frags[i].page.p);
  			return -ENOMEM;
  		}
 
@@ -1379,27 +1378,29 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
  			BUG();
 
  		offset += len;
-		frags[i].page.p = page;
-		frags[i].page_offset = 0;
-		skb_frag_size_set(&frags[i], len);
+		frags[nr_frags].page.p = page;
+		frags[nr_frags].page_offset = 0;
+		skb_frag_size_set(&frags[nr_frags], len);
  	}
-	/* swap out with old one */
-	memcpy(skb_shinfo(skb)->frags,
-	       frags,
-	       i * sizeof(skb_frag_t));
-	skb_shinfo(skb)->nr_frags = i;
-	skb->truesize += i * PAGE_SIZE;
-
-	/* remove traces of mapped pages and frag_list */
+
+	/* Copied all the bits from the frag list -- free it. */
  	skb_frag_list_init(skb);
-	uarg = skb_shinfo(skb)->destructor_arg;
-	/* increase inflight counter to offset decrement in callback */
+	xenvif_skb_zerocopy_prepare(queue, nskb);
+	kfree_skb(nskb);
+
+	/* Release all the original (foreign) frags. */
+	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
+		skb_frag_unref(skb, i);
  	atomic_inc(&queue->inflight_packets);
+	uarg = skb_shinfo(skb)->destructor_arg;
  	uarg->callback(uarg, true);
  	skb_shinfo(skb)->destructor_arg = NULL;
 
-	xenvif_skb_zerocopy_prepare(queue, nskb);
-	kfree_skb(nskb);
+	/* Fill the skb with the new (local) frags. */
+	memcpy(skb_shinfo(skb)->frags, frags,
+	       nr_frags * sizeof(skb_frag_t));
+	skb_shinfo(skb)->nr_frags = nr_frags;
+	skb->truesize += nr_frags * PAGE_SIZE;
 
  	return 0;
  }
Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCHv1 2/2] xen-netback: unref frags when handling a from-guest skb with a frag list

In reply to David Vrabel
Quoted 1 line · David Vrabel

Every time a VIF is destroyed up-to 256 pages may be leaked if packets

"up to" (and maybe a comma before it?)

Quoted 1 line

with more than MAX_SKB_FRAGS frags where transmitted from the guest.

were

Quoted 2 lines

Even worse, if another user of ballooned pages allocated one of these ballooned pages it would not handle the the unexpectedly non-zero page

                                          ^^^

One too many "the"

Quoted 11 lines

count (e.g., gntdev would deadlock when unmapping a grant because the page count would never reach 1).

When handling a from-guest skb with a frag list, unref the frags before releasing them so they are freed correctly when the VIF is destroyed.

Also swap over to the new (local) frags /after/ calling the skb destructor. This isn't strictly necessary but it's less confusing.

Quoted 85 lines
---
  drivers/net/xen-netback/netback.c |   43 +++++++++++++++++++------------------
  1 file changed, 22 insertions(+), 21 deletions(-)
drivers/net/xen-netback/netback.c +22 -21
index f7a31d2..3d06eeb 100644
@@ -1343,7 +1343,7 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
  {
  	unsigned int offset = skb_headlen(skb);
  	skb_frag_t frags[MAX_SKB_FRAGS];
-	int i;
+	int i, nr_frags;
  	struct ubuf_info *uarg;
  	struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
 
@@ -1357,17 +1357,16 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
  	skb->data_len += nskb->len;
 
  	/* create a brand new frags array and coalesce there */
-	for (i = 0; offset < skb->len; i++) {
+	for (nr_frags = 0; offset < skb->len; nr_frags++) {
  		struct page *page;
  		unsigned int len;
 
-		BUG_ON(i >= MAX_SKB_FRAGS);
+		BUG_ON(nr_frags >= MAX_SKB_FRAGS);
  		page = alloc_page(GFP_ATOMIC);
  		if (!page) {
-			int j;
  			skb->truesize += skb->data_len;
-			for (j = 0; j < i; j++)
-				put_page(frags[j].page.p);
+			for (i = 0; i < nr_frags; i++)
+				put_page(frags[i].page.p);
  			return -ENOMEM;
  		}
 
@@ -1379,27 +1378,29 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
  			BUG();
 
  		offset += len;
-		frags[i].page.p = page;
-		frags[i].page_offset = 0;
-		skb_frag_size_set(&frags[i], len);
+		frags[nr_frags].page.p = page;
+		frags[nr_frags].page_offset = 0;
+		skb_frag_size_set(&frags[nr_frags], len);
  	}
-	/* swap out with old one */
-	memcpy(skb_shinfo(skb)->frags,
-	       frags,
-	       i * sizeof(skb_frag_t));
-	skb_shinfo(skb)->nr_frags = i;
-	skb->truesize += i * PAGE_SIZE;
-
-	/* remove traces of mapped pages and frag_list */
+
+	/* Copied all the bits from the frag list -- free it. */
  	skb_frag_list_init(skb);
-	uarg = skb_shinfo(skb)->destructor_arg;
-	/* increase inflight counter to offset decrement in callback */
+	xenvif_skb_zerocopy_prepare(queue, nskb);
+	kfree_skb(nskb);
+
+	/* Release all the original (foreign) frags. */
+	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
+		skb_frag_unref(skb, i);
  	atomic_inc(&queue->inflight_packets);
+	uarg = skb_shinfo(skb)->destructor_arg;
  	uarg->callback(uarg, true);
  	skb_shinfo(skb)->destructor_arg = NULL;
 
-	xenvif_skb_zerocopy_prepare(queue, nskb);
-	kfree_skb(nskb);
+	/* Fill the skb with the new (local) frags. */
+	memcpy(skb_shinfo(skb)->frags, frags,
+	       nr_frags * sizeof(skb_frag_t));
+	skb_shinfo(skb)->nr_frags = nr_frags;
+	skb->truesize += nr_frags * PAGE_SIZE;
 
  	return 0;
  }
#8 lore raw

Re: [PATCHv1 2/2] xen-netback: unref frags when handling a from-guest skb with a frag list

In reply to David Vrabel
Quoted 10 lines · David Vrabel

Every time a VIF is destroyed up-to 256 pages may be leaked if packets with more than MAX_SKB_FRAGS frags where transmitted from the guest. Even worse, if another user of ballooned pages allocated one of these ballooned pages it would not handle the the unexpectedly non-zero page count (e.g., gntdev would deadlock when unmapping a grant because the page count would never reach 1).

When handling a from-guest skb with a frag list, unref the frags before releasing them so they are freed correctly when the VIF is destroyed.

Am I right that the majority of the first 2 hunks (and various bits of the 3rd) are just switching the outer loop to nr_frags instead of i, to free up i for use in the new code below? And also to switch j to the now available i in the inner loop.

Quoted 2 lines

Also swap over to the new (local) frags /after/ calling the skb destructor. This isn't strictly necessary but it's less confusing.

My only concern would be that this now means there is a period where the frags list is invalid. However I think the calling context is such that nobody else can have a reference to an skb which has the same shinfo as the one in our hand. Is that right?

(If not then I think the old code was also buggy/racy, but with a smaller window)

Quoted 86 lines
---
 drivers/net/xen-netback/netback.c |   43 +++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 21 deletions(-)
drivers/net/xen-netback/netback.c +22 -21
index f7a31d2..3d06eeb 100644
@@ -1343,7 +1343,7 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
 {
 	unsigned int offset = skb_headlen(skb);
 	skb_frag_t frags[MAX_SKB_FRAGS];
-	int i;
+	int i, nr_frags;
 	struct ubuf_info *uarg;
 	struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
 
@@ -1357,17 +1357,16 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
 	skb->data_len += nskb->len;
 
 	/* create a brand new frags array and coalesce there */
-	for (i = 0; offset < skb->len; i++) {
+	for (nr_frags = 0; offset < skb->len; nr_frags++) {
 		struct page *page;
 		unsigned int len;
 
-		BUG_ON(i >= MAX_SKB_FRAGS);
+		BUG_ON(nr_frags >= MAX_SKB_FRAGS);
 		page = alloc_page(GFP_ATOMIC);
 		if (!page) {
-			int j;
 			skb->truesize += skb->data_len;
-			for (j = 0; j < i; j++)
-				put_page(frags[j].page.p);
+			for (i = 0; i < nr_frags; i++)
+				put_page(frags[i].page.p);
 			return -ENOMEM;
 		}
 
@@ -1379,27 +1378,29 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
 			BUG();
 
 		offset += len;
-		frags[i].page.p = page;
-		frags[i].page_offset = 0;
-		skb_frag_size_set(&frags[i], len);
+		frags[nr_frags].page.p = page;
+		frags[nr_frags].page_offset = 0;
+		skb_frag_size_set(&frags[nr_frags], len);
 	}
-	/* swap out with old one */
-	memcpy(skb_shinfo(skb)->frags,
-	       frags,
-	       i * sizeof(skb_frag_t));
-	skb_shinfo(skb)->nr_frags = i;
-	skb->truesize += i * PAGE_SIZE;
-
-	/* remove traces of mapped pages and frag_list */
+
+	/* Copied all the bits from the frag list -- free it. */
 	skb_frag_list_init(skb);
-	uarg = skb_shinfo(skb)->destructor_arg;
-	/* increase inflight counter to offset decrement in callback */
+	xenvif_skb_zerocopy_prepare(queue, nskb);
+	kfree_skb(nskb);
+
+	/* Release all the original (foreign) frags. */
+	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
+		skb_frag_unref(skb, i);
 	atomic_inc(&queue->inflight_packets);
+	uarg = skb_shinfo(skb)->destructor_arg;
 	uarg->callback(uarg, true);
 	skb_shinfo(skb)->destructor_arg = NULL;
 
-	xenvif_skb_zerocopy_prepare(queue, nskb);
-	kfree_skb(nskb);
+	/* Fill the skb with the new (local) frags. */
+	memcpy(skb_shinfo(skb)->frags, frags,
+	       nr_frags * sizeof(skb_frag_t));
+	skb_shinfo(skb)->nr_frags = nr_frags;
+	skb->truesize += nr_frags * PAGE_SIZE;
 
 	return 0;
 }
#9 lore raw

Re: [Xen-devel] [PATCHv1 2/2] xen-netback: unref frags when handling a from-guest skb with a frag list

Quoted 8 lines · Ian Campbell
Quoted 7 lines · David Vrabel

Every time a VIF is destroyed up-to 256 pages may be leaked if packets with more than MAX_SKB_FRAGS frags where transmitted from the guest. Even worse, if another user of ballooned pages allocated one of these ballooned pages it would not handle the the unexpectedly non-zero page count (e.g., gntdev would deadlock when unmapping a grant because the page count would never reach 1).

Quoted 8 lines
Quoted 3 lines

When handling a from-guest skb with a frag list, unref the frags before releasing them so they are freed correctly when the VIF is destroyed.

Am I right that the majority of the first 2 hunks (and various bits of the 3rd) are just switching the outer loop to nr_frags instead of i, to free up i for use in the new code below? And also to switch j to the now available i in the inner loop.

Yes. If you prefer I can split this into one patch that adds the skb_frag_unref() calls and one that reorders/refactors.

Quoted 7 lines
Quoted 2 lines

Also swap over to the new (local) frags /after/ calling the skb destructor. This isn't strictly necessary but it's less confusing.

My only concern would be that this now means there is a period where the frags list is invalid. However I think the calling context is such that nobody else can have a reference to an skb which has the same shinfo as the one in our hand. Is that right?

That is correct. This skb is allocated by netback and has not yet been passed up to the network stack.

David

#10 lore raw

Re: [Xen-devel] [PATCHv1 2/2] xen-netback: unref frags when handling a from-guest skb with a frag list

Quoted 13 lines · David Vrabel
Quoted 12 lines · Ian Campbell
Quoted 10 lines · David Vrabel

Every time a VIF is destroyed up-to 256 pages may be leaked if packets with more than MAX_SKB_FRAGS frags where transmitted from the guest. Even worse, if another user of ballooned pages allocated one of these ballooned pages it would not handle the the unexpectedly non-zero page count (e.g., gntdev would deadlock when unmapping a grant because the page count would never reach 1).

When handling a from-guest skb with a frag list, unref the frags before releasing them so they are freed correctly when the VIF is destroyed.

Quoted 7 lines
Quoted 4 lines

Am I right that the majority of the first 2 hunks (and various bits of the 3rd) are just switching the outer loop to nr_frags instead of i, to free up i for use in the new code below? And also to switch j to the now available i in the inner loop.

Yes. If you prefer I can split this into one patch that adds the skb_frag_unref() calls and one that reorders/refactors.

If you can be bothered that might make things easier to read, thanks.

Quoted 10 lines
Quoted 7 lines
Quoted 2 lines

Also swap over to the new (local) frags /after/ calling the skb destructor. This isn't strictly necessary but it's less confusing.

My only concern would be that this now means there is a period where the frags list is invalid. However I think the calling context is such that nobody else can have a reference to an skb which has the same shinfo as the one in our hand. Is that right?

That is correct. This skb is allocated by netback and has not yet been passed up to the network stack.

Phew! ;-)

#11 lore raw

Re: [PATCHv1 2/2] xen-netback: unref frags when handling a from-guest skb with a frag list

In reply to David Vrabel
Quoted 13 lines · David Vrabel
Quoted 12 lines · Ian Campbell
Quoted 10 lines · David Vrabel

Every time a VIF is destroyed up-to 256 pages may be leaked if packets with more than MAX_SKB_FRAGS frags where transmitted from the guest. Even worse, if another user of ballooned pages allocated one of these ballooned pages it would not handle the the unexpectedly non-zero page count (e.g., gntdev would deadlock when unmapping a grant because the page count would never reach 1).

When handling a from-guest skb with a frag list, unref the frags before releasing them so they are freed correctly when the VIF is destroyed.

Quoted 7 lines
Quoted 4 lines

Am I right that the majority of the first 2 hunks (and various bits of the 3rd) are just switching the outer loop to nr_frags instead of i, to free up i for use in the new code below? And also to switch j to the now available i in the inner loop.

Yes. If you prefer I can split this into one patch that adds the skb_frag_unref() calls and one that reorders/refactors.

If you can be bothered that might make things easier to read, thanks.

Quoted 10 lines
Quoted 7 lines
Quoted 2 lines

Also swap over to the new (local) frags /after/ calling the skb destructor. This isn't strictly necessary but it's less confusing.

My only concern would be that this now means there is a period where the frags list is invalid. However I think the calling context is such that nobody else can have a reference to an skb which has the same shinfo as the one in our hand. Is that right?

That is correct. This skb is allocated by netback and has not yet been passed up to the network stack.

Phew! ;-)

#12 lore raw

Re: [PATCHv1 2/2] xen-netback: unref frags when handling a from-guest skb with a frag list

In reply to Ian Campbell
Quoted 8 lines · Ian Campbell
Quoted 7 lines · David Vrabel

Every time a VIF is destroyed up-to 256 pages may be leaked if packets with more than MAX_SKB_FRAGS frags where transmitted from the guest. Even worse, if another user of ballooned pages allocated one of these ballooned pages it would not handle the the unexpectedly non-zero page count (e.g., gntdev would deadlock when unmapping a grant because the page count would never reach 1).

Quoted 8 lines
Quoted 3 lines

When handling a from-guest skb with a frag list, unref the frags before releasing them so they are freed correctly when the VIF is destroyed.

Am I right that the majority of the first 2 hunks (and various bits of the 3rd) are just switching the outer loop to nr_frags instead of i, to free up i for use in the new code below? And also to switch j to the now available i in the inner loop.

Yes. If you prefer I can split this into one patch that adds the skb_frag_unref() calls and one that reorders/refactors.

Quoted 7 lines
Quoted 2 lines

Also swap over to the new (local) frags /after/ calling the skb destructor. This isn't strictly necessary but it's less confusing.

My only concern would be that this now means there is a period where the frags list is invalid. However I think the calling context is such that nobody else can have a reference to an skb which has the same shinfo as the one in our hand. Is that right?

That is correct. This skb is allocated by netback and has not yet been passed up to the network stack.

David

#13 lore raw

Re: [PATCHv1 2/2] xen-netback: unref frags when handling a from-guest skb with a frag list

In reply to David Vrabel
Quoted 10 lines · David Vrabel

Every time a VIF is destroyed up-to 256 pages may be leaked if packets with more than MAX_SKB_FRAGS frags where transmitted from the guest. Even worse, if another user of ballooned pages allocated one of these ballooned pages it would not handle the the unexpectedly non-zero page count (e.g., gntdev would deadlock when unmapping a grant because the page count would never reach 1).

When handling a from-guest skb with a frag list, unref the frags before releasing them so they are freed correctly when the VIF is destroyed.

Am I right that the majority of the first 2 hunks (and various bits of the 3rd) are just switching the outer loop to nr_frags instead of i, to free up i for use in the new code below? And also to switch j to the now available i in the inner loop.

Quoted 2 lines

Also swap over to the new (local) frags /after/ calling the skb destructor. This isn't strictly necessary but it's less confusing.

My only concern would be that this now means there is a period where the frags list is invalid. However I think the calling context is such that nobody else can have a reference to an skb which has the same shinfo as the one in our hand. Is that right?

(If not then I think the old code was also buggy/racy, but with a smaller window)

Quoted 86 lines
---
 drivers/net/xen-netback/netback.c |   43 +++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 21 deletions(-)
drivers/net/xen-netback/netback.c +22 -21
index f7a31d2..3d06eeb 100644
@@ -1343,7 +1343,7 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
 {
 	unsigned int offset = skb_headlen(skb);
 	skb_frag_t frags[MAX_SKB_FRAGS];
-	int i;
+	int i, nr_frags;
 	struct ubuf_info *uarg;
 	struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
 
@@ -1357,17 +1357,16 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
 	skb->data_len += nskb->len;
 
 	/* create a brand new frags array and coalesce there */
-	for (i = 0; offset < skb->len; i++) {
+	for (nr_frags = 0; offset < skb->len; nr_frags++) {
 		struct page *page;
 		unsigned int len;
 
-		BUG_ON(i >= MAX_SKB_FRAGS);
+		BUG_ON(nr_frags >= MAX_SKB_FRAGS);
 		page = alloc_page(GFP_ATOMIC);
 		if (!page) {
-			int j;
 			skb->truesize += skb->data_len;
-			for (j = 0; j < i; j++)
-				put_page(frags[j].page.p);
+			for (i = 0; i < nr_frags; i++)
+				put_page(frags[i].page.p);
 			return -ENOMEM;
 		}
 
@@ -1379,27 +1378,29 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
 			BUG();
 
 		offset += len;
-		frags[i].page.p = page;
-		frags[i].page_offset = 0;
-		skb_frag_size_set(&frags[i], len);
+		frags[nr_frags].page.p = page;
+		frags[nr_frags].page_offset = 0;
+		skb_frag_size_set(&frags[nr_frags], len);
 	}
-	/* swap out with old one */
-	memcpy(skb_shinfo(skb)->frags,
-	       frags,
-	       i * sizeof(skb_frag_t));
-	skb_shinfo(skb)->nr_frags = i;
-	skb->truesize += i * PAGE_SIZE;
-
-	/* remove traces of mapped pages and frag_list */
+
+	/* Copied all the bits from the frag list -- free it. */
 	skb_frag_list_init(skb);
-	uarg = skb_shinfo(skb)->destructor_arg;
-	/* increase inflight counter to offset decrement in callback */
+	xenvif_skb_zerocopy_prepare(queue, nskb);
+	kfree_skb(nskb);
+
+	/* Release all the original (foreign) frags. */
+	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
+		skb_frag_unref(skb, i);
 	atomic_inc(&queue->inflight_packets);
+	uarg = skb_shinfo(skb)->destructor_arg;
 	uarg->callback(uarg, true);
 	skb_shinfo(skb)->destructor_arg = NULL;
 
-	xenvif_skb_zerocopy_prepare(queue, nskb);
-	kfree_skb(nskb);
+	/* Fill the skb with the new (local) frags. */
+	memcpy(skb_shinfo(skb)->frags, frags,
+	       nr_frags * sizeof(skb_frag_t));
+	skb_shinfo(skb)->nr_frags = nr_frags;
+	skb->truesize += nr_frags * PAGE_SIZE;
 
 	return 0;
 }
#14 lore raw

PATCH 2/2 xen-netback: unref frags when handling a from-guest skb with a frag list

In reply to David Vrabel

Every time a VIF is destroyed up-to 256 pages may be leaked if packets with more than MAX_SKB_FRAGS frags where transmitted from the guest. Even worse, if another user of ballooned pages allocated one of these ballooned pages it would not handle the the unexpectedly non-zero page count (e.g., gntdev would deadlock when unmapping a grant because the page count would never reach 1).

When handling a from-guest skb with a frag list, unref the frags before releasing them so they are freed correctly when the VIF is destroyed.

Also swap over to the new (local) frags /after/ calling the skb destructor. This isn't strictly necessary but it's less confusing.

---
 drivers/net/xen-netback/netback.c |   43 +++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 21 deletions(-)
drivers/net/xen-netback/netback.c +22 -21
index f7a31d2..3d06eeb 100644
@@ -1343,7 +1343,7 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
 {
 	unsigned int offset = skb_headlen(skb);
 	skb_frag_t frags[MAX_SKB_FRAGS];
-	int i;
+	int i, nr_frags;
 	struct ubuf_info *uarg;
 	struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
 
@@ -1357,17 +1357,16 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
 	skb->data_len += nskb->len;
 
 	/* create a brand new frags array and coalesce there */
-	for (i = 0; offset < skb->len; i++) {
+	for (nr_frags = 0; offset < skb->len; nr_frags++) {
 		struct page *page;
 		unsigned int len;
 
-		BUG_ON(i >= MAX_SKB_FRAGS);
+		BUG_ON(nr_frags >= MAX_SKB_FRAGS);
 		page = alloc_page(GFP_ATOMIC);
 		if (!page) {
-			int j;
 			skb->truesize += skb->data_len;
-			for (j = 0; j < i; j++)
-				put_page(frags[j].page.p);
+			for (i = 0; i < nr_frags; i++)
+				put_page(frags[i].page.p);
 			return -ENOMEM;
 		}
 
@@ -1379,27 +1378,29 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s
 			BUG();
 
 		offset += len;
-		frags[i].page.p = page;
-		frags[i].page_offset = 0;
-		skb_frag_size_set(&frags[i], len);
+		frags[nr_frags].page.p = page;
+		frags[nr_frags].page_offset = 0;
+		skb_frag_size_set(&frags[nr_frags], len);
 	}
-	/* swap out with old one */
-	memcpy(skb_shinfo(skb)->frags,
-	       frags,
-	       i * sizeof(skb_frag_t));
-	skb_shinfo(skb)->nr_frags = i;
-	skb->truesize += i * PAGE_SIZE;
-
-	/* remove traces of mapped pages and frag_list */
+
+	/* Copied all the bits from the frag list -- free it. */
 	skb_frag_list_init(skb);
-	uarg = skb_shinfo(skb)->destructor_arg;
-	/* increase inflight counter to offset decrement in callback */
+	xenvif_skb_zerocopy_prepare(queue, nskb);
+	kfree_skb(nskb);
+
+	/* Release all the original (foreign) frags. */
+	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
+		skb_frag_unref(skb, i);
 	atomic_inc(&queue->inflight_packets);
+	uarg = skb_shinfo(skb)->destructor_arg;
 	uarg->callback(uarg, true);
 	skb_shinfo(skb)->destructor_arg = NULL;
 
-	xenvif_skb_zerocopy_prepare(queue, nskb);
-	kfree_skb(nskb);
+	/* Fill the skb with the new (local) frags. */
+	memcpy(skb_shinfo(skb)->frags, frags,
+	       nr_frags * sizeof(skb_frag_t));
+	skb_shinfo(skb)->nr_frags = nr_frags;
+	skb->truesize += nr_frags * PAGE_SIZE;
 
 	return 0;
 }
Signature · 1 line
1.7.10.4
maintainer
#15 lore raw
In reply to David Vrabel

From: David Vrabel <[email protected]>
Date: Tue, 3 Mar 2015 16:26:09 +0000

Quoted 3 lines

A couple of bug fixes for netback:
- make ethool stats to report the correct values.
- don't leak 1 MiB every time a VIF is destroyed.

Please fix up the minor commit message issues in patch #2 and resubmit this series, thanks.

maintainer
#16 lore raw
In reply to David Vrabel

From: David Vrabel <[email protected]>
Date: Tue, 3 Mar 2015 16:26:09 +0000

Quoted 3 lines

A couple of bug fixes for netback:
- make ethool stats to report the correct values.
- don't leak 1 MiB every time a VIF is destroyed.

Please fix up the minor commit message issues in patch #2 and resubmit this series, thanks.