Skip to contentSkip to conversation

staging: gdm724x: Fix DMA from stack

PATCH Superseded [email protected] 4 participants 5 messages Feb 9, 2021 Staging

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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

From: Amey Narkhede <[email protected]>

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

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

--
2.30.0

#1 lore raw
Thank you for the patch! Perhaps something to improve:

Hi,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on staging/staging-testing]

url:    https://github.com/0day-ci/linux/commits/ameynarkhede03-gmail-com/staging-gdm724x-Fix-DMA-from-stack/20210209-225530
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git 06b0c0dce88e2aa2f01343db0f26d214d7f264a0
config: m68k-allmodconfig (attached as .config)
compiler: m68k-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/d793f4f05736924fc2207a0f8c338115523930da
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review ameynarkhede03-gmail-com/staging-gdm724x-Fix-DMA-from-stack/20210209-225530 git checkout d793f4f05736924fc2207a0f8c338115523930da
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=m68k

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

In file included from include/linux/kernel.h:10,

                    from include/linux/list.h:9,
                    from include/linux/module.h:12,
                    from drivers/staging/gdm724x/gdm_usb.c:6:
   include/linux/scatterlist.h: In function 'sg_set_buf':
   arch/m68k/include/asm/page_mm.h:174:49: warning: ordered comparison of pointer with null pointer [-Wextra]
     174 | #define virt_addr_valid(kaddr) ((void *)(kaddr) >= (void *)PAGE_OFFSET && (void *)(kaddr) < high_memory)
         |                                                 ^~
   include/linux/compiler.h:78:42: note: in definition of macro 'unlikely'
      78 | # define unlikely(x) __builtin_expect(!!(x), 0)
         |                                          ^
   include/linux/scatterlist.h:137:2: note: in expansion of macro 'BUG_ON'
     137 |  BUG_ON(!virt_addr_valid(buf));
         |  ^~~~~~
   include/linux/scatterlist.h:137:10: note: in expansion of macro 'virt_addr_valid'
     137 |  BUG_ON(!virt_addr_valid(buf));
         |          ^~~~~~~~~~~~~~~
   drivers/staging/gdm724x/gdm_usb.c: In function 'request_mac_address':
>> drivers/staging/gdm724x/gdm_usb.c:60:21: warning: 'buf' is used uninitialized in this function [-Wuninitialized]
      60 |  struct hci_packet *hci = (struct hci_packet *)buf;
         |                     ^~~

vim +/buf +60 drivers/staging/gdm724x/gdm_usb.c

61e12104764512 Won Kang       2013-07-25  50  
61e12104764512 Won Kang       2013-07-25  51  static int gdm_usb_recv(void *priv_dev,
35db0350c5ef0d Aybuke Ozdemir 2014-03-14  52  			int (*cb)(void *cb_data,
35db0350c5ef0d Aybuke Ozdemir 2014-03-14  53  				  void *data, int len, int context),
61e12104764512 Won Kang       2013-07-25  54  			void *cb_data,
61e12104764512 Won Kang       2013-07-25  55  			int context);
61e12104764512 Won Kang       2013-07-25  56  
61e12104764512 Won Kang       2013-07-25  57  static int request_mac_address(struct lte_udev *udev)
61e12104764512 Won Kang       2013-07-25  58  {
d793f4f0573692 Amey Narkhede  2021-02-09  59  	u8 *buf;
61e12104764512 Won Kang       2013-07-25 @60  	struct hci_packet *hci = (struct hci_packet *)buf;
61e12104764512 Won Kang       2013-07-25  61  	struct usb_device *usbdev = udev->usbdev;
61e12104764512 Won Kang       2013-07-25  62  	int actual;
61e12104764512 Won Kang       2013-07-25  63  	int ret = -1;
61e12104764512 Won Kang       2013-07-25  64  
1b5e56ece3f501 Quytelda Kahja 2018-02-22  65  	hci->cmd_evt = gdm_cpu_to_dev16(udev->gdm_ed, LTE_GET_INFORMATION);
1b5e56ece3f501 Quytelda Kahja 2018-02-22  66  	hci->len = gdm_cpu_to_dev16(udev->gdm_ed, 1);
61e12104764512 Won Kang       2013-07-25  67  	hci->data[0] = MAC_ADDRESS;
61e12104764512 Won Kang       2013-07-25  68  
d793f4f0573692 Amey Narkhede  2021-02-09  69  	buf = kmalloc(16, GFP_KERNEL);
d793f4f0573692 Amey Narkhede  2021-02-09  70  	if (!buf)
d793f4f0573692 Amey Narkhede  2021-02-09  71  		return -ENOMEM;
d793f4f0573692 Amey Narkhede  2021-02-09  72  
61e12104764512 Won Kang       2013-07-25  73  	ret = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 2), buf, 5,
61e12104764512 Won Kang       2013-07-25  74  			   &actual, 1000);
61e12104764512 Won Kang       2013-07-25  75  
61e12104764512 Won Kang       2013-07-25  76  	udev->request_mac_addr = 1;
61e12104764512 Won Kang       2013-07-25  77  
61e12104764512 Won Kang       2013-07-25  78  	return ret;
61e12104764512 Won Kang       2013-07-25  79  }
61e12104764512 Won Kang       2013-07-25  80  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

