Skip to contentSkip to conversation

net/usb: rtl8150: allocate URB transfer_buffer and setup_packet separately

PATCH In review Jussi Kivilinna 3 participants 5 messages Aug 7, 2013 – Aug 10, 2013 1 message not in the archive

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

PATCH 1/1 net/usb: rtl8150: allocate URB transfer_buffer and setup_packet separately

rtl8150 allocates URB transfer_buffer and setup_packet as part of same structure 'struct async_req'. This can cause same cacheline to be DMA-mapped twice with same URB. This can lead to memory corruption on some systems.

Patch make allocation of these buffers separate.

Use of 'struct async_req' was introduced by recent commit 4d12997a9b "drivers:
net: usb: rtl8150: concurrent URB bugfix".

Patch is only compile tested.

---
 drivers/net/usb/rtl8150.c |   48 +++++++++++++++++++++++----------------------
 1 file changed, 25 insertions(+), 23 deletions(-)
drivers/net/usb/rtl8150.c +25 -23
index 6cbdac6..c353bfd 100644
@@ -142,11 +142,6 @@ struct rtl8150 {
 
 typedef struct rtl8150 rtl8150_t;
 
-struct async_req {
-	struct usb_ctrlrequest dr;
-	u16 rx_creg;
-};
-
 static const char driver_name [] = "rtl8150";
 
 /*
@@ -170,12 +165,12 @@ static int set_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
 
 static void async_set_reg_cb(struct urb *urb)
 {
-	struct async_req *req = (struct async_req *)urb->context;
 	int status = urb->status;
 
 	if (status < 0)
 		dev_dbg(&urb->dev->dev, "%s failed with %d", __func__, status);
-	kfree(req);
+	kfree(urb->setup_packet); /* dr */
+	kfree(urb->transfer_buffer); /* rx_creg */
 	usb_free_urb(urb);
 }
 
@@ -183,25 +178,27 @@ static int async_set_registers(rtl8150_t *dev, u16 indx, u16 size, u16 reg)
 {
 	int res = -ENOMEM;
 	struct urb *async_urb;
-	struct async_req *req;
+	struct usb_ctrlrequest *dr;
+	u16 *rx_creg;
+
+	dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
+	rx_creg = kmalloc(sizeof(*rx_creg), GFP_ATOMIC);
+	if (!dr || !rx_creg)
+		goto err;
 
-	req = kmalloc(sizeof(struct async_req), GFP_ATOMIC);
-	if (req == NULL)
-		return res;
 	async_urb = usb_alloc_urb(0, GFP_ATOMIC);
-	if (async_urb == NULL) {
-		kfree(req);
-		return res;
-	}
-	req->rx_creg = cpu_to_le16(reg);
-	req->dr.bRequestType = RTL8150_REQT_WRITE;
-	req->dr.bRequest = RTL8150_REQ_SET_REGS;
-	req->dr.wIndex = 0;
-	req->dr.wValue = cpu_to_le16(indx);
-	req->dr.wLength = cpu_to_le16(size);
+	if (async_urb == NULL)
+		goto err;
+
+	*rx_creg = cpu_to_le16(reg);
+	dr->bRequestType = RTL8150_REQT_WRITE;
+	dr->bRequest = RTL8150_REQ_SET_REGS;
+	dr->wIndex = 0;
+	dr->wValue = cpu_to_le16(indx);
+	dr->wLength = cpu_to_le16(size);
 	usb_fill_control_urb(async_urb, dev->udev,
-	                     usb_sndctrlpipe(dev->udev, 0), (void *)&req->dr,
-			     &req->rx_creg, size, async_set_reg_cb, req);
+			     usb_sndctrlpipe(dev->udev, 0), (void *)dr,
+			     rx_creg, size, async_set_reg_cb, NULL);
 	res = usb_submit_urb(async_urb, GFP_ATOMIC);
 	if (res) {
 		if (res == -ENODEV)
@@ -209,6 +206,11 @@ static int async_set_registers(rtl8150_t *dev, u16 indx, u16 size, u16 reg)
 		dev_err(&dev->udev->dev, "%s failed with %d\n", __func__, res);
 	}
 	return res;
