Skip to contentSkip to conversation

staging: gdm724x: Fix DMA from stack

PATCH Superseded Amey Narkhede 3 participants 5 messages Feb 10, 2021 Staging

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 staging: gdm724x: Fix DMA from stack

Stack allocated buffers cannot be used for DMA on all architectures so allocate hci_packet buffer using kmalloc.

---
Changes in v3:
- Remove superfluous buf pointer
- Reduce size of allocation of hci_packet to match number of bytes used for DMA

 drivers/staging/gdm724x/gdm_usb.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/staging/gdm724x/gdm_usb.c +7 -3
index dc4da66c3..80c58a3ef 100644
@@ -56,20 +56,24 @@ static int gdm_usb_recv(void *priv_dev,
 
 static int request_mac_address(struct lte_udev *udev)
 {
-	u8 buf[16] = {0,};
-	struct hci_packet *hci = (struct hci_packet *)buf;
+	struct hci_packet *hci;
 	struct usb_device *usbdev = udev->usbdev;
 	int actual;
 	int ret = -1;
 
+	hci = kmalloc(5, GFP_KERNEL);
+	if (!hci)
+		return -ENOMEM;
+
 	hci->cmd_evt = gdm_cpu_to_dev16(udev->gdm_ed, LTE_GET_INFORMATION);
 	hci->len = gdm_cpu_to_dev16(udev->gdm_ed, 1);
 	hci->data[0] = MAC_ADDRESS;
 
-	ret = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 2), buf, 5,
+	ret = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 2), &hci, 5,
 			   &actual, 1000);
 
 	udev->request_mac_addr = 1;
+	kfree(hci);
 
 	return ret;
 }

--
2.30.1

maintainer
#1 lore raw
Quoted 20 lines · Amey Narkhede

Stack allocated buffers cannot be used for DMA on all architectures so allocate hci_packet buffer using kmalloc.

---
Changes in v3:
- Remove superfluous buf pointer
- Reduce size of allocation of hci_packet to match number of bytes used for DMA

 drivers/staging/gdm724x/gdm_usb.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/staging/gdm724x/gdm_usb.c +0 -0
