• Chris Bainbridge's avatar
    usb: host: xhci: add mutex for non-thread-safe data · a00918d0
    Chris Bainbridge authored
    Regression in commit 638139eb ("usb: hub: allow to process more usb
    hub events in parallel")
    
    The regression resulted in intermittent failure to initialise a 10-port
    hub (with three internal VL812 4-port hub controllers) on boot, with a
    failure rate of around 8%, due to multiple race conditions when
    accessing addr_dev and slot_id in struct xhci_hcd.
    
    This regression also exposed a problem with xhci_setup_device, which
    "should be protected by the usb_address0_mutex" but no longer is due to
    
    commit 6fecd4f2 ("USB: separate usb_address0 mutexes for each bus")
    
    With separate buses (and locks) it is no longer the case that a single
    lock will protect xhci_setup_device from accesses by two parallel
    threads processing events on the two buses.
    
    Fix this by adding a mutex to protect addr_dev and slot_id in struct
    xhci_hcd, and by making the assignment of slot_id atomic.
    
    Fixes multiple boot errors:
    
    [ 0.583008] xhci_hcd 0000:00:14.0: Bad Slot ID 2
    [ 0.583009] xhci_hcd 0000:00:14.0: Could not allocate xHCI USB device data structures
    [ 0.583012] usb usb1-port3: couldn't allocate usb_device
    
    And:
    
    [ 0.637409] xhci_hcd 0000:00:14.0: Error while assigning device slot ID
    [ 0.637417] xhci_hcd 0000:00:14.0: Max number of devices this xHCI host supports is 32.
    [ 0.637421] usb usb1-port1: couldn't allocate usb_device
    
    And:
    
    [ 0.753372] xhci_hcd 0000:00:14.0: ERROR: unexpected setup context command completion code 0x0.
    [ 0.753373] usb 1-3: hub failed to enable device, error -22
    [ 0.753400] xhci_hcd 0000:00:14.0: Error while assigning device slot ID
    [ 0.753402] xhci_hcd 0000:00:14.0: Max number of devices this xHCI host supports is 32.
    [ 0.753403] usb usb1-port3: couldn't allocate usb_device
    
    And:
    
    [ 11.018386] usb 1-3: device descriptor read/all, error -110
    
    And:
    
    [ 5.753838] xhci_hcd 0000:00:14.0: Timeout while waiting for setup device command
    
    Tested with 200 reboots, resulting in no USB hub init related errors.
    
    Fixes: 638139eb ("usb: hub: allow to process more usb hub events in parallel")
    Link: https://lkml.kernel.org/g/CAP-bSRb=A0iEYobdGCLpwynS7pkxpt_9ZnwyZTPVAoy0Y=Zo3Q@mail.gmail.comSigned-off-by: default avatarChris Bainbridge <chris.bainbridge@gmail.com>
    Cc: <stable@vger.kernel.org> # 3.18+
    [changed git commit description style for checkpatch -Mathias]
    Signed-off-by: default avatarMathias Nyman <mathias.nyman@linux.intel.com>
    Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
    a00918d0
xhci.h 64.6 KB