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 AD09F13EFF4; Tue, 27 Feb 2024 14:05:37 +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=1709042737; cv=none; b=T2oKsE/DSohq8l1YGlTAEK0B5HhJ/KEh6VNsmhKSFkA8im/SSq4f3ftp9axnR/VjC9j67cc+DQnpsfT5KI52DQR89pFgM8ZqImGKB7OhKuLkQPLXliVlrL2ogeHPo61JBSUw16VV6DG5maYCYS+SBnFiV3x2vu1fxoMOpcPVxbk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709042737; c=relaxed/simple; bh=3d6PoKnh8bDWHqOIuHDSjo9GZIzsvQKoKdFai2LZAAk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Grfbq80HR0rCEwT6ekdIF4kejkjOK4sUoVv3l6YimCSUZF21+6zZPPgcsFVaJeaZUnJSM4R11B6F3RJt1ayEIeWN6qf0AUaXwsMj0kDYqkqeUR1sMPo6h1+LSk1t0BpWg38tpfevg/p3FAu3OXjKLtyifg4u2acGGavBtcGI68Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=wAReddFt; 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="wAReddFt" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3C850C433C7; Tue, 27 Feb 2024 14:05:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1709042737; bh=3d6PoKnh8bDWHqOIuHDSjo9GZIzsvQKoKdFai2LZAAk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wAReddFtGWbRa9uhpN5LbCkv7HIrYWf12VHZ6SYH1rEgDewmHpNbqz21iLk4kYlDX F9zMMcePDz7DER8zqqoiroR8IqEIZwS9htjmhupfISGq27UL4Om2HbFcXTJRz6oZxm +MWmaCsHYmVTGCsq0JD6mMyK8r70etz6ShVq3sL0= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Xu Yang , Heikki Krogerus Subject: [PATCH 5.15 103/245] usb: roles: dont get/set_role() when usb_role_switch is unregistered Date: Tue, 27 Feb 2024 14:24:51 +0100 Message-ID: <20240227131618.560048259@linuxfoundation.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240227131615.098467438@linuxfoundation.org> References: <20240227131615.098467438@linuxfoundation.org> User-Agent: quilt/0.67 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.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Xu Yang commit b787a3e781759026a6212736ef8e52cf83d1821a upstream. 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: stable@vger.kernel.org Signed-off-by: Xu Yang Acked-by: Heikki Krogerus Link: https://lore.kernel.org/r/20240129093739.2371530-2-xu.yang_2@nxp.com Signed-off-by: Greg Kroah-Hartman --- drivers/usb/roles/class.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) --- a/drivers/usb/roles/class.c +++ b/drivers/usb/roles/class.c @@ -21,6 +21,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; @@ -47,6 +48,9 @@ int usb_role_switch_set_role(struct usb_ if (IS_ERR_OR_NULL(sw)) return 0; + if (!sw->registered) + return -EOPNOTSUPP; + mutex_lock(&sw->lock); ret = sw->set(sw, role); @@ -72,7 +76,7 @@ enum usb_role usb_role_switch_get_role(s { 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); @@ -356,6 +360,8 @@ usb_role_switch_register(struct device * return ERR_PTR(ret); } + sw->registered = true; + /* TODO: Symlinks for the host port and the device controller. */ return sw; @@ -370,8 +376,10 @@ EXPORT_SYMBOL_GPL(usb_role_switch_regist */ 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);