2015-08-23 18:45:22

by Muhammad Falak R Wani

[permalink] [raw]
Subject: [PATCH] usb: core: driver: Use kmalloc_array

This patch introduces the use of function kmalloc_array(), instead
of using kmalloc(), for allocating memory for an array and removes
the corresponding call to kmalloc().

Signed-off-by: Muhammad Falak R Wani <[email protected]>
---
drivers/usb/core/driver.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index 818369a..d19106b 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -416,7 +416,7 @@ static int usb_unbind_interface(struct device *dev)
if (ep->streams == 0)
continue;
if (j == 0) {
- eps = kmalloc(USB_MAXENDPOINTS * sizeof(void *),
+ eps = kmalloc_array(USB_MAXENDPOINTS, sizeof(void *),
GFP_KERNEL);
if (!eps) {
dev_warn(dev, "oom, leaking streams\n");
--
1.9.1


2015-08-23 18:59:03

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] usb: core: driver: Use kmalloc_array

On Mon, 2015-08-24 at 00:18 +0530, Muhammad Falak R Wani wrote:
> This patch introduces the use of function kmalloc_array(), instead
> of using kmalloc(), for allocating memory for an array and removes
> the corresponding call to kmalloc().
[]
> diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
[]
> @@ -416,7 +416,7 @@ static int usb_unbind_interface(struct device *dev)
> if (ep->streams == 0)
> continue;
> if (j == 0) {
> - eps = kmalloc(USB_MAXENDPOINTS * sizeof(void *),
> + eps = kmalloc_array(USB_MAXENDPOINTS, sizeof(void *),
> GFP_KERNEL);
> if (!eps) {
> dev_warn(dev, "oom, leaking streams\n");

Allocations like this really don't need to use
kmalloc_array as it's unlikely that USB_MAXENDPOINTS
is very large.

If you are going to do this, it'd be nicer to keep
the parenthesis alignment and likely also better to
remove the dev_warn for a memory leak.