From patchwork Mon Jan 29 09:37:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xu Yang X-Patchwork-Id: 13535311 Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03on2076.outbound.protection.outlook.com [40.107.104.76]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3A4825677E for ; Mon, 29 Jan 2024 09:31:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.104.76 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706520675; cv=fail; b=or1sVXCfRMan/IJx6WqFamhWKNUfJmhQBaYv4ZR+Ls6sy7ffdDfIF5n/7qH/BZQqOkVfPpaS+K/TPrPNZz2/g61IAwXW6MpOD2c+a9A5oVy1SbwKU/t69E74wnU3L+HaDBitCkbSvINo44llC5ctIYc7Ut5kfM8kReuKywZyQZs= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706520675; c=relaxed/simple; bh=ydh41rTKIqy4Q6rL2x2uutUQv+fwBMau6RKfpkLQYvY=; h=From:To:Cc:Subject:Date:Message-Id:Content-Type:MIME-Version; b=n8yOVRhO48f/+ZIYaADX4y9YtZ2tQAsIQTWr9en7kRhyXPmKUWHmCq/cOvjU3H2jn0hUfEkVE6YksEay9Ko/VeYvCXeoVxqQek9jmk9e1p9qE1ntNv/jB/GDb/i9dPexrLVc6dJ0Ogv0VlLfroxiiKvETKa4ZjYmOlibTdwCo9E= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b=YI0QFHz2; arc=fail smtp.client-ip=40.107.104.76 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nxp.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b="YI0QFHz2" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kaECeY9Wsm+Q9rlvYLdk4vANWUO8z3WSgSlndJji/5EHOYSNwZ9ItEjdl6r5yh/x/iGWzACpQvWRBoQQEOvtN4sngw8FjTVYwlLlIWbKySaLg++4o7udFEI16CNFxgDBxa738CS57GvhPuFK8IZuoMj6ZDKgMJGA3eEXU1Sj0+4wRBjZAfGAeL+Cc+3fIQwggFqu8wPYgPwD7BXqrnPrc6V0pEVOU/nNk+6o96hTB9uQrmHgQMuWE8/v4i3/jFylXAAGTtCogVsL+Oo5XmPIJNj14C6SDvyzqw7mA/sQ3cS3Wmuyq14G75gr2jzAhVh0Dzr4C6mFEYGfWCd+q8ik3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=QoJb5bB04iRB1/ov73mTeaWhKaQ3Le4be4LEqNvwQHg=; b=khWcb4maz0qKUL/QB2BbENb6tk5qmRa9Wn6t6+1Vw1jfazHLL6FtK5WcWdN4un5l549jJ6ISyTJer6jaQySK/1mgaEJ7zg6muqymaiHYKtpslMN73QBqOOw9NK2I6den23BfV2jPLGsnquRkWrLcDJee1A4F5Wa2s60hKgz7SFTVKx5xmFdDnF9fD53F8tO9WBOqOuDhMLG502HEKouo/5Zdp4oK85aGie0pRuZdq9iS5ZXMmAZLMB7w5bq0tNHZ9BD1lp+F9CeI5yk7ELEjPS4nxuW48k4HOZRGPWzoj3UGtOBycGeZNoR1Tq2nc4m8B/+RabnS+qvC+O4+Lsp+Vg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=QoJb5bB04iRB1/ov73mTeaWhKaQ3Le4be4LEqNvwQHg=; b=YI0QFHz2HWzJyTKiG6zYA1uhBA21Fq1rpP47mvxA57rMtc8D3h9CfENp72jGIP32sqXpQSSmzUxOt56FfNlGR8Wsy+jw3fZEhQpiddUe4XYTUgu3krmiRAnXNlTy/rztAqg2p5GNXu1Sc3Ope7c6nyg5GQD6g7GJXj0guDMgagM= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from DU2PR04MB8822.eurprd04.prod.outlook.com (2603:10a6:10:2e1::11) by PAXPR04MB8927.eurprd04.prod.outlook.com (2603:10a6:102:20e::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Mon, 29 Jan 2024 09:31:09 +0000 Received: from DU2PR04MB8822.eurprd04.prod.outlook.com ([fe80::d45f:4483:c11:68b0]) by DU2PR04MB8822.eurprd04.prod.outlook.com ([fe80::d45f:4483:c11:68b0%7]) with mapi id 15.20.7228.029; Mon, 29 Jan 2024 09:31:09 +0000 From: Xu Yang To: gregkh@linuxfoundation.org, benjamin.tissoires@redhat.com, hdegoede@redhat.com, ivan.orlov0322@gmail.com, heikki.krogerus@linux.intel.com Cc: linux-usb@vger.kernel.org, linux-imx@nxp.com, jun.li@nxp.com, stern@rowland.harvard.edu Subject: [PATCH v3 1/2] usb: roles: fix NULL pointer issue when put module's reference Date: Mon, 29 Jan 2024 17:37:38 +0800 Message-Id: <20240129093739.2371530-1-xu.yang_2@nxp.com> X-Mailer: git-send-email 2.34.1 X-ClientProxiedBy: SG2PR01CA0119.apcprd01.prod.exchangelabs.com (2603:1096:4:40::23) To DU2PR04MB8822.eurprd04.prod.outlook.com (2603:10a6:10:2e1::11) Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU2PR04MB8822:EE_|PAXPR04MB8927:EE_ X-MS-Office365-Filtering-Correlation-Id: 397966c6-224c-4a87-83f0-08dc20ad069c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EH73HTUMIwT+SqvbFkw33e2iDjLl/lC+L4OczRbMTyMB9yvAq2vL0DpWFRjmfaq6YeI+7b7ry/Esggya87SdE52Dx1VrV5KsTUoqjwOg0Z86xnhqhmz4KPXGvnF/Um3cQqgzwVxkMu/zO6CgGB5tU2JG421BwSvVdqqUka+UeoSHn7OVoY3zzI3D5Z5yeZpjEA4QaHLwFAe0EeQrTeKB/MiEK2k1k6+BWEI4V7/R19cPhyVJt6Z2SM2H40tUv6PHYi0m2j6A8u6fsE+kJD/1JVyUmU+NZSDGPw+ftcN3lXeCfvMfZTdtlwUQL8rifr4EhKXZcFcbVvzJeNLpz2NmuWPjc2q9d4oWblPmWcwc40noPYU2B7qYgrLPVkqTQuGQhnSDP48x4yLWK8m/21hhs0OY8xE4fXQLfO8F4KsFzLyMufKb03fCktPrCslk/3bLg76Co+47TtAdinoHLKeEm3ebnyd/WBKkuI194jGBAsRxCn3QeZwkad+KfWXcWKG+G4rzOSLvBQBGGdBcE2gR6XTBUf0TEw+Y32gsuDdQG2XIn5deWPrSZuZKFrQguG0Yifrw1gJ1mR+xnx1BrIbdbRGq9hojlp8kX+M2bWhzkxA= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DU2PR04MB8822.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(346002)(136003)(366004)(39860400002)(376002)(396003)(230922051799003)(451199024)(1800799012)(64100799003)(186009)(1076003)(26005)(83380400001)(52116002)(6666004)(6506007)(6512007)(38100700002)(5660300002)(8676002)(41300700001)(4326008)(2906002)(478600001)(8936002)(6486002)(316002)(66556008)(66476007)(66946007)(86362001)(36756003)(38350700005)(2616005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: sMRxW1Nia6gjIRxqChu4d4CsNrhsq6+pS/bWsmXJBRa4IfDzUeFmSnazQBj1/T3W21BOXyDOJmHnXx7W2v+/nUw/zCtii38lnnSe4bQGbCXOqP6qoNQWpNugRxrxf8DiRKf8+bEBKeP82tXu3VO4KjbR4czSSKIRVwIAbnM9gzOGZzJH6S/jBINcbCrUp2iEb/0Dtt+vIRKlVqBL6z/FT5AvaCKvjrgEazyXMrHHOJ2R+mHxeJO8UtGxj1UYtPZVd7epR7UE0g/lNhBDlSRo1imSuM5uhuOnWBqW3Yc8Uv5D3GIuXG8Zh8apXnRml96PcgeVxkaCw+a/uVxZleExietztnMrm3ox2rHsdnT+75A+7Ud3SePzxOr91BPFrEsxYDp+lxs0MDHzdV2BqusyJAlSNj26NLH9uH7/XT0mPKaTpxo9tO13upNfjka61BSSONZx8oKI5uwNRxZ3eMTDgOqZ1TnCQV9AmjPrJrZUMsuBmDLAV4vNCE00FhSwTiGiwOeMmFdgRak5kOMDKr1itSri/XKa/qTu00jqjKLMcT7wJ//n8xX4beg5WbD6oVfjCkug9TYJkpxNvgzDlvct1Nx4LikJQPt6NKdRzJSBHiF7M2LN3I78m5O85sHSeNoVxYuQj//4GTlT/D5vr7G0062qPImO3e1XpBn5d7YxFXlWZCBidgileZLjuZgph6Oy96+Ry4xycUdPauV1gOm4bjdbfJCuwNOTRqQnJo1ZTVd01nLFjmHaFM1t843zUzKWa5Mes6iv6Q4Syuz6OLxfnWk70xJriuK4BSN52PN8qzpbJ0a29wWM2JqVYJHPiT5QO7QB5b+uwyeGrmnYCDJI/I0/0hwBuN1DXKG4qJKrDqSA0opxOcN+IYYUBFP6uc9/oPGxTOYAcNzqifBfbVI8EsG7ljnYf1tLWlHefwpXK0oFE80P90vvKbih+Lyeo9OvvPjjy2b0b7lxzO4kcqoh26IKkrVsuobdzF9435BcEoMorGsWSAdq8+ohEp24ILeVqiBu3XB2a1O35BVDnQkTvZXlAG0AhiHLFQ0XoWllviuA/lFbtQAzsUAxu1h6BZKEOhfFajnE9MpE/y14Y/I7/rN42rIdBWe7qaXVAY4/OArftTDnw4PASulLU48/X3R63pl5QxMDhb+bi3YpbsaXz0yGIfsK2osiX//6E4KQHDqMZaKEMlwyjYxFTtMclEzwGa4QvrpT/Yglze7vPUCqZIH6C7XJNYJWfHiNdT+4R41fLTAufkD13oBLKxwtlK6W6+bg6UqtwFf6xo3N1LN8mSJRjvGTEm5q1JEwtu6kikvyhuVFfG2mxN/GA6TZchcQZtJJh4mZhuabBdqES4BXUct9+WTpup02xfMGvyU7tFfYu+OWf/TcjbDd0u5iElqoYD1wH0IOmm0jISVV8fF7SdQpPChpKWMyMdokAorQXWomXOAi8QENbvVJFfKys/Ge508NrSdBYz3frIXp4Q5sBeLU1ulAesy9pz0WR3bfNZ1P/OCpkNFZefwjCVlTpaHxOikSCiTnxIUQpUjlETpvIbR8YTRKoXIQ3sVioH5+0kISiAi7nRPrsdxcfQUQCKQg X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 397966c6-224c-4a87-83f0-08dc20ad069c X-MS-Exchange-CrossTenant-AuthSource: DU2PR04MB8822.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Jan 2024 09:31:09.3966 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: UTD9H5C6Q2wMwRXgRsYjbPl+tdELSqGRuz3dyb1ukYzLwWtw+lm7i4VmAcogpEG4NAjT3Ou/E9yXYNM9le08Kw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXPR04MB8927 In current design, usb role class driver will get usb_role_switch parent's module reference after the user get usb_role_switch device and put the reference after the user put the usb_role_switch device. However, the parent device of usb_role_switch may be removed before the user put the usb_role_switch. If so, then, NULL pointer issue will be met when the user put the parent module's reference. This will save the module pointer in structure of usb_role_switch. Then, we don't need to find module by iterating long relations. Fixes: 5c54fcac9a9d ("usb: roles: Take care of driver module reference counting") cc: Signed-off-by: Xu Yang Acked-by: Heikki Krogerus --- Changes in v2: - save module pointer as a member of usb_role_switch as suggested by Alan Changes in v3: - no changes --- drivers/usb/roles/class.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c index ae41578bd014..2bad038fb9ad 100644 --- a/drivers/usb/roles/class.c +++ b/drivers/usb/roles/class.c @@ -21,6 +21,7 @@ static const struct class role_class = { struct usb_role_switch { struct device dev; struct mutex lock; /* device lock*/ + struct module *module; /* the module this device depends on */ enum usb_role role; /* From descriptor */ @@ -135,7 +136,7 @@ struct usb_role_switch *usb_role_switch_get(struct device *dev) usb_role_switch_match); if (!IS_ERR_OR_NULL(sw)) - WARN_ON(!try_module_get(sw->dev.parent->driver->owner)); + WARN_ON(!try_module_get(sw->module)); return sw; } @@ -157,7 +158,7 @@ struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode) sw = fwnode_connection_find_match(fwnode, "usb-role-switch", NULL, usb_role_switch_match); if (!IS_ERR_OR_NULL(sw)) - WARN_ON(!try_module_get(sw->dev.parent->driver->owner)); + WARN_ON(!try_module_get(sw->module)); return sw; } @@ -172,7 +173,7 @@ EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get); void usb_role_switch_put(struct usb_role_switch *sw) { if (!IS_ERR_OR_NULL(sw)) { - module_put(sw->dev.parent->driver->owner); + module_put(sw->module); put_device(&sw->dev); } } @@ -189,15 +190,18 @@ struct usb_role_switch * usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode) { struct device *dev; + struct usb_role_switch *sw = NULL; if (!fwnode) return NULL; dev = class_find_device_by_fwnode(&role_class, fwnode); - if (dev) - WARN_ON(!try_module_get(dev->parent->driver->owner)); + if (dev) { + sw = to_role_switch(dev); + WARN_ON(!try_module_get(sw->module)); + } - return dev ? to_role_switch(dev) : NULL; + return sw; } EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode); @@ -338,6 +342,7 @@ usb_role_switch_register(struct device *parent, sw->set = desc->set; sw->get = desc->get; + sw->module = parent->driver->owner; sw->dev.parent = parent; sw->dev.fwnode = desc->fwnode; sw->dev.class = &role_class; From patchwork Mon Jan 29 09:37:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xu Yang X-Patchwork-Id: 13535312 Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03on2076.outbound.protection.outlook.com [40.107.104.76]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE0D856B67 for ; Mon, 29 Jan 2024 09:31:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.104.76 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706520678; cv=fail; b=NCYpBOuwaTmjYKvySjw1ULZnxb7RTeguwGWvXlAbP1FCvMExQyY3vh0o0YHwE+uLcw22gyPn6nLcs1AKsMUSU/uuYRve1pw6taVIkARhO/MRs6D3RShy2RaoXCgMikiTAi/OwotQyWiyusPO3jwODxlFDrXbL5FSze7+q/scMic= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706520678; c=relaxed/simple; bh=EQiP55PNAY5d/5UnqZD8Yu8ZCLyvPOKQ/TkoG+QHOVU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=ctWbCytXRYfrL9XRng/iInGcFBIKFWoKhMycrTM/1pfHuF2EI8aonLOJR4AVCYeKvSUgwBEOEDi8bpFJv3uQiGdibgSLydgMfFDQLqg8K4og+JIcuG+vgKtrusZ07C2POL4Kvy2q4KiNj0QSIF4zl5pbxBkzVRFPDN2VV4VDJuQ= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b=Un2Hn0v3; arc=fail smtp.client-ip=40.107.104.76 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nxp.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=nxp.com header.i=@nxp.com header.b="Un2Hn0v3" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RrqBpIDjmS7olOx4aQ1H4d2S+cJ9S35zd3h2WW/exEDKPEgSMce2qTRaTD36QCaF3lnpYNQEQGvM00qr8aHYsOwJGaamDFtONGqP60obyhcOaBYqTpb9MR8fclPIpsHv2MdX5F3pb1f6/s45SzCsWr23rZKRfZSSeLEirlrfnFRo3uI5jwnOLFsakQqa3GI7asUHuKQrPJSCamuT906jQVxyDpAEEfva4+hcbc183pGnYYzFwTjVngqDi7A2f23SLAZU8Xc9hn6O6NLOKavG+ga9mRGclOta1v0cxnydh+chV5QpLDg8K3bzkIYSJMK8yCVBQgWRAw61v7XqYQ9L3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=3Oewcl+WXESe4ZEBxZCinwnNIqRJSuRxZ56IVB4iqAc=; b=fHdpt+3Lyo7eC3jHQHpvK8T5bduZoIYUIvSXQtK9EX37HoOkBLEH421aE80fkzBsQYSIgHEjP2RMWmMG/9QuEfsEW0P1VeJb4I+8tZ127U+mFEZs/dCzsnE+ybrEuI3BWhl7pb2n+aHbQudPALIL/IWYA+P8qEVxNOOs1b+n29XOvNbRZjVZY76wTeKkWATWmGNCB2sFKN7F5/2qJ6leq84ZnmGHrViPIgBMWOJkoykW3nuoY+3LoDzib8oBZaW2+bnyH1Lp87CXiL9UwX4uV+qp+N8Zwzu5Pgs/9jWRWpGoxEpD9XY+fudgB3IewOCOmDeiQCGqZBTOqH+Ov6lvKg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3Oewcl+WXESe4ZEBxZCinwnNIqRJSuRxZ56IVB4iqAc=; b=Un2Hn0v3JTUFfGV2YPZD10I/llxzW7zIcBgB5Yh7ODQ+Jb1PGi1BEcGiJj1aFEVFQDWIZ+9KJCysBPTWzRTkXRzOUCdr/ZKE/ZPEnphGdmjxtZ6kaWJ97MMDkYEClQWpQcVAx8Z/Tj/3QMNNoh9xeYD6rQ6bc/SF5dmAz6YKQ1c= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from DU2PR04MB8822.eurprd04.prod.outlook.com (2603:10a6:10:2e1::11) by PAXPR04MB8927.eurprd04.prod.outlook.com (2603:10a6:102:20e::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Mon, 29 Jan 2024 09:31:12 +0000 Received: from DU2PR04MB8822.eurprd04.prod.outlook.com ([fe80::d45f:4483:c11:68b0]) by DU2PR04MB8822.eurprd04.prod.outlook.com ([fe80::d45f:4483:c11:68b0%7]) with mapi id 15.20.7228.029; Mon, 29 Jan 2024 09:31:12 +0000 From: Xu Yang To: gregkh@linuxfoundation.org, benjamin.tissoires@redhat.com, hdegoede@redhat.com, ivan.orlov0322@gmail.com, heikki.krogerus@linux.intel.com Cc: linux-usb@vger.kernel.org, linux-imx@nxp.com, jun.li@nxp.com, stern@rowland.harvard.edu Subject: [PATCH v3 2/2] usb: roles: don't get/set_role() when usb_role_switch is unregistered Date: Mon, 29 Jan 2024 17:37:39 +0800 Message-Id: <20240129093739.2371530-2-xu.yang_2@nxp.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240129093739.2371530-1-xu.yang_2@nxp.com> References: <20240129093739.2371530-1-xu.yang_2@nxp.com> X-ClientProxiedBy: SG2PR01CA0119.apcprd01.prod.exchangelabs.com (2603:1096:4:40::23) To DU2PR04MB8822.eurprd04.prod.outlook.com (2603:10a6:10:2e1::11) Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU2PR04MB8822:EE_|PAXPR04MB8927:EE_ X-MS-Office365-Filtering-Correlation-Id: f9c659a1-c2d9-4bf1-cc34-08dc20ad08a3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6HgufpZ+/3EEgbicgFXmM8Sav48NlwU6zT3oVaMpZ4yurYSu881Zwfymr+HjLiVi12Ve5LrNwiOhwRSFf1ZIZWQhT1E6BU393izlxihPS/QaVBee8phLWEnx/Ikj9mnh1/r72EmgoOWvfM45sgYh/GrlvZktZ72EPa3OJTCH4NXfMeRbIPjmhiDyvcPL905gat5d8lOX9jq79LCbt4pMzp+CTc5x/hCcR9U1OEz8OBJbAXpCuJyWDYjqiRcObGwxcxcSlP5a4+p0uRfrMh0UpcY06fBSq4acgmD+rNYFUkC1mB+G3CE7YfnmwoVz1JcLc53Cm86nqqarQeOKUueb5i5c2a1ZFXGnCK7ColNNhXrNP1fuKtXwUmLk+YrMWUmZc6/z/P6+/+Bh2ALPsJxLInEzW5mXaKSWpmFgRfEhpTshISyOsxQkldipeGle1MHaw4+X2cQwN6vlUwtqSF1jrGmgoqsglf5WHYx15TmvwZ1wGnm8ORmW5ZZQasWRy7sIBo492TUeRUZQ7Tzz3atrPq/QuoDL0LfxGag/x8l9LqB7bfBtmJKoku+q//br5TJ4kp8TY5tJimKJA0IOmEjUUfrJytL+7oYDATcj/igz5mBU7/st+MOYbM0iR4cKtHh/ X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DU2PR04MB8822.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(346002)(136003)(366004)(39860400002)(376002)(396003)(230922051799003)(451199024)(1800799012)(64100799003)(186009)(1076003)(26005)(83380400001)(52116002)(6666004)(6506007)(6512007)(38100700002)(5660300002)(8676002)(41300700001)(4326008)(2906002)(478600001)(8936002)(6486002)(316002)(66556008)(66476007)(66946007)(86362001)(36756003)(38350700005)(2616005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: SG1DRTHjlB3rORTd+W92Y754Z7Tm4/SClp0TuyVFsHvWVqniKXaY+k+uzsrDPCDPdBxasgVHByAlHX8bAezkNnJzVp6h/PtF6ow7P8GY+Fob64QrGfmjiRqzAmB3Bk1UEsjPE4oGCxKoJpvMp346Fq3220eeUeE5b9OhG88ABd1dnFaFIG9asRD/CeWMrpu3l+Nebx9Iex+peF0afNai8731gbFBddGHHNGFmfYgiHqO9C48SBwimCPvEBDTWraVIyKrifKVDQGXtZHHuVdB6xS6AX6JPAtrc0xVB55otrpyErq78enJQvMzfOv1XuZ3ExlhDLnGCGmzBX9SsgpUjKlqoehxJbDHzj9UyNy4KTy5rVTF9fe6PCGbRnC+gFSsH8SOndx3cWAhIPPpOtpceW+q39evD8+7blAK9OQzSPKwq5qGG7RBkjqF6WSYXz07V+4dzRMGKilr+NxVyfGEA2dpxg2rrOneWK4y6qy6BYb9hJCj0TsxbemKXcZL+J3Z+6phyfBTbCJPBnh4ZOA2d+HY22GMP8yQczP8z/hynPW7l5pu16rW6le5avAy/ZrmbepHXnfaO73Rt3GPkowvai6VYyg9GmxN6wf15zd9bEnTYbbZJ6GEATEsP9LKGFAJX71aZ/UvhuvxlS5r/y0rxcorQaDAlXbejnnGONGDjWj+SqbVdRAzBDRliZXNdD71kVDWI9KbWMCgkZL2/aEvc9eXk4BSSlDoU+KM9uHOEG50qcIV+xLCYIyXZ+SbRRGIcZhgeyJSCOzbnSl/xgfHKcYlN8vh0u5QzcrTwEcvO/n4uVSmP64t9CnarjCYQcjzhNZrWZnUhGyd8q1rP64Wfn1NzDoySRbY79gDdUez9RufgxNwaLkbhDE63Y9iGAqGmZ/pdcs7Bdb/fiHo0dhwmGRUSC7Axf2dBOtZS/KxPqtmKnPca7azqxoBlmBGAue7nOkmxdM7PUC8j9VKTs1R2pI0Nt8kAVh/hESNpD+2iZ4tNqFl1uKJGyIylLBqpU4DAUeVQGmmgFc4/Yzkgibc3d1wP0fcKy53Tw6+xeFGoCPkSZfgjLOnV7DE8f3owhLmQV5UvBK7t+b/4gfYW+51T6PIBaxtzHtn1cdWW60wq0K5Rn1OFjOlrgbWbzK11UJ+Cm2FIxvHtd//D39r9VKGLPsfiphUgn2uJLc+4JWMysC0chgNQCB7ur2Lo7xLkuqS4Zsbey4MmQCPC0wLx+UxVtpIN4fuKj2w1dLgnpymu3jb77PwotaS4I5TCYQwzQaVLhbleIQ6OnQUciN09YYPGn6avVPRCpuzJb8Xkz5T5sxjVrpcb3vw1E36feg/0hNTg3d7NoMfpJb898URopUvfmgQ/13eo0VoEhtmrLWR+w15Q1PoPQ6Jnq2Pfa/uIO0n2oWX2RUdfDdhz5g/qdw5jEsqnvURlTRUZlpSpMpkIM0f2ywioEXZJ3rfXjWH+VsMpXEBeYIACYmVBfX9zS0VyfJiHdqdxt4XQA89ncZOjrU5qHTlwToFU2JAt+AVD5KB9M3lxaxwtyTC78KVbCwfdJHYe8TQ9RnbdLHhNgNSz/CABCuzOm18y2rpsMnRwKST X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: f9c659a1-c2d9-4bf1-cc34-08dc20ad08a3 X-MS-Exchange-CrossTenant-AuthSource: DU2PR04MB8822.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Jan 2024 09:31:12.8502 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 69PqoDmSEO0YUoo+YLK+HTfoGpb2lLRKQi8L2X6Ew4RBxsgDgpWQJHqMceFSkuZHZmuyJfY1yXk1+yqOMNBRfQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXPR04MB8927 There is a possibility that usb_role_switch device is unregistered before the user put usb_role_switch. In this case, the user may still want to get/set_role() since the user can't sense the changes of usb_role_switch. This will add a flag to show if usb_role_switch is already registered and avoid unwanted behaviors. Fixes: fde0aa6c175a ("usb: common: Small class for USB role switches") cc: Signed-off-by: Xu Yang Acked-by: Heikki Krogerus --- Changes in v2: - new patch during test patch 1 - add registered flag Changes in v3: - add fix tag and stable list --- drivers/usb/roles/class.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c index 2bad038fb9ad..70165dd86b5d 100644 --- a/drivers/usb/roles/class.c +++ b/drivers/usb/roles/class.c @@ -23,6 +23,7 @@ struct usb_role_switch { struct mutex lock; /* device lock*/ struct module *module; /* the module this device depends on */ enum usb_role role; + bool registered; /* From descriptor */ struct device *usb2_port; @@ -49,6 +50,9 @@ int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role) if (IS_ERR_OR_NULL(sw)) return 0; + if (!sw->registered) + return -EOPNOTSUPP; + mutex_lock(&sw->lock); ret = sw->set(sw, role); @@ -74,7 +78,7 @@ enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw) { enum usb_role role; - if (IS_ERR_OR_NULL(sw)) + if (IS_ERR_OR_NULL(sw) || !sw->registered) return USB_ROLE_NONE; mutex_lock(&sw->lock); @@ -357,6 +361,8 @@ usb_role_switch_register(struct device *parent, return ERR_PTR(ret); } + sw->registered = true; + /* TODO: Symlinks for the host port and the device controller. */ return sw; @@ -371,8 +377,10 @@ EXPORT_SYMBOL_GPL(usb_role_switch_register); */ void usb_role_switch_unregister(struct usb_role_switch *sw) { - if (!IS_ERR_OR_NULL(sw)) + if (!IS_ERR_OR_NULL(sw)) { + sw->registered = false; device_unregister(&sw->dev); + } } EXPORT_SYMBOL_GPL(usb_role_switch_unregister);