* [PATCH] net: af_packet: Don't initialize vnet_hdr @ 2011-05-12 20:36 Joe Perches 2011-05-12 21:26 ` David Miller 0 siblings, 1 reply; 8+ messages in thread From: Joe Perches @ 2011-05-12 20:36 UTC (permalink / raw) To: linux-kernel; +Cc: David S. Miller, netdev Save a memset, initialize only the portion necessary. packet_snd either gets this structure completely from memcpy_fromiovec or uses only the hdr_len set to 0, so don't always initialize the structure to 0. Signed-off-by: Joe Perches <joe@perches.com> --- net/packet/af_packet.c | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-) diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c index 549527b..ac88df9 100644 --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -1123,7 +1123,7 @@ static int packet_snd(struct socket *sock, __be16 proto; unsigned char *addr; int ifindex, err, reserve = 0; - struct virtio_net_hdr vnet_hdr = { 0 }; + struct virtio_net_hdr vnet_hdr; int offset = 0; int vnet_hdr_len; struct packet_sock *po = pkt_sk(sk); @@ -1206,7 +1206,9 @@ static int packet_snd(struct socket *sock, goto out_unlock; } - } + } else + vnet_hdr.hdr_len = 0; + err = -EMSGSIZE; if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN)) -- 1.7.5.rc3.dirty ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH] net: af_packet: Don't initialize vnet_hdr 2011-05-12 20:36 [PATCH] net: af_packet: Don't initialize vnet_hdr Joe Perches @ 2011-05-12 21:26 ` David Miller 2011-05-12 21:33 ` Joe Perches 0 siblings, 1 reply; 8+ messages in thread From: David Miller @ 2011-05-12 21:26 UTC (permalink / raw) To: joe; +Cc: linux-kernel, netdev From: Joe Perches <joe@perches.com> Date: Thu, 12 May 2011 13:36:10 -0700 > Save a memset, initialize only the portion necessary. > > packet_snd either gets this structure completely from > memcpy_fromiovec or uses only the hdr_len set to 0, > so don't always initialize the structure to 0. > > Signed-off-by: Joe Perches <joe@perches.com> On ARM this won't be tightly packed, therefore you'll leave uninitialized pieces of padding in this structure and this thing is an "on-the-wire" network header. I'm not applying this. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] net: af_packet: Don't initialize vnet_hdr 2011-05-12 21:26 ` David Miller @ 2011-05-12 21:33 ` Joe Perches 2011-05-12 21:36 ` David Miller 0 siblings, 1 reply; 8+ messages in thread From: Joe Perches @ 2011-05-12 21:33 UTC (permalink / raw) To: David Miller; +Cc: linux-kernel, netdev On Thu, 2011-05-12 at 17:26 -0400, David Miller wrote: > From: Joe Perches <joe@perches.com> > Date: Thu, 12 May 2011 13:36:10 -0700 > > > Save a memset, initialize only the portion necessary. > > > > packet_snd either gets this structure completely from > > memcpy_fromiovec or uses only the hdr_len set to 0, > > so don't always initialize the structure to 0. > > > > Signed-off-by: Joe Perches <joe@perches.com> > > On ARM this won't be tightly packed, therefore you'll leave > uninitialized pieces of padding in this structure and this > thing is an "on-the-wire" network header. > > I'm not applying this. I believe that it's only sent when po->has_vnet_hdr is set. In that case, it's completely filled from memcpy_fromiovec. In the not set po->has_vnet_hdr case, only vnet_hdr.hdr_len is accessed by packet_alloc_skb. cheers, Joe ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] net: af_packet: Don't initialize vnet_hdr 2011-05-12 21:33 ` Joe Perches @ 2011-05-12 21:36 ` David Miller 2011-05-12 21:55 ` Joe Perches 0 siblings, 1 reply; 8+ messages in thread From: David Miller @ 2011-05-12 21:36 UTC (permalink / raw) To: joe; +Cc: linux-kernel, netdev From: Joe Perches <joe@perches.com> Date: Thu, 12 May 2011 14:33:20 -0700 > On Thu, 2011-05-12 at 17:26 -0400, David Miller wrote: >> From: Joe Perches <joe@perches.com> >> Date: Thu, 12 May 2011 13:36:10 -0700 >> >> > Save a memset, initialize only the portion necessary. >> > >> > packet_snd either gets this structure completely from >> > memcpy_fromiovec or uses only the hdr_len set to 0, >> > so don't always initialize the structure to 0. >> > >> > Signed-off-by: Joe Perches <joe@perches.com> >> >> On ARM this won't be tightly packed, therefore you'll leave >> uninitialized pieces of padding in this structure and this >> thing is an "on-the-wire" network header. >> >> I'm not applying this. > > I believe that it's only sent when po->has_vnet_hdr > is set. In that case, it's completely filled from > memcpy_fromiovec. In the not set po->has_vnet_hdr case, > only vnet_hdr.hdr_len is accessed by packet_alloc_skb. I think with the way this code is protecting accesses to vnet_hdr, it's going to start warning that some parts "may" be use uninitialized. It's also slightly ugly to partially initialize these on-stack things. I would rather see the code rearranged such that this sort of hackish scheme isn't necessary. Again, I'm not applying this. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] net: af_packet: Don't initialize vnet_hdr 2011-05-12 21:36 ` David Miller @ 2011-05-12 21:55 ` Joe Perches 2011-05-12 21:59 ` David Miller 0 siblings, 1 reply; 8+ messages in thread From: Joe Perches @ 2011-05-12 21:55 UTC (permalink / raw) To: David Miller; +Cc: linux-kernel, netdev Save an initialization because when this structure is used it's completely filled by memcpy_fromiovec. Add a new variable used for the 0 sized allocation when this structure is not used. Signed-off-by: Joe Perches <joe@perches.com> --- On Thu, 2011-05-12 at 17:36 -0400, David Miller wrote: > I would rather see the code rearranged such that this sort of > hackish scheme isn't necessary. net/packet/af_packet.c | 10 +++++++--- 1 files changed, 7 insertions(+), 3 deletions(-) diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c index 549527b..cc1e83d 100644 --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -1123,9 +1123,10 @@ static int packet_snd(struct socket *sock, __be16 proto; unsigned char *addr; int ifindex, err, reserve = 0; - struct virtio_net_hdr vnet_hdr = { 0 }; + struct virtio_net_hdr vnet_hdr; int offset = 0; int vnet_hdr_len; + size_t alloc_vnet_hdr_len; struct packet_sock *po = pkt_sk(sk); unsigned short gso_type = 0; @@ -1206,7 +1207,10 @@ static int packet_snd(struct socket *sock, goto out_unlock; } - } + alloc_vnet_hdr_len = vnet_hdr.hdr_len; + } else + alloc_vnet_hdr_len = 0; + err = -EMSGSIZE; if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN)) @@ -1214,7 +1218,7 @@ static int packet_snd(struct socket *sock, err = -ENOBUFS; skb = packet_alloc_skb(sk, LL_ALLOCATED_SPACE(dev), - LL_RESERVED_SPACE(dev), len, vnet_hdr.hdr_len, + LL_RESERVED_SPACE(dev), len, alloc_vnet_hdr_len, msg->msg_flags & MSG_DONTWAIT, &err); if (skb == NULL) goto out_unlock; ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH] net: af_packet: Don't initialize vnet_hdr 2011-05-12 21:55 ` Joe Perches @ 2011-05-12 21:59 ` David Miller 2011-05-12 23:25 ` [PATCH net-next V3] net: af_packet: Untangle packet_snd by adding vpacket_snd Joe Perches 0 siblings, 1 reply; 8+ messages in thread From: David Miller @ 2011-05-12 21:59 UTC (permalink / raw) To: joe; +Cc: linux-kernel, netdev From: Joe Perches <joe@perches.com> Date: Thu, 12 May 2011 14:55:48 -0700 > Save an initialization because when this structure > is used it's completely filled by memcpy_fromiovec. > > Add a new variable used for the 0 sized allocation > when this structure is not used. > > Signed-off-by: Joe Perches <joe@perches.com> > > --- > > On Thu, 2011-05-12 at 17:36 -0400, David Miller wrote: >> I would rather see the code rearranged such that this sort of >> hackish scheme isn't necessary. You misunderstood me. It's this: struct virtio_net_hdr vnet_hdr; if (po->has_vnet_hdr) { initialize &vnet_hdr } ... if (po->has_vnet_hdr) { use vnet_hdr } which I'm talking about when I say "hackish scheme". The compiler cannot conclusively see that the control flow always goes to the code that initialized vnet_hdr every time it reaches the code that uses it. I want _that_ part rearranged, not what you decided to tackle here. For the third time, I'm not applying your patch. ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH net-next V3] net: af_packet: Untangle packet_snd by adding vpacket_snd 2011-05-12 21:59 ` David Miller @ 2011-05-12 23:25 ` Joe Perches 2011-05-13 5:11 ` Eric Dumazet 0 siblings, 1 reply; 8+ messages in thread From: Joe Perches @ 2011-05-12 23:25 UTC (permalink / raw) To: linux-kernel; +Cc: David S. Miller, netdev The current packet_snd handles both vlan and non-vlan frames with initialization of a struct virtio_net_hdr that is unused by non-vlan frames. Create a new vpacket_snd that uses this virtio_net_hdr and remove it and the vlan and gso logic from packet_snd. Signed-off-by: Joe Perches <joe@perches.com> --- net/packet/af_packet.c | 250 ++++++++++++++++++++++++++++++++---------------- 1 files changed, 169 insertions(+), 81 deletions(-) diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c index 549527b..d4bcf51 100644 --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -1113,42 +1113,38 @@ static inline struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad, return skb; } -static int packet_snd(struct socket *sock, - struct msghdr *msg, size_t len) +static int vpacket_snd(struct socket *sock, struct msghdr *msg, size_t len) { struct sock *sk = sock->sk; - struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name; struct sk_buff *skb; struct net_device *dev; __be16 proto; unsigned char *addr; int ifindex, err, reserve = 0; - struct virtio_net_hdr vnet_hdr = { 0 }; - int offset = 0; + struct virtio_net_hdr vnet_hdr; + int offset; int vnet_hdr_len; struct packet_sock *po = pkt_sk(sk); unsigned short gso_type = 0; + struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name; - /* - * Get and verify the address. - */ - - if (saddr == NULL) { - ifindex = po->ifindex; - proto = po->num; - addr = NULL; + /* Get and verify the address */ + if (!saddr) { + ifindex = po->ifindex; + proto = po->num; + addr = NULL; } else { err = -EINVAL; if (msg->msg_namelen < sizeof(struct sockaddr_ll)) goto out; - if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr))) + if (msg->msg_namelen < (saddr->sll_halen + + offsetof(struct sockaddr_ll, sll_addr))) goto out; - ifindex = saddr->sll_ifindex; - proto = saddr->sll_protocol; - addr = saddr->sll_addr; + ifindex = saddr->sll_ifindex; + proto = saddr->sll_protocol; + addr = saddr->sll_addr; } - dev = dev_get_by_index(sock_net(sk), ifindex); err = -ENXIO; if (dev == NULL) @@ -1160,52 +1156,47 @@ static int packet_snd(struct socket *sock, if (!(dev->flags & IFF_UP)) goto out_unlock; - if (po->has_vnet_hdr) { - vnet_hdr_len = sizeof(vnet_hdr); + vnet_hdr_len = sizeof(vnet_hdr); - err = -EINVAL; - if (len < vnet_hdr_len) - goto out_unlock; - - len -= vnet_hdr_len; + err = -EINVAL; + if (len < vnet_hdr_len) + goto out_unlock; - err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov, - vnet_hdr_len); - if (err < 0) - goto out_unlock; + len -= vnet_hdr_len; - if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && - (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 > - vnet_hdr.hdr_len)) - vnet_hdr.hdr_len = vnet_hdr.csum_start + - vnet_hdr.csum_offset + 2; + err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov, vnet_hdr_len); + if (err < 0) + goto out_unlock; - err = -EINVAL; - if (vnet_hdr.hdr_len > len) - goto out_unlock; + if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && + (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 > vnet_hdr.hdr_len)) + vnet_hdr.hdr_len = (vnet_hdr.csum_start + + vnet_hdr.csum_offset + 2); - if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) { - switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { - case VIRTIO_NET_HDR_GSO_TCPV4: - gso_type = SKB_GSO_TCPV4; - break; - case VIRTIO_NET_HDR_GSO_TCPV6: - gso_type = SKB_GSO_TCPV6; - break; - case VIRTIO_NET_HDR_GSO_UDP: - gso_type = SKB_GSO_UDP; - break; - default: - goto out_unlock; - } + err = -EINVAL; + if (vnet_hdr.hdr_len > len) + goto out_unlock; - if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN) - gso_type |= SKB_GSO_TCP_ECN; + if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) { + switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { + case VIRTIO_NET_HDR_GSO_TCPV4: + gso_type = SKB_GSO_TCPV4; + break; + case VIRTIO_NET_HDR_GSO_TCPV6: + gso_type = SKB_GSO_TCPV6; + break; + case VIRTIO_NET_HDR_GSO_UDP: + gso_type = SKB_GSO_UDP; + break; + default: + goto out_unlock; + } - if (vnet_hdr.gso_size == 0) - goto out_unlock; + if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN) + gso_type |= SKB_GSO_TCP_ECN; - } + if (vnet_hdr.gso_size == 0) + goto out_unlock; } err = -EMSGSIZE; @@ -1216,15 +1207,19 @@ static int packet_snd(struct socket *sock, skb = packet_alloc_skb(sk, LL_ALLOCATED_SPACE(dev), LL_RESERVED_SPACE(dev), len, vnet_hdr.hdr_len, msg->msg_flags & MSG_DONTWAIT, &err); - if (skb == NULL) + if (!skb) goto out_unlock; skb_set_network_header(skb, reserve); err = -EINVAL; - if (sock->type == SOCK_DGRAM && - (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0) - goto out_free; + if (sock->type == SOCK_DGRAM) { + offset = dev_hard_header(skb, dev, ntohs(proto), addr, + NULL, len); + if (offset < 0) + goto out_free; + } else + offset = 0; /* Returns -EFAULT on error */ err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len); @@ -1253,33 +1248,123 @@ static int packet_snd(struct socket *sock, skb->priority = sk->sk_priority; skb->mark = sk->sk_mark; - if (po->has_vnet_hdr) { - if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { - if (!skb_partial_csum_set(skb, vnet_hdr.csum_start, - vnet_hdr.csum_offset)) { - err = -EINVAL; - goto out_free; - } + if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { + if (!skb_partial_csum_set(skb, vnet_hdr.csum_start, + vnet_hdr.csum_offset)) { + err = -EINVAL; + goto out_free; } + } + + skb_shinfo(skb)->gso_size = vnet_hdr.gso_size; + skb_shinfo(skb)->gso_type = gso_type; - skb_shinfo(skb)->gso_size = vnet_hdr.gso_size; - skb_shinfo(skb)->gso_type = gso_type; + /* Header must be checked, and gso_segs computed */ + skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; + skb_shinfo(skb)->gso_segs = 0; - /* Header must be checked, and gso_segs computed. */ - skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; - skb_shinfo(skb)->gso_segs = 0; + len += vnet_hdr_len; - len += vnet_hdr_len; + /* Now send it */ + err = dev_queue_xmit(skb); + if (err > 0) { + err = net_xmit_errno(err); + if (err) + goto out_unlock; } - /* - * Now send it - */ + dev_put(dev); - err = dev_queue_xmit(skb); - if (err > 0 && (err = net_xmit_errno(err)) != 0) + return len; + +out_free: + kfree_skb(skb); +out_unlock: + if (dev) + dev_put(dev); +out: + return err; +} + +static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len) +{ + struct sock *sk = sock->sk; + struct sk_buff *skb; + struct net_device *dev; + __be16 proto; + unsigned char *addr; + int ifindex, err, reserve = 0; + int offset; + struct packet_sock *po = pkt_sk(sk); + struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name; + + /* Get and verify the address */ + if (!saddr) { + ifindex = po->ifindex; + proto = po->num; + addr = NULL; + } else { + err = -EINVAL; + if (msg->msg_namelen < sizeof(struct sockaddr_ll)) + goto out; + if (msg->msg_namelen < (saddr->sll_halen + + offsetof(struct sockaddr_ll, sll_addr))) + goto out; + ifindex = saddr->sll_ifindex; + proto = saddr->sll_protocol; + addr = saddr->sll_addr; + } + + dev = dev_get_by_index(sock_net(sk), ifindex); + err = -ENXIO; + if (dev == NULL) + goto out_unlock; + if (sock->type == SOCK_RAW) + reserve = dev->hard_header_len; + + err = -ENETDOWN; + if (!(dev->flags & IFF_UP)) + goto out_unlock; + + err = -ENOBUFS; + skb = packet_alloc_skb(sk, LL_ALLOCATED_SPACE(dev), + LL_RESERVED_SPACE(dev), len, 0, + msg->msg_flags & MSG_DONTWAIT, &err); + if (!skb) goto out_unlock; + skb_set_network_header(skb, reserve); + + err = -EINVAL; + if (sock->type == SOCK_DGRAM) { + offset = dev_hard_header(skb, dev, ntohs(proto), addr, + NULL, len); + if (offset < 0) + goto out_free; + } else + offset = 0; + + /* Returns -EFAULT on error */ + err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len); + if (err) + goto out_free; + err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags); + if (err < 0) + goto out_free; + + skb->protocol = proto; + skb->dev = dev; + skb->priority = sk->sk_priority; + skb->mark = sk->sk_mark; + + /* Now send it */ + err = dev_queue_xmit(skb); + if (err > 0) { + err = net_xmit_errno(err); + if (err) + goto out_unlock; + } + dev_put(dev); return len; @@ -1294,14 +1379,17 @@ out: } static int packet_sendmsg(struct kiocb *iocb, struct socket *sock, - struct msghdr *msg, size_t len) + struct msghdr *msg, size_t len) { struct sock *sk = sock->sk; struct packet_sock *po = pkt_sk(sk); + if (po->tx_ring.pg_vec) return tpacket_snd(po, msg); - else - return packet_snd(sock, msg, len); + else if (po->has_vnet_hdr) + return vpacket_snd(sock, msg, len); + + return packet_snd(sock, msg, len); } /* -- 1.7.5.rc3.dirty ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH net-next V3] net: af_packet: Untangle packet_snd by adding vpacket_snd 2011-05-12 23:25 ` [PATCH net-next V3] net: af_packet: Untangle packet_snd by adding vpacket_snd Joe Perches @ 2011-05-13 5:11 ` Eric Dumazet 0 siblings, 0 replies; 8+ messages in thread From: Eric Dumazet @ 2011-05-13 5:11 UTC (permalink / raw) To: Joe Perches; +Cc: linux-kernel, David S. Miller, netdev Le jeudi 12 mai 2011 à 16:25 -0700, Joe Perches a écrit : > The current packet_snd handles both vlan and non-vlan frames > with initialization of a struct virtio_net_hdr that is unused > by non-vlan frames. > > Create a new vpacket_snd that uses this virtio_net_hdr and > remove it and the vlan and gso logic from packet_snd. > 1) I find this kind of patches a real pain, frankly. When I have to look around code and commits to find bugs and bugs origins, I'll have to fully check this kind of patch, and this slow down the process a _lot_, because I have no idea if "Joe Perches" actually tested the patch, or if its yet another "code beautifier" process in the wild. You should have shooted when/before commit bfd5f4a3 (packet: Add GSO/csum offload support) was accepted. 2) You did not CCed Sridhar Samudrala <sri@us.ibm.com>, the author of above commit. I dont think David will take the time to test your patch. 3) You add some non codingstyle artifacts. I am afraid it makes me nervous for patches bringing no real values but shuffling the code and making maintainers life more difficult. ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2011-05-13 5:11 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2011-05-12 20:36 [PATCH] net: af_packet: Don't initialize vnet_hdr Joe Perches 2011-05-12 21:26 ` David Miller 2011-05-12 21:33 ` Joe Perches 2011-05-12 21:36 ` David Miller 2011-05-12 21:55 ` Joe Perches 2011-05-12 21:59 ` David Miller 2011-05-12 23:25 ` [PATCH net-next V3] net: af_packet: Untangle packet_snd by adding vpacket_snd Joe Perches 2011-05-13 5:11 ` Eric Dumazet
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).