From patchwork Wed Sep 25 06:29:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?6IOh6L+e5Yuk?= X-Patchwork-Id: 13811637 Received: from APC01-PSA-obe.outbound.protection.outlook.com (mail-psaapc01on2058.outbound.protection.outlook.com [40.107.255.58]) (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 BA2A2647; Wed, 25 Sep 2024 06:29:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.255.58 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727245778; cv=fail; b=YPSWavVE7do/KPmw2NL6aOqjUwuEmcKEDVAwA5R84xaJB4x2HpTdGGos8sw9ZcvJIHZwmdjC9gtZuXw1/uQ63gsh61LmRMMK8qEfQw1147GxDJeR8LtoY5UeFAMwdUMtQMUi5Dy7RFMpVnZj9dpdPtCGTI4Ege6y6ZqH5+tk1gg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727245778; c=relaxed/simple; bh=fu2GtYlzTZpbvC7g+6qD7+bnie36ivONJ+qbfgkc+Z0=; h=From:To:CC:Subject:Date:Message-ID:Content-Type:MIME-Version; b=uo1sBfx4n6hCRl/udVzOiS2c3USYM1R0ddr38ZoE/X9pAB3tSy2OXiyJfAiUyyBCItYbpvGUqqXQxFG7LglE0LoJLcmB7gFrvjWdPJLtGlABSO3UIedMidXgRc8z079XkWtFw1CBGC+11tc7LfdYuQZ2zt8+HE/iAo7ReNykzNI= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=vivo.com; spf=pass smtp.mailfrom=vivo.com; dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com header.b=lc6NUfI6; arc=fail smtp.client-ip=40.107.255.58 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=vivo.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=vivo.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com header.b="lc6NUfI6" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=O5XYQ18ZCHXXrXtSzC/Z6lbLLnZKLZVY5nel3OHqqsv3MrGL7DU/9vJzT7xQHFDWXKUHHE4jsC9qnFlmtrnfDKmLAUh/fiWNF61xH0wjZetwC7iIJoi2sGjg6lvBbPsXTHwkMNVb9hSTwBaIcTSDXEk+ivdfc53Ems+yX5MPHSX3H8CLby8iUgMv54CQowSD6hwBzy5OKVnoIlQKjDKYAtFeBx7lzylQEAXcfof0s+gZEWE50oATOFZmulfP28X357e3P4eYw601wfHhSao8W2w0y5e3bLV0MHgqa4OL+/6SPC1uzXPjlxcBZwZNW/BunbqKJ3gDnqtoMTtlKWWQHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=fu2GtYlzTZpbvC7g+6qD7+bnie36ivONJ+qbfgkc+Z0=; b=gsZp7+W7N7PhJfFs+61H2+ipbJXwfeMfQAeRzW4k1QGuBznYhlJRwVUBVaHc06mA2rIRNLHQZxR3NwcpNUKnPxhFsYJMeLO0KPJAHUu09bztFb+EiL4ytJhuwzWHUf8R79wKe0Sy//rPkHew+R/BwhMUFK+mpGN9IcMQ7jKJEjReSSIqfvjOdtnBw0jNkfbWC1IMbvcUny8B+aIQehhdNFcFX4/RM6BoDRqvlJB9/SmwptwivfqoNoidLFHWklaY6L95PwQRQ7pE0WVdA5PBqLDBuoyHfmK38yhOcSsqffge8u3ZmdI6zhPcD9camT4dsyzS4c/SM6WGJSkhlNYAYg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=vivo.com; dmarc=pass action=none header.from=vivo.com; dkim=pass header.d=vivo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vivo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=fu2GtYlzTZpbvC7g+6qD7+bnie36ivONJ+qbfgkc+Z0=; b=lc6NUfI6VA3/3ycN3SCYdUSa9E7Qg+RzuZ5/V+/XquFzJt+ohC8AaljddLvxsR63t22CcHHMfWEODos7zGidu0A82PDKHe70qz2zFlRXB+av6K3FPbjwAOPxfcaMLwO0jBptaYEEoMCBx7RF/TNI1ePdWzMNa942sTak1J8rHewCoaL/qQojiODwX4wvz5sEp56E6cQKhJ2/BKGuvCTZWsZku2cVj2Svmiowfbbh9rD264An+Df/HzA5/e7C6TsrAny4n4Xkh7J5cRR7t6K+mmPIAasMQB8cvBw+BQ1E76E0e46CSPystyBx96tVr/iloNRi8gvg5aPHrSnVA8IzRA== Received: from TYUPR06MB6217.apcprd06.prod.outlook.com (2603:1096:400:358::7) by TYZPR06MB6936.apcprd06.prod.outlook.com (2603:1096:405:42::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7982.28; Wed, 25 Sep 2024 06:29:32 +0000 Received: from TYUPR06MB6217.apcprd06.prod.outlook.com ([fe80::c18d:f7c6:7590:64fe]) by TYUPR06MB6217.apcprd06.prod.outlook.com ([fe80::c18d:f7c6:7590:64fe%5]) with mapi id 15.20.7982.022; Wed, 25 Sep 2024 06:29:32 +0000 From: =?utf-8?b?6IOh6L+e5Yuk?= To: "gregkh@linuxfoundation.org" CC: Michael Nazzareno Trimarchi , Prashanth K , "quic_jjohnson@quicinc.com" , "linux-usb@vger.kernel.org" , "linux-kernel@vger.kernel.org" , opensource.kernel , "akpm@linux-foundation.org" Subject: =?utf-8?q?=E7=AD=94=E5=A4=8D=3A_=5BPATCH_v9=5D_usb=3A_gadget=3A_u?= =?utf-8?q?=5Fserial=3A_Add_null_pointer_check_in_gs=5Fread=5Fcomplete_=26_g?= =?utf-8?q?s=5Fwrite=5Fcomplete?= Thread-Topic: [PATCH v9] usb: gadget: u_serial: Add null pointer check in gs_read_complete & gs_write_complete Thread-Index: AdsPE1bYjUtvsR7DQN+/HxlvWWVxXw== Date: Wed, 25 Sep 2024 06:29:32 +0000 Message-ID: Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=vivo.com; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: TYUPR06MB6217:EE_|TYZPR06MB6936:EE_ x-ms-office365-filtering-correlation-id: f02a2ad8-aaca-4d9e-3121-08dcdd2b6adf x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0;ARA:13230040|376014|1800799024|366016|38070700018; x-microsoft-antispam-message-info: =?utf-8?q?4R36xSGukGTFWsuH/H2SA8DQjkM3D4U?= =?utf-8?q?DV4yjqPcEQF0o9lFQh9gO7IYElP5J+K4y89K4aiy3d3L6hqIVGFuOhGPwcBh98rNQ?= =?utf-8?q?n14An3qQWN61NMKjHGHRDCvhYT66/I0xWisEqzfdgpKOqGE95LbF1OsmUXsTvc1OP?= =?utf-8?q?2wh1jmYoAb87alQevgshoQo/UAzCDKVcdfUonWwXWXds3i2bN90MHNyJjl+9AaCZ5?= =?utf-8?q?h/nMTgZ3lx34Y6agxi60qB3/4hOC3JEwm62pziEEWJGcdpaLvy15Ge2f5xx/2KSPc?= =?utf-8?q?vLHBUXxIMClKUgr9gkW4ySfWENxANHpcIe09W7KFFh8NK8P+7TEG+7G1GGFCOf9zT?= =?utf-8?q?McsqlVVNk05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QR+Ys3GJLQDdmUjEgSsA/cbmqLHDoIDYprKBf71yxY7gHoh2WhQC?= =?utf-8?q?OOWlZbxWpRvQUbppwj36PQCyhtoAk5cDTcXnwyXp6aIbcxh+/1tB26bpAa1/Fmn1u?= =?utf-8?q?60HBnuqM5SWmgIqSY569hQ7vQ5nfRG2PSA=3D=3D?= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:zh-cn;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:TYUPR06MB6217.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(1800799024)(366016)(38070700018);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?eqofmrxQ8aMrWlmi3S0EUKPi0nOC?= =?utf-8?q?VvqPsMhXiF2HlMK/hRLYwgsYKwdLnwL+KABi/zLuBp7jyvj2N5O7nrCzkn9zf4OJL?= =?utf-8?q?7/vVhZo5QQfdXFbWzIi2wyuQXrDkXGEOXdO7Tb/rn8ehpgz8UN/VeGqAyacsbKaOL?= =?utf-8?q?uwQZd9KROvFlWDBPOs2SmBRNAkrW2gBgPIeLfee6orjh0Q4ocy8ybs2aEAtvxvQUJ?= =?utf-8?q?U6mXJ4FIcS0JyjHWRLNeQz7hFv1wYs+wDEuBJK1ciEdenLlF/fc7RZ8sPdmVWhdBX?= =?utf-8?q?qeYlGt6sp/+0yxhiZs3bEdgWIVNw7PfP4XTUjEi7pYJon4PGxBxlpDUmMZt/yijKF?= =?utf-8?q?1LL/ekqZ5EgII5Zr9MaRbm+K3/hPWZLWtEJvtmikx1a5tz1LqeWH4wkDhZ8XtlFGE?= =?utf-8?q?txdUcTW1DeLMbTD6dk86xaMdzcP3Poojn3nSDB5oaJL2CyOGgb/FfinI6MmBkqxIV?= =?utf-8?q?h1IuMbVHgG8mAz3Eigln6utJRhy+mPDbpk6W5i4Q88iWioVIeThp4QS8XpG6wVw4+?= =?utf-8?q?V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?BCV0cDgHRYg6IibCreHXihgBEaUSDIF8hm0DXX2zUBtLUpszqAofTEzWiVCWOIgw6?= =?utf-8?q?0onz8Vb+Gb4nczBJgoH/Rkn1DcBiLNfgmJUopO81lLTAVfjnyZKkAbXE=3D?= Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: TYUPR06MB6217.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: f02a2ad8-aaca-4d9e-3121-08dcdd2b6adf X-MS-Exchange-CrossTenant-originalarrivaltime: 25 Sep 2024 06:29:32.6040 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 923e42dc-48d5-4cbe-b582-1a797a6412ed X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: nCfsJBWzVw6fyPIL9bAkKlQwnXBjQHT9iN3XKtjJ9pqxb+gwkxP9h4BFbk9iJ2hmpH/x/uhfuUjhBFxFUyPucg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYZPR06MB6936 Hello linux community expert: >> >> From: Lianqin Hu >> >> >> >> Considering that in some extreme cases, when the unbind operation >> >> is being executed, gserial_disconnect has already cleared >> >> gser->ioport, triggering a gadget reconfiguration at this time and >> >> gs_read_complete gets called afterwards, which results in accessing >> >> null pointer, add a null pointer check to prevent this situation. >> >> >> >> Added a static spinlock to prevent gser->ioport from becoming null >> >> after the newly added check. >> >> >In looking at this further, shouldn't we just be moving around where we call usb_ep_disable() in gserial_disconnect()? >> >> >Can't we shut down the endpoints _BEFORE_ we set the port structures to NULL? After the endpoints are stopped, then we know that there is no outstanding i/o possible and then we can clean up properly? >> >>> >Not to say that your change doesn't fix the race condition here, but cleaning up things in the proper order might be the better way as then there can not be any race conditions at all? >> >> >Have you tried that? >> >> Thank you for your guidance, we studied and tried. >I'm confused, you tried what and what happened? After this modification, our stress test problem no longer recurs.( I'm sorry for taking so long to reply. There have been many urgent issues in the project recently and I have invested a lot of time to support project issues.) Thanks diff --git a/drivers/usb/gadget/function/u_serial.c b/drivers/usb/gadget/function/u_serial.c index b394105e55d6..1712e9cd08be 100644 --- a/drivers/usb/gadget/function/u_serial.c +++ b/drivers/usb/gadget/function/u_serial.c @@ -1395,6 +1395,10 @@ void gserial_disconnect(struct gserial *gser) /* REVISIT as above: how best to track this? */ port->port_line_coding = gser->port_line_coding; + /* disable endpoints, aborting down any active I/O */ + usb_ep_disable(gser->out); + usb_ep_disable(gser->in); + port->port_usb = NULL; gser->ioport = NULL; if (port->port.count > 0) { @@ -1406,10 +1410,6 @@ void gserial_disconnect(struct gserial *gser) spin_unlock(&port->port_lock); spin_unlock_irqrestore(&serial_port_lock, flags); - /* disable endpoints, aborting down any active I/O */ - usb_ep_disable(gser->out); - usb_ep_disable(gser->in); - /* finally, free any unused/unusable I/O buffers */ spin_lock_irqsave(&port->port_lock, flags); if (port->port.count == 0)