From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 ECA3425C715; Tue, 11 Mar 2025 15:10:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741705855; cv=none; b=h6cPiMlYmfpTv8y/LHS4pujexmjXXnD+mPfEwjXDSbGh8gZnWn+hooFrpAxU0AKLXE8hmQ6TnI3IeiH1wotGP7p0wIc4DFoWnWkhEWUDcE6xuVmRFVUjKbwPA+gGmaESOtRBucOs9cdhCVSUStQwh7aCFHaqpGKZPSw1KEVyOlM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741705855; c=relaxed/simple; bh=ROSh81onf8aYwHrMx4a82HCllqTmm2H6SHZKk5ozhaY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T9geHjU/JlRqs96ztLVpZLTrYk5zZUeqTNqf5VUZfYyYwwlILT2k7FwCCTcHZMLdcMwrF1Y5+8eJwspI1UazplgcoTukcVlQ9b16Oe3cyaKsO7j5NNlOS/W7gp4I+gA5BkEIx01/MAp+fsx2zm3BurvSbUTrUlH1fWgZNoANzYw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=S/dHXTQN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="S/dHXTQN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7576FC4CEE9; Tue, 11 Mar 2025 15:10:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1741705854; bh=ROSh81onf8aYwHrMx4a82HCllqTmm2H6SHZKk5ozhaY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=S/dHXTQN2I14EiyXo8FbbyLgdbSOWP7ZZccMGvfofPB7JNKLMRG44sO2vjhnCLt1J dY0mpAey3sWADZ4xvov94pSPUMwV3BXwFs+alwhw7S5KAF9eDWcwrEWFVrHuhbk+MR qJQN776C3VLdE0pou58DUf9XQbtCPt+wrRZdkY/I= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, stable , Elson Roy Serrao , Heikki Krogerus Subject: [PATCH 5.4 178/328] usb: roles: set switch registered flag early on Date: Tue, 11 Mar 2025 15:59:08 +0100 Message-ID: <20250311145721.980063204@linuxfoundation.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250311145714.865727435@linuxfoundation.org> References: <20250311145714.865727435@linuxfoundation.org> User-Agent: quilt/0.68 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Elson Roy Serrao commit 634775a752a86784511018a108f3b530cc3399a7 upstream. The role switch registration and set_role() can happen in parallel as they are invoked independent of each other. There is a possibility that a driver might spend significant amount of time in usb_role_switch_register() API due to the presence of time intensive operations like component_add() which operate under common mutex. This leads to a time window after allocating the switch and before setting the registered flag where the set role notifications are dropped. Below timeline summarizes this behavior Thread1 | Thread2 usb_role_switch_register() | | | ---> allocate switch | | | ---> component_add() | usb_role_switch_set_role() | | | | | --> Drop role notifications | | since sw->registered | | flag is not set. | | --->Set registered flag.| To avoid this, set the registered flag early on in the switch register API. Fixes: b787a3e78175 ("usb: roles: don't get/set_role() when usb_role_switch is unregistered") Cc: stable Signed-off-by: Elson Roy Serrao Reviewed-by: Heikki Krogerus Link: https://lore.kernel.org/r/20250206193950.22421-1-quic_eserrao@quicinc.com Signed-off-by: Greg Kroah-Hartman --- drivers/usb/roles/class.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) --- a/drivers/usb/roles/class.c +++ b/drivers/usb/roles/class.c @@ -317,14 +317,15 @@ usb_role_switch_register(struct device * sw->dev.type = &usb_role_dev_type; dev_set_name(&sw->dev, "%s-role-switch", dev_name(parent)); + sw->registered = true; + ret = device_register(&sw->dev); if (ret) { + sw->registered = false; put_device(&sw->dev); return ERR_PTR(ret); } - sw->registered = true; - /* TODO: Symlinks for the host port and the device controller. */ return sw;