[SLOF] [PATCH 2/5] Stack optimization in libusb: split up setup_new_device()

Thomas Huth thuth at redhat.com
Mon Nov 30 21:46:51 AEDT 2015


On 30/11/15 07:01, Alexey Kardashevskiy wrote:
> On 11/26/2015 06:58 AM, Thomas Huth wrote:
>> When scanning hubs, the code in libusb can be called recursively, for
>> example usb_hub_init() calls setup_new_device(), which then calls
>> slof_usb_handle() to execute Forth code for the next device.
>> If that next device is a hub, we end up recursively in usb_hub_init()
>> again.
>> Since stack space is limited in SLOF, we can optimize here a little
>> bit by splitting up the setup_new_device() function into the part
>> that retrieves the descriptors (which takes most of the stack space
>> in this code path since the descriptors are placed on the stack),
>> and the part that populates the the device tree node of the new
>> device (which is responsible for the recursion).
>>
>> Signed-off-by: Thomas Huth <thuth at redhat.com>
>> ---
>>   lib/libusb/usb-core.c | 35 ++---------------------------------
>>   lib/libusb/usb-core.h |  7 +++++--
>>   lib/libusb/usb-ehci.c |  4 +++-
>>   lib/libusb/usb-hub.c  |  4 +++-
>>   lib/libusb/usb-ohci.c |  4 +++-
>>   lib/libusb/usb-slof.c | 34 +++++++++++++++++++++++++++++++++-
>>   lib/libusb/usb-xhci.c |  4 +++-
>>   7 files changed, 52 insertions(+), 40 deletions(-)
>>
>> diff --git a/lib/libusb/usb-core.c b/lib/libusb/usb-core.c
>> index 6719c57..a55f016 100644
>> --- a/lib/libusb/usb-core.c
>> +++ b/lib/libusb/usb-core.c
>> @@ -383,8 +383,6 @@ int usb_hid_exit(void *vdev)
>>       return true;
>>   }
>>
>> -#define usb_get_intf_class(x) ((x & 0x00FF0000) >> 16)
>> -
>>   int usb_msc_init(void *vdev)
>>   {
>>       struct usb_dev *dev;
>> @@ -420,7 +418,7 @@ int usb_msc_exit(void *vdev)
>>       return true;
>>   }
>>
>> -static int usb_msc_reset(struct usb_dev *dev)
>> +int usb_msc_reset(struct usb_dev *dev)
>>   {
>>       struct usb_dev_req req;
>>
>> @@ -485,7 +483,7 @@ static int usb_handle_device(struct usb_dev *dev,
>> struct usb_dev_config_descr *c
>>       return true;
>>   }
>>
>> -int setup_new_device(struct usb_dev *dev, unsigned int port)
>> +int usb_setup_new_device(struct usb_dev *dev, unsigned int port)
>>   {
>>       struct usb_dev_descr descr;
>>       struct usb_dev_config_descr cfg;
> 
> 
> Just to confirm - these descr/cfg plus the rest (ep/len/data) is what
> you optimized by this patch (which is good, I am just making sure I read
> this right)?

Right. I think GCC also inlines the function usb_handle_device() and
some others into setup_new_device(), so their local variables are
accounted into the stack frame of setup_new_device(), too, i.e. it's
even a little bit more than just the local variables of setup_new_device().

 Thomas



More information about the SLOF mailing list