index dc4da66c3..80c58a3ef 100644
@@ -56,20 +56,24 @@ static int gdm_usb_recv(void *priv_dev,
 

drivers/staging/gdm724x/gdm_usb.c static int gdm_usb_recv(void *priv_dev, · line 64

Quoted 10 lines
drivers/staging/gdm724x/gdm_usb.c +2 -2
 static int request_mac_address(struct lte_udev *udev)
 {
-	u8 buf[16] = {0,};
-	struct hci_packet *hci = (struct hci_packet *)buf;
+	struct hci_packet *hci;
 	struct usb_device *usbdev = udev->usbdev;
 	int actual;
 	int ret = -1;
 
+	hci = kmalloc(5, GFP_KERNEL);

Why "5" and not:

	hci = kmalloc(sizeof(*hci), GFP_KERNEL);

?

thanks,

greg k-h

#2 lore raw
Quoted 33 lines · Greg KH
Quoted 20 lines · Amey Narkhede

Stack allocated buffers cannot be used for DMA on all architectures so allocate hci_packet buffer using kmalloc.

---
Changes in v3:
- Remove superfluous buf pointer
- Reduce size of allocation of hci_packet to match number of bytes used for DMA

 drivers/staging/gdm724x/gdm_usb.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/staging/gdm724x/gdm_usb.c +0 -0
index dc4da66c3..80c58a3ef 100644
@@ -56,20 +56,24 @@ static int gdm_usb_recv(void *priv_dev,
 

drivers/staging/gdm724x/gdm_usb.c static int gdm_usb_recv(void *priv_dev, · line 64

Quoted 10 lines
drivers/staging/gdm724x/gdm_usb.c +2 -2
 static int request_mac_address(struct lte_udev *udev)
 {
-	u8 buf[16] = {0,};
-	struct hci_packet *hci = (struct hci_packet *)buf;
+	struct hci_packet *hci;
 	struct usb_device *usbdev = udev->usbdev;
 	int actual;
 	int ret = -1;
 
+	hci = kmalloc(5, GFP_KERNEL);

Why "5" and not:

Quoted 6 lines
	hci = kmalloc(sizeof(*hci), GFP_KERNEL);

?

thanks,

greg k-h

I really need a cup of coffee :)
I'll send v4

Thanks,
Amey

#3 lore raw
Quoted 31 lines · Amey Narkhede
Quoted 30 lines · Greg KH
Quoted 29 lines · Amey Narkhede

Stack allocated buffers cannot be used for DMA on all architectures so allocate hci_packet buffer using kmalloc.

---
Changes in v3:
- Remove superfluous buf pointer
- Reduce size of allocation of hci_packet to match number of bytes used for DMA

 drivers/staging/gdm724x/gdm_usb.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/staging/gdm724x/gdm_usb.c +1 -2
index dc4da66c3..80c58a3ef 100644
@@ -56,20 +56,24 @@ static int gdm_usb_recv(void *priv_dev,
 
 static int request_mac_address(struct lte_udev *udev)
 {
-	u8 buf[16] = {0,};
-	struct hci_packet *hci = (struct hci_packet *)buf;
+	struct hci_packet *hci;
 	struct usb_device *usbdev = udev->usbdev;
 	int actual;
 	int ret = -1;
 
Quoted 4 lines
Quoted 4 lines

drivers/staging/gdm724x/gdm_usb.c static int gdm_usb_recv(void *priv_dev, · line 64

Quoted 1 line
drivers/staging/gdm724x/gdm_usb.c +1 -0
+	hci = kmalloc(5, GFP_KERNEL);

Why "5" and not:

	hci = kmalloc(sizeof(*hci), GFP_KERNEL);

5 is correct and sizeof(*hci) is 4. The hci struct ends in a zero element array. You could do:

	hci = kmalloc(struct_size(hci, data, 1), GFP_KERNEL);

I'm not sure it's more readable. But you still will have to resend because the patch passes "&hci" to usb_bulk_msg() instead of "hci" so it will corrupt memory.

I always encourage people to write the patch and then sit on it over night and send it the next day.

regards,
dan carpenter

#4 lore raw
Quoted 46 lines · Dan Carpenter
Quoted 31 lines · Amey Narkhede
Quoted 30 lines · Greg KH
Quoted 29 lines · Amey Narkhede

Stack allocated buffers cannot be used for DMA on all architectures so allocate hci_packet buffer using kmalloc.

---
Changes in v3:
- Remove superfluous buf pointer
- Reduce size of allocation of hci_packet to match number of bytes used for DMA

 drivers/staging/gdm724x/gdm_usb.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/staging/gdm724x/gdm_usb.c +1 -2
index dc4da66c3..80c58a3ef 100644
@@ -56,20 +56,24 @@ static int gdm_usb_recv(void *priv_dev,
 
 static int request_mac_address(struct lte_udev *udev)
 {
-	u8 buf[16] = {0,};
-	struct hci_packet *hci = (struct hci_packet *)buf;
+	struct hci_packet *hci;
 	struct usb_device *usbdev = udev->usbdev;
 	int actual;
 	int ret = -1;
 
Quoted 4 lines
Quoted 4 lines

drivers/staging/gdm724x/gdm_usb.c static int gdm_usb_recv(void *priv_dev, · line 64

Quoted 1 line
drivers/staging/gdm724x/gdm_usb.c +1 -0
+	hci = kmalloc(5, GFP_KERNEL);

Why "5" and not:

	hci = kmalloc(sizeof(*hci), GFP_KERNEL);

5 is correct and sizeof(*hci) is 4. The hci struct ends in a zero element array. You could do:

	hci = kmalloc(struct_size(hci, data, 1), GFP_KERNEL);

I'm not sure it's more readable. But you still will have to resend because the patch passes "&hci" to usb_bulk_msg() instead of "hci" so it will corrupt memory.

Quoted 6 lines

I always encourage people to write the patch and then sit on it over night and send it the next day.

regards,
dan carpenter

Yes I was about to send patch with kmalloc(sizeof(*hci) + sizeof(u8)). Now I'll take your advice and send the patch tomorrow.

Thanks,
Amey