maintainer
#2 lore raw
In reply to [email protected]
Quoted 25 lines · On Tue, Feb 09, 2021 at 08:24:15PM +0530, [email protected] wrote:

From: Amey Narkhede <[email protected]>

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

---
 drivers/staging/gdm724x/gdm_usb.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/staging/gdm724x/gdm_usb.c +1 -1
index dc4da66c3..50dc463d4 100644
@@ -56,7 +56,7 @@ static int gdm_usb_recv(void *priv_dev,
 
 static int request_mac_address(struct lte_udev *udev)
 {
-	u8 buf[16] = {0,};
+	u8 *buf;
 	struct hci_packet *hci = (struct hci_packet *)buf;
 	struct usb_device *usbdev = udev->usbdev;
 	int actual;

drivers/staging/gdm724x/gdm_usb.c static int request_mac_address(struct lte_udev *udev) · line 72

Quoted 7 lines
drivers/staging/gdm724x/gdm_usb.c +4 -0
@@ -66,6 +66,10 @@ static int request_mac_address(struct lte_udev *udev)
 	hci->len = gdm_cpu_to_dev16(udev->gdm_ed, 1);
 	hci->data[0] = MAC_ADDRESS;
 
+	buf = kmalloc(16, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+

This is great, but you just added a build warning, which implies that the patch is incorrect.

You also have a memory leak here, which is not acceptable :(

thanks,

greg k-h

#3 lore raw
Quoted 37 lines · Greg KH
Quoted 25 lines · On Tue, Feb 09, 2021 at 08:24:15PM +0530, [email protected] wrote:

From: Amey Narkhede <[email protected]>

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

---
 drivers/staging/gdm724x/gdm_usb.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/staging/gdm724x/gdm_usb.c +1 -1
index dc4da66c3..50dc463d4 100644
@@ -56,7 +56,7 @@ static int gdm_usb_recv(void *priv_dev,
 
 static int request_mac_address(struct lte_udev *udev)
 {
-	u8 buf[16] = {0,};
+	u8 *buf;
 	struct hci_packet *hci = (struct hci_packet *)buf;
 	struct usb_device *usbdev = udev->usbdev;
 	int actual;

drivers/staging/gdm724x/gdm_usb.c static int request_mac_address(struct lte_udev *udev) · line 72

Quoted 7 lines
drivers/staging/gdm724x/gdm_usb.c +4 -0
@@ -66,6 +66,10 @@ static int request_mac_address(struct lte_udev *udev)
 	hci->len = gdm_cpu_to_dev16(udev->gdm_ed, 1);
 	hci->data[0] = MAC_ADDRESS;
 
+	buf = kmalloc(16, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+

This is great, but you just added a build warning, which implies that the patch is incorrect.

Quoted 5 lines

You also have a memory leak here, which is not acceptable :(

thanks,

greg k-h

Apologoes. I'll send v2.

Thanks,
Amey

#4 lore raw
In reply to [email protected]
Quoted 25 lines · On Tue, Feb 09, 2021 at 08:24:15PM +0530, [email protected] wrote:

From: Amey Narkhede <[email protected]>

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

---
 drivers/staging/gdm724x/gdm_usb.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/staging/gdm724x/gdm_usb.c +1 -1
index dc4da66c3..50dc463d4 100644
@@ -56,7 +56,7 @@ static int gdm_usb_recv(void *priv_dev,
 
 static int request_mac_address(struct lte_udev *udev)
 {
-	u8 buf[16] = {0,};
+	u8 *buf;
 	struct hci_packet *hci = (struct hci_packet *)buf;
 	struct usb_device *usbdev = udev->usbdev;
 	int actual;

drivers/staging/gdm724x/gdm_usb.c static int request_mac_address(struct lte_udev *udev) · line 74

Quoted 9 lines
drivers/staging/gdm724x/gdm_usb.c +4 -0
@@ -66,6 +66,10 @@ static int request_mac_address(struct lte_udev *udev)
 	hci->len = gdm_cpu_to_dev16(udev->gdm_ed, 1);
 	hci->data[0] = MAC_ADDRESS;
 
+	buf = kmalloc(16, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
 	ret = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 2), buf, 5,
 			   &actual, 1000);

You need to add a kfree() as well.

regards,
dan carpenter