Skip to contentSkip to conversation

staging: gdm724x: Fix DMA from stack

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

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 kzalloc().

---
Changes in v2:
- Fixed build warning
- Fixed memory leak using kfree

 drivers/staging/gdm724x/gdm_usb.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/staging/gdm724x/gdm_usb.c +7 -2
index dc4da66c3..c4a9b90c5 100644
@@ -56,11 +56,15 @@ 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;
+	u8 *buf;
+	struct hci_packet *hci;
 	struct usb_device *usbdev = udev->usbdev;
 	int actual;
 	int ret = -1;
+	buf = kzalloc(16, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+	hci = (struct hci_packet *)buf;
 
 	hci->cmd_evt = gdm_cpu_to_dev16(udev->gdm_ed, LTE_GET_INFORMATION);
 	hci->len = gdm_cpu_to_dev16(udev->gdm_ed, 1);
@@ -70,6 +74,7 @@ static int request_mac_address(struct lte_udev *udev)
 			   &actual, 1000);
 
 	udev->request_mac_addr = 1;
+	kfree(buf);
 
 	return ret;
 }

--
2.30.0

maintainer
#1 lore raw
Quoted 19 lines · Amey Narkhede

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

---
Changes in v2:
- Fixed build warning
- Fixed memory leak using kfree

 drivers/staging/gdm724x/gdm_usb.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/staging/gdm724x/gdm_usb.c +0 -0
