From patchwork Tue Jan 10 17:32:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Kaehlcke X-Patchwork-Id: 13095405 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3FE02C54EBE for ; Tue, 10 Jan 2023 17:33:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234199AbjAJRdE (ORCPT ); Tue, 10 Jan 2023 12:33:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56456 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234020AbjAJRc7 (ORCPT ); Tue, 10 Jan 2023 12:32:59 -0500 Received: from mail-io1-xd2e.google.com (mail-io1-xd2e.google.com [IPv6:2607:f8b0:4864:20::d2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D227059D09 for ; Tue, 10 Jan 2023 09:32:57 -0800 (PST) Received: by mail-io1-xd2e.google.com with SMTP id 3so6416509iou.12 for ; Tue, 10 Jan 2023 09:32:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=OtDNURmcpxSd4X5CvSSnrMvtckUZqJr40ZSleDNYRZs=; b=PSSbxEopop8onxh/vKdHAcgs4yAkwL1+Or4bv4hn/RAv/zJ7mJeuy4G8Fvq6/C6wFM qu8sY5piesWyr/yiuxpF39tJvv4fSSU9ne6kGhXfTFlbDTZ00kIXNjUuxI/flIJMuFx7 +MdlVzkigYOHg8dZS1mM/rlisjpzcf0ju5PBs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=OtDNURmcpxSd4X5CvSSnrMvtckUZqJr40ZSleDNYRZs=; b=s4gunx2CL7YsS7OXO7ViJH+4LNZuI76bMGvmpZFhKrTb9hDTgxmyFyEJB3xlpKJpqF Pe4xMYLg7zArWoK32J6yxC+SWNnTO3TvOZ/DfNm+GYAK0X0fd3UuXpUqIRvIRGsT1RYY jUhJTJbMpnz59S6r9mhamcYBiMU7zaP/ekT1q4mRlRhoJXOTIShU7FS3cjbJY/NkhqaG JY2lEzdzVRYy/1n+onaCKOpNvILMJQbvMbjt35i2Eli1JenLWrL7m0heo90VHYIS01Ce ysV2aU2QcvQaay14IFMGrucxA0OO/JNii+1hKanq9NaIEVSkCYeLSorG4T+9AvAT8H9u m/Zg== X-Gm-Message-State: AFqh2krnEHwuIp1pt7p1SZcnClsIE4vLxGaF+hJDzo47eF5us7BbkfGB ORHMxS7Z0snxEu6KqHTBiX1gUg== X-Google-Smtp-Source: AMrXdXvpXvMe09tsbZT3n43/sy0SmBNBMcGmUZYebCUw7PUGrhsQko+s/6SqyEcy5wClUoFKEktBcg== X-Received: by 2002:a5e:8808:0:b0:6e2:af6e:3b58 with SMTP id l8-20020a5e8808000000b006e2af6e3b58mr46499475ioj.12.1673371977226; Tue, 10 Jan 2023 09:32:57 -0800 (PST) Received: from localhost (30.23.70.34.bc.googleusercontent.com. [34.70.23.30]) by smtp.gmail.com with UTF8SMTPSA id f3-20020a02a103000000b0039e91c28766sm1230804jag.167.2023.01.10.09.32.56 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 10 Jan 2023 09:32:56 -0800 (PST) From: Matthias Kaehlcke To: Greg Kroah-Hartman Cc: Icenowy Zheng , stable@vger.kernel.org, Alexander Stein , linux-usb@vger.kernel.org, Johan Hovold , linux-kernel@vger.kernel.org, Stefan Wahren , Douglas Anderson , Matthias Kaehlcke , Ravi Chandra Sadineni Subject: [PATCH v2 1/2] usb: misc: onboard_hub: Invert driver registration order Date: Tue, 10 Jan 2023 17:32:52 +0000 Message-Id: <20230110172954.v2.1.I75494ebee7027a50235ce4b1e930fa73a578fbe2@changeid> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org The onboard_hub 'driver' consists of two drivers, a platform driver and a USB driver. Currently when the onboard hub driver is initialized it first registers the platform driver, then the USB driver. This results in a race condition when the 'attach' work is executed, which is scheduled when the platform device is probed. The purpose of fhe 'attach' work is to bind elegible USB hub devices to the onboard_hub USB driver. This fails if the work runs before the USB driver has been registered. Register the USB driver first, then the platform driver. This increases the chances that the onboard_hub USB devices are probed before their corresponding platform device, which the USB driver tries to locate in _probe(). The driver already handles this situation and defers probing if the onboard hub platform device doesn't exist yet. Cc: stable@vger.kernel.org Fixes: 8bc063641ceb ("usb: misc: Add onboard_usb_hub driver") Link: https://lore.kernel.org/lkml/Y6W00vQm3jfLflUJ@hovoldconsulting.com/T/#m0d64295f017942fd988f7c53425db302d61952b4 Reported-by: Alexander Stein Signed-off-by: Matthias Kaehlcke Tested-by: Stefan Wahren --- (no changes since v1) drivers/usb/misc/onboard_usb_hub.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/usb/misc/onboard_usb_hub.c b/drivers/usb/misc/onboard_usb_hub.c index 94e7966e199d..db0844b30bbd 100644 --- a/drivers/usb/misc/onboard_usb_hub.c +++ b/drivers/usb/misc/onboard_usb_hub.c @@ -433,13 +433,13 @@ static int __init onboard_hub_init(void) { int ret; - ret = platform_driver_register(&onboard_hub_driver); + ret = usb_register_device_driver(&onboard_hub_usbdev_driver, THIS_MODULE); if (ret) return ret; - ret = usb_register_device_driver(&onboard_hub_usbdev_driver, THIS_MODULE); + ret = platform_driver_register(&onboard_hub_driver); if (ret) - platform_driver_unregister(&onboard_hub_driver); + usb_deregister_device_driver(&onboard_hub_usbdev_driver); return ret; } From patchwork Tue Jan 10 17:32:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Kaehlcke X-Patchwork-Id: 13095406 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7EB6EC677F1 for ; Tue, 10 Jan 2023 17:33:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234479AbjAJRdF (ORCPT ); Tue, 10 Jan 2023 12:33:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234313AbjAJRc7 (ORCPT ); Tue, 10 Jan 2023 12:32:59 -0500 Received: from mail-io1-xd2f.google.com (mail-io1-xd2f.google.com [IPv6:2607:f8b0:4864:20::d2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A89015B15B for ; Tue, 10 Jan 2023 09:32:58 -0800 (PST) Received: by mail-io1-xd2f.google.com with SMTP id 3so6416532iou.12 for ; Tue, 10 Jan 2023 09:32:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2WglFD8vrQmflspPOJQOiyJdpFr/DrUWVK6xL71FE/s=; b=h+Jx3Wt1+stYfeew4k9Zc6l0xTu35n7g5OprwCCJHzoJNIyN8fWJMBNXBnMzFjBym2 QSPQfAAN5DNE7nmx7jsgQjJ9CvMYrYxpOYmeOTJRcgU3Q7N0ZGPssAgf4y1hJ2IfqIlv dMRyZxY9GssiBFrOVwzCvZvgbJufTbMvAOR68= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2WglFD8vrQmflspPOJQOiyJdpFr/DrUWVK6xL71FE/s=; b=tVrnJP6tAH/TZ2zvGg10nM3c5nD9fHGKFDzmcQmO2luhYPb1IOybbWkGcmiRCaqLqJ EuEVXGb7Yhz578Eqs7Qc+5SPbfAg24P6JQ/qXIBo1UMCqbTg4UHUrp1osUMKLDSVP7/y lBDIKpaBCbz/d3viVtGqD5ZIX7nsqHRET7i4a85Lb4GAtaWC7EfF7QkTo6+oXExNd6gu MZqqEf8DcVfpOqJqTp0MZTviL5QstFxUJTkaMwK0n3hWEGNgtRMtZK+MyMDP8K1qY17Q Cj7R60unRW/325zfZ+VOdMgKNoa/CD+0qt0XgzglXBG+AtjO3pEseC5z0S3cvqy1stZn QHeA== X-Gm-Message-State: AFqh2kpGTkoY//viai9YUZXGDZ2AeDrFTFC4bY+RpKWGVAnYRod1HDVO q3yvtj2TSu8Wa75T71wqX/ayOg== X-Google-Smtp-Source: AMrXdXvCag/yv2jYixjCgU1I2FqjFssXU34NC/8jslwPn/DflH79Q2heRz5OtoWlxIMyifOOys6Qbg== X-Received: by 2002:a5e:a817:0:b0:6ec:c7a1:d597 with SMTP id c23-20020a5ea817000000b006ecc7a1d597mr49886778ioa.2.1673371978057; Tue, 10 Jan 2023 09:32:58 -0800 (PST) Received: from localhost (30.23.70.34.bc.googleusercontent.com. [34.70.23.30]) by smtp.gmail.com with UTF8SMTPSA id l131-20020a6b3e89000000b006ccc36c963fsm4342016ioa.43.2023.01.10.09.32.57 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 10 Jan 2023 09:32:57 -0800 (PST) From: Matthias Kaehlcke To: Greg Kroah-Hartman Cc: Icenowy Zheng , stable@vger.kernel.org, Alexander Stein , linux-usb@vger.kernel.org, Johan Hovold , linux-kernel@vger.kernel.org, Stefan Wahren , Douglas Anderson , Matthias Kaehlcke , Ravi Chandra Sadineni Subject: [PATCH v2 2/2] usb: misc: onboard_hub: Move 'attach' work to the driver Date: Tue, 10 Jan 2023 17:32:53 +0000 Message-Id: <20230110172954.v2.2.I16b51f32db0c32f8a8532900bfe1c70c8572881a@changeid> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog In-Reply-To: <20230110172954.v2.1.I75494ebee7027a50235ce4b1e930fa73a578fbe2@changeid> References: <20230110172954.v2.1.I75494ebee7027a50235ce4b1e930fa73a578fbe2@changeid> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org Currently each onboard_hub platform device owns an 'attach' work, which is scheduled when the device probes. With this deadlocks have been reported on a Raspberry Pi 3 B+ [1], which has nested onboard hubs. The flow of the deadlock is something like this (with the onboard_hub driver built as a module) [2]: - USB root hub is instantiated - core hub driver calls onboard_hub_create_pdevs(), which creates the 'raw' platform device for the 1st level hub - 1st level hub is probed by the core hub driver - core hub driver calls onboard_hub_create_pdevs(), which creates the 'raw' platform device for the 2nd level hub - onboard_hub platform driver is registered - platform device for 1st level hub is probed - schedules 'attach' work - platform device for 2nd level hub is probed - schedules 'attach' work - onboard_hub USB driver is registered - device (and parent) lock of hub is held while the device is re-probed with the onboard_hub driver - 'attach' work (running in another thread) calls driver_attach(), which blocks on one of the hub device locks - onboard_hub_destroy_pdevs() is called by the core hub driver when one of the hubs is detached - destroying the pdevs invokes onboard_hub_remove(), which waits for the 'attach' work to complete - waits forever, since the 'attach' work can't acquire the device lock Use a single work struct for the driver instead of having a work struct per onboard hub platform driver instance. With that it isn't necessary to cancel the work in onboard_hub_remove(), which fixes the deadlock. The work is only cancelled when the driver is unloaded. [1] https://lore.kernel.org/r/d04bcc45-3471-4417-b30b-5cf9880d785d@i2se.com/ [2] https://lore.kernel.org/all/Y6OrGbqaMy2iVDWB@google.com/ Cc: stable@vger.kernel.org Fixes: 8bc063641ceb ("usb: misc: Add onboard_usb_hub driver") Link: https://lore.kernel.org/r/d04bcc45-3471-4417-b30b-5cf9880d785d@i2se.com/ Link: https://lore.kernel.org/all/Y6OrGbqaMy2iVDWB@google.com/ Reported-by: Stefan Wahren Signed-off-by: Matthias Kaehlcke --- Changes in v2: - drop loop in onboard_hub_probe() to wait for an already running 'attach' work to finish. The loop can cause deadlocks and is not needed. Rationale for why the loop in onboard_hub_probe() isn't needed: The idea behind the loop was: The currently running work might not take into account the USB devices of the hub that is currently probed, which should probe shortly after the hub was powered on. The 'attach' work is only needed for USB devices that were previously detached through device_release_driver() in onboard_hub_remove(). These USB device objects only persist in the kernel if the hub is not powered off (or put into reset) by onboard_hub_remove(). If onboard_hub_probe() is invoked and the USB device objects persisted, then an already running 'attach' work should take them into account. If they didn't persist the running work might miss them, but that wouldn't be a problem since the newly created USB devices don't need to be explicitly attached because they weren't detached previously. drivers/usb/misc/onboard_usb_hub.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/usb/misc/onboard_usb_hub.c b/drivers/usb/misc/onboard_usb_hub.c index db0844b30bbd..969c4c4f2ae9 100644 --- a/drivers/usb/misc/onboard_usb_hub.c +++ b/drivers/usb/misc/onboard_usb_hub.c @@ -27,7 +27,10 @@ #include "onboard_usb_hub.h" +static void onboard_hub_attach_usb_driver(struct work_struct *work); + static struct usb_device_driver onboard_hub_usbdev_driver; +static DECLARE_WORK(attach_usb_driver_work, onboard_hub_attach_usb_driver); /************************** Platform driver **************************/ @@ -45,7 +48,6 @@ struct onboard_hub { bool is_powered_on; bool going_away; struct list_head udev_list; - struct work_struct attach_usb_driver_work; struct mutex lock; }; @@ -271,8 +273,7 @@ static int onboard_hub_probe(struct platform_device *pdev) * This needs to be done deferred to avoid self-deadlocks on systems * with nested onboard hubs. */ - INIT_WORK(&hub->attach_usb_driver_work, onboard_hub_attach_usb_driver); - schedule_work(&hub->attach_usb_driver_work); + schedule_work(&attach_usb_driver_work); return 0; } @@ -285,9 +286,6 @@ static int onboard_hub_remove(struct platform_device *pdev) hub->going_away = true; - if (&hub->attach_usb_driver_work != current_work()) - cancel_work_sync(&hub->attach_usb_driver_work); - mutex_lock(&hub->lock); /* unbind the USB devices to avoid dangling references to this device */ @@ -449,6 +447,8 @@ static void __exit onboard_hub_exit(void) { usb_deregister_device_driver(&onboard_hub_usbdev_driver); platform_driver_unregister(&onboard_hub_driver); + + cancel_work_sync(&attach_usb_driver_work); } module_exit(onboard_hub_exit);