From: Rémi Denis-Courmont <remi.denis-courmont@xxxxxxxxx> Instead of a large (physically) linear buffer, we generate a paged sk_buff, so no extra memory copy is involved. This removes high-order allocations and saves quite a bit of locked memory (MTU is 65543 bytes, was padded to 128 kilo-bytes). This also limits overcharging of the receiver socket buffer space. Signed-off-by: Rémi Denis-Courmont <remi.denis-courmont@xxxxxxxxx> --- drivers/usb/gadget/f_phonet.c | 76 +++++++++++++++++++++++++++------------- 1 files changed, 51 insertions(+), 25 deletions(-) diff --git a/drivers/usb/gadget/f_phonet.c b/drivers/usb/gadget/f_phonet.c index 25e711f..51235f2 100644 --- a/drivers/usb/gadget/f_phonet.c +++ b/drivers/usb/gadget/f_phonet.c @@ -35,6 +35,10 @@ #include "u_phonet.h" #define PN_MEDIA_USB 0x1B +#define MAXPACKET 512 +#if (PAGE_SIZE % MAXPACKET) +#error MAXPACKET must divide PAGE_SIZE! +#endif /*-------------------------------------------------------------------------*/ @@ -45,6 +49,10 @@ struct phonet_port { struct f_phonet { struct usb_function function; + struct { + struct sk_buff *skb; + spinlock_t lock; + } rx; struct net_device *dev; struct usb_ep *in_ep, *out_ep; @@ -138,7 +146,7 @@ pn_hs_sink_desc = { .bEndpointAddress = USB_DIR_OUT, .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = cpu_to_le16(512), + .wMaxPacketSize = cpu_to_le16(MAXPACKET), }; static struct usb_endpoint_descriptor @@ -308,21 +316,21 @@ static void pn_net_setup(struct net_device *dev) static int pn_rx_submit(struct f_phonet *fp, struct usb_request *req, gfp_t gfp_flags) { - struct sk_buff *skb; - const size_t size = fp->dev->mtu; + struct net_device *dev = fp->dev; + struct page *page; int err; - skb = alloc_skb(size, gfp_flags); - if (!skb) + page = __netdev_alloc_page(dev, gfp_flags); + if (!page) return -ENOMEM; - req->buf = skb->data; - req->length = size; - req->context = skb; + req->buf = page_address(page); + req->length = PAGE_SIZE; + req->context = page; err = usb_ep_queue(fp->out_ep, req, gfp_flags); if (unlikely(err)) - dev_kfree_skb_any(skb); + netdev_free_page(dev, page); return err; } @@ -330,25 +338,37 @@ static void pn_rx_complete(struct usb_ep *ep, struct usb_request *req) { struct f_phonet *fp = ep->driver_data; struct net_device *dev = fp->dev; - struct sk_buff *skb = req->context; + struct page *page = req->context; + struct sk_buff *skb; + unsigned long flags; int status = req->status; switch (status) { case 0: - if (unlikely(!netif_running(dev))) - break; - if (unlikely(req->actual < 1)) + spin_lock_irqsave(&fp->rx.lock, flags); + skb = fp->rx.skb; + if (!skb) + skb = fp->rx.skb = netdev_alloc_skb(dev, 12); + if (req->actual < req->length) /* Last fragment */ + fp->rx.skb = NULL; + spin_unlock_irqrestore(&fp->rx.lock, flags); + + if (unlikely(!skb)) break; - skb_put(skb, req->actual); - skb->protocol = htons(ETH_P_PHONET); - skb_reset_mac_header(skb); - __skb_pull(skb, 1); - skb->dev = dev; - dev->stats.rx_packets++; - dev->stats.rx_bytes += skb->len; - - netif_rx(skb); - skb = NULL; + skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, 0, + req->actual); + page = NULL; + + if (req->actual < req->length) { /* Last fragment */ + skb->protocol = htons(ETH_P_PHONET); + skb_reset_mac_header(skb); + pskb_pull(skb, 1); + skb->dev = dev; + dev->stats.rx_packets++; + dev->stats.rx_bytes += skb->len; + + netif_rx(skb); + } break; /* Do not resubmit in these cases: */ @@ -366,8 +386,8 @@ static void pn_rx_complete(struct usb_ep *ep, struct usb_request *req) break; } - if (skb) - dev_kfree_skb_any(skb); + if (page) + netdev_free_page(dev, page); if (req) pn_rx_submit(fp, req, GFP_ATOMIC); } @@ -386,6 +407,10 @@ static void __pn_reset(struct usb_function *f) usb_ep_disable(fp->out_ep); usb_ep_disable(fp->in_ep); + if (fp->rx.skb) { + dev_kfree_skb_irq(fp->rx.skb); + fp->rx.skb = NULL; + } } static int pn_set_alt(struct usb_function *f, unsigned intf, unsigned alt) @@ -584,6 +609,7 @@ int __init phonet_bind_config(struct usb_configuration *c) fp->function.set_alt = pn_set_alt; fp->function.get_alt = pn_get_alt; fp->function.disable = pn_disconnect; + spin_lock_init(&fp->rx.lock); err = usb_add_function(c, &fp->function); if (err) -- 1.5.6.5 -- To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html