index dc4da66c3..c4a9b90c5 100644
@@ -56,11 +56,15 @@ 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 +3 -2
 static int request_mac_address(struct lte_udev *udev)
 {
-	u8 buf[16] = {0,};
-	struct hci_packet *hci = (struct hci_packet *)buf;
+	u8 *buf;
+	struct hci_packet *hci;
 	struct usb_device *usbdev = udev->usbdev;
 	int actual;
 	int ret = -1;
+	buf = kzalloc(16, GFP_KERNEL);

checkpatch did not complain about this?

And why do you need 'buf' anymore now? Why not just allocate hci and pass that to the request instead? Saves you an extra cast and an extra pointer.

thanks,

greg k-h

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

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

---
Changes in v2:
- Fixed build warning
- Fixed memory leak using kfree

 drivers/staging/gdm724x/gdm_usb.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/staging/gdm724x/gdm_usb.c +0 -0
index dc4da66c3..c4a9b90c5 100644
@@ -56,11 +56,15 @@ static int gdm_usb_recv(void *priv_dev,
 
Quoted 12 lines

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 +3 -2
 static int request_mac_address(struct lte_udev *udev)
 {
-	u8 buf[16] = {0,};
-	struct hci_packet *hci = (struct hci_packet *)buf;
+	u8 *buf;
+	struct hci_packet *hci;
 	struct usb_device *usbdev = udev->usbdev;
 	int actual;
 	int ret = -1;
+	buf = kzalloc(16, GFP_KERNEL);

checkpatch did not complain about this?

No. checkpatch shows no errors and warnings.

Quoted 8 lines

And why do you need 'buf' anymore now? Why not just allocate hci and pass that to the request instead? Saves you an extra cast and an extra pointer.

thanks,

greg k-h

Thanks. I'll send v3. I assume now we don't need kzalloc anymore as we initialize the hci_packet so kmalloc(sizeof(struct hci_packet),..) will do.

Amey

#3 lore raw
Quoted 39 lines · Amey Narkhede
Quoted 20 lines · Greg KH
Quoted 19 lines · Amey Narkhede

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

---
Changes in v2:
- Fixed build warning
- Fixed memory leak using kfree

 drivers/staging/gdm724x/gdm_usb.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/staging/gdm724x/gdm_usb.c +0 -0
index dc4da66c3..c4a9b90c5 100644
@@ -56,11 +56,15 @@ static int gdm_usb_recv(void *priv_dev,
 
Quoted 12 lines

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 +3 -2
 static int request_mac_address(struct lte_udev *udev)
 {
-	u8 buf[16] = {0,};
-	struct hci_packet *hci = (struct hci_packet *)buf;
+	u8 *buf;
+	struct hci_packet *hci;
 	struct usb_device *usbdev = udev->usbdev;
 	int actual;
 	int ret = -1;
+	buf = kzalloc(16, GFP_KERNEL);

checkpatch did not complain about this?

No. checkpatch shows no errors and warnings.

Quoted 5 lines

And why do you need 'buf' anymore now? Why not just allocate hci and pass that to the request instead? Saves you an extra cast and an extra pointer.

Quoted 5 lines
Quoted 3 lines

thanks,

greg k-h

Thanks. I'll send v3. I assume now we don't need kzalloc anymore as we initialize the hci_packet so kmalloc(sizeof(struct hci_packet),..) will do.

We only initialize the first five bytes, but it also seems as if we only use the first five bytes which raises the question of why we are allocating 16 bytes.

regards,
dan carpenter

#4 lore raw
Quoted 46 lines · Dan Carpenter
Quoted 39 lines · Amey Narkhede
Quoted 20 lines · Greg KH
Quoted 19 lines · Amey Narkhede

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

---
Changes in v2:
- Fixed build warning
- Fixed memory leak using kfree

 drivers/staging/gdm724x/gdm_usb.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/staging/gdm724x/gdm_usb.c +0 -0
index dc4da66c3..c4a9b90c5 100644
@@ -56,11 +56,15 @@ static int gdm_usb_recv(void *priv_dev,
 
Quoted 12 lines

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 +3 -2
 static int request_mac_address(struct lte_udev *udev)
 {
-	u8 buf[16] = {0,};
-	struct hci_packet *hci = (struct hci_packet *)buf;
+	u8 *buf;
+	struct hci_packet *hci;
 	struct usb_device *usbdev = udev->usbdev;
 	int actual;
 	int ret = -1;
+	buf = kzalloc(16, GFP_KERNEL);

checkpatch did not complain about this?

No. checkpatch shows no errors and warnings.

Quoted 5 lines

And why do you need 'buf' anymore now? Why not just allocate hci and pass that to the request instead? Saves you an extra cast and an extra pointer.

Quoted 5 lines
Quoted 3 lines

thanks,

greg k-h

Thanks. I'll send v3. I assume now we don't need kzalloc anymore as we initialize the hci_packet so kmalloc(sizeof(struct hci_packet),..) will do.

Quoted 7 lines

We only initialize the first five bytes, but it also seems as if we only use the first five bytes which raises the question of why we are allocating 16 bytes.

regards,
dan carpenter

That makes sense. I kept 16 bytes as original implementation allocated 16 bytes on stack.

Thanks,
Amey

maintainer
#5 lore raw
In reply to Amey Narkhede
Quoted 33 lines · Amey Narkhede
Quoted 20 lines · Greg KH
Quoted 19 lines · Amey Narkhede

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

---
Changes in v2:
- Fixed build warning
- Fixed memory leak using kfree

 drivers/staging/gdm724x/gdm_usb.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/staging/gdm724x/gdm_usb.c +0 -0
index dc4da66c3..c4a9b90c5 100644
@@ -56,11 +56,15 @@ static int gdm_usb_recv(void *priv_dev,
 
Quoted 12 lines

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 +3 -2
 static int request_mac_address(struct lte_udev *udev)
 {
-	u8 buf[16] = {0,};
-	struct hci_packet *hci = (struct hci_packet *)buf;
+	u8 *buf;
+	struct hci_packet *hci;
 	struct usb_device *usbdev = udev->usbdev;
 	int actual;
 	int ret = -1;
+	buf = kzalloc(16, GFP_KERNEL);

checkpatch did not complain about this?

Quoted 1 line

No. checkpatch shows no errors and warnings.

Please add a blank line after variables and before logic.

Quoted 9 lines
Quoted 7 lines

And why do you need 'buf' anymore now? Why not just allocate hci and pass that to the request instead? Saves you an extra cast and an extra pointer.

thanks,

greg k-h

Thanks. I'll send v3. I assume now we don't need kzalloc anymore as we initialize the hci_packet so kmalloc(sizeof(struct hci_packet),..) will do.

Why is it needed now? And why would that change?

thanks,

greg k-h

#6 lore raw
Quoted 34 lines · Greg KH
Quoted 33 lines · Amey Narkhede
Quoted 32 lines · Greg KH
Quoted 19 lines · Amey Narkhede

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

---
Changes in v2:
- Fixed build warning
- Fixed memory leak using kfree

 drivers/staging/gdm724x/gdm_usb.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/staging/gdm724x/gdm_usb.c +0 -0
index dc4da66c3..c4a9b90c5 100644
@@ -56,11 +56,15 @@ 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 +3 -2
 static int request_mac_address(struct lte_udev *udev)
 {
-	u8 buf[16] = {0,};
-	struct hci_packet *hci = (struct hci_packet *)buf;
+	u8 *buf;
+	struct hci_packet *hci;
 	struct usb_device *usbdev = udev->usbdev;
 	int actual;
 	int ret = -1;
+	buf = kzalloc(16, GFP_KERNEL);

checkpatch did not complain about this?

Quoted 4 lines
Quoted 1 line

No. checkpatch shows no errors and warnings.

Please add a blank line after variables and before logic.

Will do thanks.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

And why do you need 'buf' anymore now? Why not just allocate hci and pass that to the request instead? Saves you an extra cast and an extra pointer.

thanks,

greg k-h

Quoted 8 lines
Quoted 2 lines

Thanks. I'll send v3. I assume now we don't need kzalloc anymore as we initialize the hci_packet so kmalloc(sizeof(struct hci_packet),..) will do.

Why is it needed now? And why would that change?

thanks,

greg k-h

I was thinking about allcoating hci_packet(hci) but as Dan said we only use first five bytes so kmalloc(5, ...) should work.

Thanks,
Amey