Re: [PATCH v1 3/4] USBNET: support DMA SG

2013-08-03 Thread Ming Lei
On Sun, Aug 4, 2013 at 3:07 AM, David Miller  wrote:
> From: Oliver Neukum 
> Date: Sat, 03 Aug 2013 07:56:53 +0200
>
>> On Sat, 2013-08-03 at 10:46 +0800, Ming Lei wrote:
>>
>>> @@ -1268,10 +1298,14 @@ netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
>>>  entry = (struct skb_data *) skb->cb;
>>>  entry->urb = urb;
>>>  entry->dev = dev;
>>> -entry->length = length;
>>>
>>>  usb_fill_bulk_urb (urb, dev->udev, dev->out,
>>>  skb->data, skb->len, tx_complete, skb);
>>> +if (dev->can_dma_sg) {
>>> +if (build_dma_sg(skb, urb) < 0)
>>> +goto drop;
>>
>> Where do you free urb->sg?
>
> Indeed, this appears to leak.
>
> The devio.c code in the USB layer takes care to manage freeing of
> the urb->sg, so this usbnet code will have to as well.

Hamm, kfree(urb->sg) is removed carelessly in v1, sorry for that.

Will fix it in -v2.

Thanks,
--
Ming Lei
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v1 3/4] USBNET: support DMA SG

2013-08-03 Thread David Miller
From: Oliver Neukum 
Date: Sat, 03 Aug 2013 07:56:53 +0200

> On Sat, 2013-08-03 at 10:46 +0800, Ming Lei wrote:
> 
>> @@ -1268,10 +1298,14 @@ netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
>>  entry = (struct skb_data *) skb->cb;
>>  entry->urb = urb;
>>  entry->dev = dev;
>> -entry->length = length;
>>  
>>  usb_fill_bulk_urb (urb, dev->udev, dev->out,
>>  skb->data, skb->len, tx_complete, skb);
>> +if (dev->can_dma_sg) {
>> +if (build_dma_sg(skb, urb) < 0)
>> +goto drop;
> 
> Where do you free urb->sg?

Indeed, this appears to leak.

The devio.c code in the USB layer takes care to manage freeing of
the urb->sg, so this usbnet code will have to as well.
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v1 3/4] USBNET: support DMA SG

2013-08-02 Thread Oliver Neukum
On Sat, 2013-08-03 at 10:46 +0800, Ming Lei wrote:

> @@ -1268,10 +1298,14 @@ netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
>   entry = (struct skb_data *) skb->cb;
>   entry->urb = urb;
>   entry->dev = dev;
> - entry->length = length;
>  
>   usb_fill_bulk_urb (urb, dev->udev, dev->out,
>   skb->data, skb->len, tx_complete, skb);
> + if (dev->can_dma_sg) {
> + if (build_dma_sg(skb, urb) < 0)
> + goto drop;

Where do you free urb->sg?

Regards
Oliver




--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v1 3/4] USBNET: support DMA SG

2013-08-02 Thread Ming Lei
This patch introduces support of DMA SG if the USB host controller
which usbnet device is attached to is capable of building packet from
discontinuous buffers.

The patch supports passing the skb fragment buffers to usb stack directly
via urb->sg.

Cc: Eric Dumazet 
Cc: Ben Hutchings 
Cc: Grant Grundler 
Cc: Freddy Xin 
Cc: Oliver Neukum 
Cc: Alan Stern 
Signed-off-by: Ming Lei 
---
 drivers/net/usb/usbnet.c   |   38 --
 include/linux/usb/usbnet.h |1 +
 2 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/drivers/net/usb/usbnet.c b/drivers/net/usb/usbnet.c
index e4811d7..51753b3 100644
--- a/drivers/net/usb/usbnet.c
+++ b/drivers/net/usb/usbnet.c
@@ -1232,6 +1232,37 @@ EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
 
 /*-*/
 
+static int build_dma_sg(const struct sk_buff *skb, struct urb *urb)
+{
+   unsigned num_sgs, total_len = 0;
+   int i, s = 0;
+
+   num_sgs = skb_shinfo(skb)->nr_frags + 1;
+   if (num_sgs == 1)
+   return 0;
+
+   urb->sg = kmalloc(num_sgs * sizeof(struct scatterlist), GFP_ATOMIC);
+   if (!urb->sg)
+   return -ENOMEM;
+
+   urb->num_sgs = num_sgs;
+   sg_init_table(urb->sg, urb->num_sgs);
+
+   sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb));
+   total_len += skb_headlen(skb);
+
+   for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
+   struct skb_frag_struct *f = &skb_shinfo(skb)->frags[i];
+
+   total_len += skb_frag_size(f);
+   sg_set_page(&urb->sg[i + s], f->page.p, f->size,
+   f->page_offset);
+   }
+   urb->transfer_buffer_length = total_len;
+
+   return 1;
+}
+
 netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
 struct net_device *net)
 {
@@ -1258,7 +1289,6 @@ netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
goto drop;
}
}
-   length = skb->len;
 
if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
netif_dbg(dev, tx_err, dev->net, "no urb\n");
@@ -1268,10 +1298,14 @@ netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
entry = (struct skb_data *) skb->cb;
entry->urb = urb;
entry->dev = dev;
-   entry->length = length;
 
usb_fill_bulk_urb (urb, dev->udev, dev->out,
skb->data, skb->len, tx_complete, skb);
+   if (dev->can_dma_sg) {
+   if (build_dma_sg(skb, urb) < 0)
+   goto drop;
+   }
+   entry->length = length = urb->transfer_buffer_length;
 
/* don't assume the hardware handles USB_ZERO_PACKET
 * NOTE:  strictly conforming cdc-ether devices should expect
diff --git a/include/linux/usb/usbnet.h b/include/linux/usb/usbnet.h
index 8fbc008..9cb2fe8 100644
--- a/include/linux/usb/usbnet.h
+++ b/include/linux/usb/usbnet.h
@@ -35,6 +35,7 @@ struct usbnet {
unsigned char   suspend_count;
unsigned char   pkt_cnt, pkt_err;
unsigned short  rx_qlen, tx_qlen;
+   unsignedcan_dma_sg:1;
 
/* i/o info: pipes etc */
unsignedin, out;
-- 
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html