Skip to contentSkip to conversation

net: consolidate netif_needs_gso() checks

PATCH In review Jeff Kirsher 4 participants 10 messages Feb 27, 2010 – Mar 8, 2010

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

PATCH v2 1/1 net: consolidate netif_needs_gso() checks

From: John Fastabend <[email protected]>

netif_needs_gso() is checked twice in the TX path once, before submitting the skb to the qdisc and once after it is dequeued from the qdisc just before calling ndo_hard_start(). This opens a window for a user to change the gso/tso or tx checksum settings that can cause netif_needs_gso to be true in one check and false in the other.

Specifically, changing TX checksum setting may cause the warning in skb_gso_segment() to be triggered if the checksum is calculated earlier.

This consolidates the netif_needs_gso() calls so that the stack only checks if gso is needed in dev_hard_start_xmit().

---

 net/core/dev.c |   50 +++++++++++++++++++++++++++++---------------------
 1 files changed, 29 insertions(+), 21 deletions(-)
net/core/dev.c +29 -21
index eb7f1a4..626124d 100644
@@ -1835,12 +1835,40 @@ int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
 {
 	const struct net_device_ops *ops = dev->netdev_ops;
 	int rc = NETDEV_TX_OK;
+	int need_gso = netif_needs_gso(dev, skb);
+
+	if (!need_gso) {
+		if (skb_has_frags(skb) &&
+		    !(dev->features & NETIF_F_FRAGLIST) &&
+		    __skb_linearize(skb))
+			goto out_kfree_skb;
+
+		/* Fragmented skb is linearized if device does not support SG,
+		 * or if at least one of fragments is in highmem and device
+		 * does not support DMA from it.
+		 */
+		if (skb_shinfo(skb)->nr_frags &&
+		    (!(dev->features & NETIF_F_SG) ||
+		      illegal_highdma(dev, skb)) &&
+		    __skb_linearize(skb))
+			goto out_kfree_skb;
+		/* If packet is not checksummed and device does not support
+		 * checksumming for this protocol, complete checksumming here.
+		 */
+		if (skb->ip_summed == CHECKSUM_PARTIAL) {
+			skb_set_transport_header(skb, skb->csum_start -
+				      skb_headroom(skb));
+			if (!dev_can_checksum(dev, skb) &&
+			     skb_checksum_help(skb))
+				goto out_kfree_skb;
+		}
+	}
 
 	if (likely(!skb->next)) {
 		if (!list_empty(&ptype_all))
 			dev_queue_xmit_nit(skb, dev);
 
-		if (netif_needs_gso(dev, skb)) {
+		if (need_gso) {
 			if (unlikely(dev_gso_segment(skb)))
 				goto out_kfree_skb;
 			if (skb->next)
@@ -2056,25 +2084,6 @@ int dev_queue_xmit(struct sk_buff *skb)
 	struct Qdisc *q;
 	int rc = -ENOMEM;
 
-	/* GSO will handle the following emulations directly. */
-	if (netif_needs_gso(dev, skb))
-		goto gso;
-
-	/* Convert a paged skb to linear, if required */
-	if (skb_needs_linearize(skb, dev) && __skb_linearize(skb))
-		goto out_kfree_skb;
-
-	/* If packet is not checksummed and device does not support
-	 * checksumming for this protocol, complete checksumming here.
-	 */
-	if (skb->ip_summed == CHECKSUM_PARTIAL) {
-		skb_set_transport_header(skb, skb->csum_start -
-					      skb_headroom(skb));
-		if (!dev_can_checksum(dev, skb) && skb_checksum_help(skb))
-			goto out_kfree_skb;
-	}
-
-gso:
 	/* Disable soft irqs for various locks below. Also
 	 * stops preemption for RCU.
 	 */
@@ -2133,7 +2142,6 @@ gso:
 	rc = -ENETDOWN;
 	rcu_read_unlock_bh();
 
-out_kfree_skb:
 	kfree_skb(skb);
 	return rc;
 out:
maintainer
#1 lore raw

From: Jeff Kirsher <[email protected]>
Date: Fri, 26 Feb 2010 16:20:11 -0800

Quoted 14 lines

From: John Fastabend <[email protected]>

netif_needs_gso() is checked twice in the TX path once, before submitting the skb to the qdisc and once after it is dequeued from the qdisc just before calling ndo_hard_start(). This opens a window for a user to change the gso/tso or tx checksum settings that can cause netif_needs_gso to be true in one check and false in the other.

Specifically, changing TX checksum setting may cause the warning in skb_gso_segment() to be triggered if the checksum is calculated earlier.

Quoted 6 lines

This consolidates the netif_needs_gso() calls so that the stack only checks if gso is needed in dev_hard_start_xmit().

This looks mostly fine, but I have at least one doubt.

If we have ip_summed == CHECKSUM_PARTIAL might some classifier or packet scheduler action module require that the transport header is setup properly before the SKB gets into there?

Arguably, that's happening already in the GSO case but this change is bringing the issue more to light.

Herbert, could you also take a look at this patch?

Thanks!

Quoted 81 lines
net/core/dev.c +29 -21
index eb7f1a4..626124d 100644
@@ -1835,12 +1835,40 @@ int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
 {
 	const struct net_device_ops *ops = dev->netdev_ops;
 	int rc = NETDEV_TX_OK;
+	int need_gso = netif_needs_gso(dev, skb);
+
+	if (!need_gso) {
+		if (skb_has_frags(skb) &&
+		    !(dev->features & NETIF_F_FRAGLIST) &&
+		    __skb_linearize(skb))
+			goto out_kfree_skb;
+
+		/* Fragmented skb is linearized if device does not support SG,
+		 * or if at least one of fragments is in highmem and device
+		 * does not support DMA from it.
+		 */
+		if (skb_shinfo(skb)->nr_frags &&
+		    (!(dev->features & NETIF_F_SG) ||
+		      illegal_highdma(dev, skb)) &&
+		    __skb_linearize(skb))
+			goto out_kfree_skb;
+		/* If packet is not checksummed and device does not support
+		 * checksumming for this protocol, complete checksumming here.
+		 */
+		if (skb->ip_summed == CHECKSUM_PARTIAL) {
+			skb_set_transport_header(skb, skb->csum_start -
+				      skb_headroom(skb));
+			if (!dev_can_checksum(dev, skb) &&
+			     skb_checksum_help(skb))
+				goto out_kfree_skb;
+		}
+	}
 
 	if (likely(!skb->next)) {
 		if (!list_empty(&ptype_all))
 			dev_queue_xmit_nit(skb, dev);
 
-		if (netif_needs_gso(dev, skb)) {
+		if (need_gso) {
 			if (unlikely(dev_gso_segment(skb)))
 				goto out_kfree_skb;
 			if (skb->next)
@@ -2056,25 +2084,6 @@ int dev_queue_xmit(struct sk_buff *skb)
 	struct Qdisc *q;
 	int rc = -ENOMEM;
 
-	/* GSO will handle the following emulations directly. */
-	if (netif_needs_gso(dev, skb))
-		goto gso;
-
-	/* Convert a paged skb to linear, if required */
-	if (skb_needs_linearize(skb, dev) && __skb_linearize(skb))
-		goto out_kfree_skb;
-
-	/* If packet is not checksummed and device does not support
-	 * checksumming for this protocol, complete checksumming here.
-	 */
-	if (skb->ip_summed == CHECKSUM_PARTIAL) {
-		skb_set_transport_header(skb, skb->csum_start -
-					      skb_headroom(skb));
-		if (!dev_can_checksum(dev, skb) && skb_checksum_help(skb))
-			goto out_kfree_skb;
-	}
-
-gso:
 	/* Disable soft irqs for various locks below. Also
 	 * stops preemption for RCU.
 	 */
@@ -2133,7 +2142,6 @@ gso:
 	rc = -ENETDOWN;
 	rcu_read_unlock_bh();
 
-out_kfree_skb:
 	kfree_skb(skb);
 	return rc;
 out:
#2 lore raw
Quoted 5 lines · David Miller

If we have ip_summed == CHECKSUM_PARTIAL might some classifier or packet scheduler action module require that the transport header is setup properly before the SKB gets into there?

I think this is OK as the transport header setting was only there for backwards compatibility with certain drivers that relied on it. Its use was very much isolated.

I just did a grep on net/sched and couldn't see anything obvious that uses transport_header.

Quoted 17 lines
Quoted 17 lines
net/core/dev.c +9 -0
index eb7f1a4..626124d 100644
@@ -1835,12 +1835,40 @@ int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
 {
 	const struct net_device_ops *ops = dev->netdev_ops;
 	int rc = NETDEV_TX_OK;
+	int need_gso = netif_needs_gso(dev, skb);
+
+	if (!need_gso) {
+		if (skb_has_frags(skb) &&
+		    !(dev->features & NETIF_F_FRAGLIST) &&
+		    __skb_linearize(skb))
+			goto out_kfree_skb;
+
+		/* Fragmented skb is linearized if device does not support SG,
Quoted 8 lines

net/core/dev.c int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev, · line 1854

Quoted 8 lines
net/core/dev.c +8 -0
+		 * or if at least one of fragments is in highmem and device
+		 * does not support DMA from it.
+		 */
+		if (skb_shinfo(skb)->nr_frags &&
+		    (!(dev->features & NETIF_F_SG) ||
+		      illegal_highdma(dev, skb)) &&
+		    __skb_linearize(skb))
+			goto out_kfree_skb;

Please use skb_needs_linearize.

Quoted 16 lines
Quoted 16 lines
net/core/dev.c +11 -0
+		/* If packet is not checksummed and device does not support
+		 * checksumming for this protocol, complete checksumming here.
+		 */
+		if (skb->ip_summed == CHECKSUM_PARTIAL) {
+			skb_set_transport_header(skb, skb->csum_start -
+				      skb_headroom(skb));
+			if (!dev_can_checksum(dev, skb) &&
+			     skb_checksum_help(skb))
+				goto out_kfree_skb;
+		}
+	}
 
 	if (likely(!skb->next)) {
 		if (!list_empty(&ptype_all))
 			dev_queue_xmit_nit(skb, dev);
 
Quoted 5 lines

net/core/dev.c int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev, · line 1874

Quoted 5 lines
net/core/dev.c +1 -1
-		if (netif_needs_gso(dev, skb)) {
+		if (need_gso) {
 			if (unlikely(dev_gso_segment(skb)))
 				goto out_kfree_skb;
 			if (skb->next)

That whole if block should be moved into an else clause here:

		if (netif_needs_gso(dev, skb)) {
			if (unlikely(dev_gso_segment(skb)))
				goto out_kfree_skb;
			if (skb->next)
				goto gso;
		} else {
			do your thing
		}

The reason is that the other paths only act on the fragments generated by GSO, so logically with your change we shouldn't apply any further software emulation to them, even if the device setting changed.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer
#3 lore raw

From: Herbert Xu <[email protected]>
Date: Sat, 27 Feb 2010 23:52:45 +0800

Quoted 2 lines

I just did a grep on net/sched and couldn't see anything obvious that uses transport_header.

I think skb_checksum_help() would be such a use and I see a reference in net/sched/sch_netem.c

#4 lore raw
Quoted 8 lines · David Miller

From: Herbert Xu <[email protected]>
Date: Sat, 27 Feb 2010 23:52:45 +0800

Quoted 2 lines

I just did a grep on net/sched and couldn't see anything obvious that uses transport_header.

I think skb_checksum_help() would be such a use and I see a reference in net/sched/sch_netem.c

AFAICS skb_checksum_help uses csum_start and not transport_header. Has this changed recently?

Once upon a time some drivers used transport_header instead of csum_start, but even those seem to be gone mostly so one day we could remove this setting completely.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer
#5 lore raw

From: Herbert Xu <[email protected]>
Date: Sun, 28 Feb 2010 08:29:53 +0800

Quoted 10 lines · David Miller
Quoted 8 lines

From: Herbert Xu <[email protected]>
Date: Sat, 27 Feb 2010 23:52:45 +0800

Quoted 2 lines

I just did a grep on net/sched and couldn't see anything obvious that uses transport_header.

I think skb_checksum_help() would be such a use and I see a reference in net/sched/sch_netem.c

Quoted 6 lines

AFAICS skb_checksum_help uses csum_start and not transport_header. Has this changed recently?

Once upon a time some drivers used transport_header instead of csum_start, but even those seem to be gone mostly so one day we could remove this setting completely.

Ok, then this clears up all of my concerns. Thanks Herbert.

#7 lore raw
In reply to Herbert Xu
Quoted 80 lines · Herbert Xu
Quoted 5 lines · David Miller

If we have ip_summed == CHECKSUM_PARTIAL might some classifier or packet scheduler action module require that the transport header is setup properly before the SKB gets into there?

I think this is OK as the transport header setting was only there for backwards compatibility with certain drivers that relied on it. Its use was very much isolated.

I just did a grep on net/sched and couldn't see anything obvious that uses transport_header.

Quoted 18 lines
Quoted 18 lines
net/core/dev.c +10 -0
index eb7f1a4..626124d 100644
@@ -1835,12 +1835,40 @@ int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
 {
 	const struct net_device_ops *ops = dev->netdev_ops;
 	int rc = NETDEV_TX_OK;
+	int need_gso = netif_needs_gso(dev, skb);
+
+	if (!need_gso) {
+		if (skb_has_frags(skb) &&
+		    !(dev->features & NETIF_F_FRAGLIST) &&
+		    __skb_linearize(skb))
+			goto out_kfree_skb;
+
+		/* Fragmented skb is linearized if device does not support SG,
+		 * or if at least one of fragments is in highmem and device
Quoted 8 lines

net/core/dev.c int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev, · line 1866

Quoted 8 lines
net/core/dev.c +7 -0
+		 * does not support DMA from it.
+		 */
+		if (skb_shinfo(skb)->nr_frags &&
+		    (!(dev->features & NETIF_F_SG) ||
+		      illegal_highdma(dev, skb)) &&
+		    __skb_linearize(skb))
+			goto out_kfree_skb;
 

Please use skb_needs_linearize.

Quoted 16 lines
Quoted 16 lines
net/core/dev.c +11 -0
+		/* If packet is not checksummed and device does not support
+		 * checksumming for this protocol, complete checksumming here.
+		 */
+		if (skb->ip_summed == CHECKSUM_PARTIAL) {
+			skb_set_transport_header(skb, skb->csum_start -
+				      skb_headroom(skb));
+			if (!dev_can_checksum(dev, skb) &&
+			     skb_checksum_help(skb))
+				goto out_kfree_skb;
+		}
+	}
 
 	if (likely(!skb->next)) {
 		if (!list_empty(&ptype_all))
 			dev_queue_xmit_nit(skb, dev);
 
Quoted 6 lines
Quoted 6 lines
net/core/dev.c +1 -1
-		if (netif_needs_gso(dev, skb)) {
+		if (need_gso) {
 			if (unlikely(dev_gso_segment(skb)))
 				goto out_kfree_skb;

if (skb->next)

That whole if block should be moved into an else clause here:

		if (netif_needs_gso(dev, skb)) {
			if (unlikely(dev_gso_segment(skb)))
				goto out_kfree_skb;
			if (skb->next)
				goto gso;
		} else {
			do your thing
		}
Quoted 7 lines

The reason is that the other paths only act on the fragments generated by GSO, so logically with your change we shouldn't apply any further software emulation to them, even if the device setting changed.

Cheers,

Herbert,

It looks like dev_gso_segment() could be used to "Verify header integrity only" according to the comment? If this is true I think the logic should probably be

		if (netif_needs_gso(dev, skb)) {
			if (unlikely(dev_gso_segment(skb)))
				goto out_kfree_skb;
			if (skb->next)
				goto gso;
		} 
		do your thing

That way we linearize the skb if necessary in the case were dev_gso_segment() only verifies the header and does not return a list of segments.

thanks,
John.

#8 lore raw
Quoted 14 lines · John Fastabend

It looks like dev_gso_segment() could be used to "Verify header integrity only" according to the comment? If this is true I think the logic should probably be

		if (netif_needs_gso(dev, skb)) {
			if (unlikely(dev_gso_segment(skb)))
				goto out_kfree_skb;
			if (skb->next)
				goto gso;
		} 		do your thing
Quoted 3 lines

That way we linearize the skb if necessary in the case were dev_gso_segment() only verifies the header and does not return a list of segments.

If we needed to linearise the skb then dev_gso_segment should perform the segmentation. Is there a case where it doesn't?

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#9 lore raw
Quoted 13 lines · Herbert Xu
Quoted 11 lines · John Fastabend

It looks like dev_gso_segment() could be used to "Verify header integrity only" according to the comment? If this is true I think the logic should probably be

		if (netif_needs_gso(dev, skb)) {
			if (unlikely(dev_gso_segment(skb)))
				goto out_kfree_skb;
			if (skb->next)
				goto gso;
		} 		do your thing
Quoted 7 lines
Quoted 2 lines

That way we linearize the skb if necessary in the case were dev_gso_segment() only verifies the header and does not return a list of segments.

If we needed to linearise the skb then dev_gso_segment should perform the segmentation. Is there a case where it doesn't?

Cheers,

Nope as far as I can tell all cases are covered my concerns were unfounded. Thanks for the review, I'll get this updated and resent.

-John.