diff --git a/drivers/usb/gadget/function/android_f_accessory.c b/drivers/usb/gadget/function/android_f_accessory.c index 44653667a6ca..e87984222608 100644 --- a/drivers/usb/gadget/function/android_f_accessory.c +++ b/drivers/usb/gadget/function/android_f_accessory.c @@ -1290,6 +1290,14 @@ static int acc_init(void) return 0; err_free_dev: + /* + * Multiple threads might try to access the acc_dev_instance + * therefore protect the failure path with spinlock to avoid race + * conditions. + */ + spin_lock_irqsave(&acc_dev_instance_lock, flags); + acc_dev_instance = NULL; + spin_unlock_irqrestore(&acc_dev_instance_lock, flags); kfree(dev); pr_err("USB accessory gadget driver failed to initialize\n"); return ret;