Skip to contentSkip to conversation

Avoid to use kmalloc in usb/core/message.c

PATCH In review Masatake YAMATO 3 participants 4 messages Jul 5, 2005 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Avoid to use kmalloc in usb/core/message.c

In some functions in drivers/usb/core/message.c kmalloc is used to allocate fix size of memory chunks; and the chunks is freed at the end of each functions. The sizes are not so large: 8(struct usb_ctrlrequest), 18(struct usb_device_descriptor), and 2(u16) bytes. I wonder why the invocations of kmalloc are needed in these functions.

Following patch is for avoiding invocations of kmalloc. Instead stacks are used.

The patch is generated by co-diff against rsync://kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git

drivers/usb/core/message.c (mode:100644) +8 -17
Index: drivers/usb/core/message.c
===================================================================
@@ -142,11 +142,9 @@
 int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request, __u8 requesttype,
                         __u16 value, __u16 index, void *data, __u16 size, int timeout)
 {
-       struct usb_ctrlrequest *dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
+       struct usb_ctrlrequest dr_rec;
+       struct usb_ctrlrequest *dr = &dr_rec;
        int ret;
-
-       if (!dr)
-               return -ENOMEM;
 
        dr->bRequestType= requesttype;
        dr->bRequest = request;
@@ -158,7 +156,6 @@
 
        ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);
 
-       kfree(dr);
 
        return ret;
 }
@@ -794,19 +791,17 @@
  */
 int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
 {
+       struct usb_device_descriptor  desc_rec;
        struct usb_device_descriptor *desc;
        int ret;
 
        if (size > sizeof(*desc))
                return -EINVAL;
-       desc = kmalloc(sizeof(*desc), GFP_NOIO);
-       if (!desc)
-               return -ENOMEM;
+       desc = &desc_rec;
 
        ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
        if (ret >= 0)
                memcpy(&dev->descriptor, desc, size);
-       kfree(desc);
        return ret;
 }
 
@@ -835,17 +830,13 @@
 int usb_get_status(struct usb_device *dev, int type, int target, void *data)
 {
        int ret;
-       u16 *status = kmalloc(sizeof(*status), GFP_KERNEL);
-
-       if (!status)
-               return -ENOMEM;
+       u16 status;
 
        ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
-               USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status,
-               sizeof(*status), USB_CTRL_GET_TIMEOUT);
+               USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, &status,
+               sizeof(status), USB_CTRL_GET_TIMEOUT);
 
-       *(u16 *)data = *status;
-       kfree(status);
+       *(u16 *)data = status;
        return ret;
 }
 
#1 lore raw
Quoted 1 line

I wonder why the invocations of kmalloc are needed in these functions.

Because some architectures can't do DMA to/from the stack.

All the best,

Duncan.

#2 lore raw
Quoted 3 lines · Duncan Sands
Quoted 1 line

I wonder why the invocations of kmalloc are needed in these functions.

Because some architectures can't do DMA to/from the stack.

doing dma to/from kmalloc also isn't too nice... should be using dma_alloc_*() API I guess

#3 lore raw
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

I wonder why the invocations of kmalloc are needed in these functions.

Because some architectures can't do DMA to/from the stack.

doing dma to/from kmalloc also isn't too nice... should be using dma_alloc_*() API I guess

The USB core applies dma_map_single to the buffer, so its OK.

Ciao,

Duncan.