+
+err:
+	kfree(dr);
+	kfree(rx_creg);
+	return res;
 }
 
 static int read_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 * reg)
maintainer
#1 lore raw
Quoted 4 lines · Jussi Kivilinna

rtl8150 allocates URB transfer_buffer and setup_packet as part of same structure 'struct async_req'. This can cause same cacheline to be DMA-mapped twice with same URB. This can lead to memory corruption on some systems.

I can see performance impact due to the double mapping. However, memory corruption seems a bit too much for sane cache and DMA controllers. Out of interest - which is the architecture that will potentially corrupt the memory.

cheers,
Petko

A message that is not in the archive (sent off-list or not yet received): alpine.DEB.2.10.1308081809120.4258-WOI+tvYbpdBn9MRfDTB0XAC/[email protected]

#3 lore raw
Quoted 11 lines · Petko Manolov
Quoted 4 lines · Jussi Kivilinna

rtl8150 allocates URB transfer_buffer and setup_packet as part of same structure 'struct async_req'. This can cause same cacheline to be DMA-mapped twice with same URB. This can lead to memory corruption on some systems.

I can see performance impact due to the double mapping. However, memory corruption seems a bit too much for sane cache and DMA controllers. Out of interest - which is the architecture that will potentially corrupt the memory.

rtlwifi driver had similar structure to allocate both setup_packet and transfer_buffer in single go (overlapping dma-mapping cachelines) and this caused problems on ARM/sunxi. Problems means: memory corruptions at random locations, device freezes and lock-ups.

-Jussi

Quoted 5 lines

cheers,
Petko

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

maintainer
#4 lore raw
Quoted 13 lines · Jussi Kivilinna
Quoted 11 lines · Petko Manolov
Quoted 4 lines · Jussi Kivilinna

rtl8150 allocates URB transfer_buffer and setup_packet as part of same structure 'struct async_req'. This can cause same cacheline to be DMA-mapped twice with same URB. This can lead to memory corruption on some systems.

I can see performance impact due to the double mapping. However, memory corruption seems a bit too much for sane cache and DMA controllers. Out of interest - which is the architecture that will potentially corrupt the memory.

Quoted 4 lines

rtlwifi driver had similar structure to allocate both setup_packet and transfer_buffer in single go (overlapping dma-mapping cachelines) and this caused problems on ARM/sunxi. Problems means: memory corruptions at random locations, device freezes and lock-ups.

Broken controllers?..

#5 lore raw
Quoted 15 lines · Petko Manolov
Quoted 13 lines · Jussi Kivilinna
Quoted 11 lines · Petko Manolov
Quoted 4 lines · Jussi Kivilinna

rtl8150 allocates URB transfer_buffer and setup_packet as part of same structure 'struct async_req'. This can cause same cacheline to be DMA-mapped twice with same URB. This can lead to memory corruption on some systems.

I can see performance impact due to the double mapping. However, memory corruption seems a bit too much for sane cache and DMA controllers. Out of interest - which is the architecture that will potentially corrupt the memory.

Quoted 7 lines
Quoted 4 lines

rtlwifi driver had similar structure to allocate both setup_packet and transfer_buffer in single go (overlapping dma-mapping cachelines) and this caused problems on ARM/sunxi. Problems means: memory corruptions at random locations, device freezes and lock-ups.

Broken controllers?..

At first I thought so too, but I discussed about this at linux-usb, and answer was that driver which uses same (dma/-)cacheline for multiple buffers is buggy..
http://marc.info/?l=linux-usb&m=137130407121137&w=2 (at end)
http://marc.info/?l=linux-usb&m=137137080902265&w=2
http://marc.info/?l=linux-usb&m=137242422120782&w=2

-Jussi