From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795035174; b=RuwGkeEi5Mi7jwfdwytbx88h9wxGML00tJqu16BVwpN2nxHw3GWKIPls4ho7luk7J196 f6qxLmyfG7r1Ve6wiUiWy6wTCiimoyjZ1avGpxQwANPUANnnokq1ICJKpNs7wElOEzNhj 7LOTGAofCWkUK2w+F23b4kysjY+Raq29dpeh/2guV7zRB7kPyOxUCdLfTLY8Wg1SIiQYD lr395MfZj9vc2JQvlp6tcP+Cq5qzVMcdqUfoznOkp6M33A9CiRtUQXfEAFjk8+tpBB0dH flFhG53LW7t9S0o5AgCRauLbxK0tm9NG7HYEJMK+HFphrrd8hh2odPu7tQbKqFq+BTQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795035174; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=Vw7N41WO/jQ+dvwO87LabMfAAqGHz8VYgUjw2+H5Ud8=; b=R98fIRCHy1qhxm7DebLY3ZqR6Hr91ZjMg/v4ehu7XO3/W3fhPAb3mhHabxVxcpvW3cQ6 D3my5BjmWSDgp2Lk3TG35oe+C+f0SqmJV9cVTed7Yay20AVHjWsmdj4nVy3G37QGQ8BBJ CivdCWJIgsc3rp6jpn4+elokCyoMWo6PRjOhqAVf/08s3iBvRcoBA3TmvncwR3a6N8VDM Fubgy759QXVKP98pIqChv5PbwGIShAJ5wuqX8nLs1Dz1pvWi1aSXYS0Ak/I7PAjUDkj3I 0dKekU0vk5QgGTRyewzpotXGb4Cpsfj2G6HGw/Y0Ct3JoaRtOaoV2XchR0SZBbdEEYg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=LmLdMnNz; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7B349606A320; Wed, 18 Nov 2026 20:52:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=LmLdMnNz Received: by smtp.kernel.org (Postfix) with ESMTPSA id CBE351F000C4; Wed, 18 Nov 2026 20:52:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795035174; bh=Vw7N41WO/jQ+dvwO87LabMfAAqGHz8VYgUjw2+H5Ud8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=LmLdMnNzFCul775InlvN0nlU4zGODMod2KZI8zgBLXa1iDLAHNHZ8fGX+pfiWIHuR vVs8FHo1qYXsHVvUVXta+YX7wIXC6ZrT1mLFZjkwoh6fIzAoax0cGylgDnqAVsfWyO J5j2pUqZBFniNpUeDMNX/8iMlth3aU05YmFxppsY= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Manivannan Sadhasivam , Loic Poulain , Bryan ODonoghue , Vinod Koul , Oleg Keri Subject: [PATCH 6.6 0630/3318] phy: core: Fix race-condition between _of_phy_get() and try_module_get() Date: Wed, 18 Nov 2026 15:20:52 +0100 Message-ID: <20261118150336.774634403@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118150313.527275416@linuxfoundation.org> References: <20261118150313.527275416@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Bryan O'Donoghue commit 090055ee629523092688da42428558ba56d4272f upstream. Sashiko asked during a patch review if the existing usage pattern had a race condition; specifically in of_phy_get() if it was possible between returning from _of_phy_get() and running try_module_get() that a module might be unbound leading to use-after-free. Looking at the code this appears to be so, there is no linkage between the phy and module under a synchronisation primitive. Using the phy_provider_mutex in phy_get() will ensure there is a link between the returned phy pointer and the module_get() bumping the module reference count. Amend phy_get(), of_phy_get() and devm_of_phy_get_by_index() to fix the same usage pattern. phy_provider_unregister() must take the phy_provider_mutex so amending phy_get()/of_phy_get() to take that same mutex guarantees there is no use-after-free. Fixes: ff764963479a1 ("drivers: phy: add generic PHY framework") Cc: stable@vger.kernel.org Reviewed-by: Manivannan Sadhasivam Reviewed-by: Loic Poulain Tested-by: Oleg Keri # Lenovo Yoga Slim 7x Gen 11 (Glymur) Signed-off-by: Bryan O'Donoghue Link: https://patch.msgid.link/20260918-b4-linux-next-25-03-13-dtsi-x1e80100-camss-v20-1-dc244e124e71@linaro.org Signed-off-by: Vinod Koul Signed-off-by: Greg Kroah-Hartman --- drivers/phy/phy-core.c | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) --- a/drivers/phy/phy-core.c +++ b/drivers/phy/phy-core.c @@ -119,13 +119,13 @@ static struct phy *phy_find(struct devic const char *dev_id = dev_name(dev); struct phy_lookup *p, *pl = NULL; - mutex_lock(&phy_provider_mutex); + lockdep_assert_held(&phy_provider_mutex); + list_for_each_entry(p, &phys, node) if (!strcmp(p->dev_id, dev_id) && !strcmp(p->con_id, con_id)) { pl = p; break; } - mutex_unlock(&phy_provider_mutex); return pl ? pl->phy : ERR_PTR(-ENODEV); } @@ -574,6 +574,8 @@ static struct phy *_of_phy_get(struct de struct phy *phy = NULL; struct of_phandle_args args; + lockdep_assert_held(&phy_provider_mutex); + ret = of_parse_phandle_with_args(np, "phys", "#phy-cells", index, &args); if (ret) @@ -585,11 +587,10 @@ static struct phy *_of_phy_get(struct de goto out_put_node; } - mutex_lock(&phy_provider_mutex); phy_provider = of_phy_provider_lookup(args.np); if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) { phy = ERR_PTR(-EPROBE_DEFER); - goto out_unlock; + goto out_put_node; } if (!of_device_is_available(args.np)) { @@ -603,8 +604,6 @@ static struct phy *_of_phy_get(struct de out_put_module: module_put(phy_provider->owner); -out_unlock: - mutex_unlock(&phy_provider_mutex); out_put_node: of_node_put(args.np); @@ -628,15 +627,21 @@ struct phy *of_phy_get(struct device_nod if (con_id) index = of_property_match_string(np, "phy-names", con_id); + mutex_lock(&phy_provider_mutex); + phy = _of_phy_get(np, index); if (IS_ERR(phy)) - return phy; + goto out_unlock; - if (!try_module_get(phy->ops->owner)) - return ERR_PTR(-EPROBE_DEFER); + if (!try_module_get(phy->ops->owner)) { + phy = ERR_PTR(-EPROBE_DEFER); + goto out_unlock; + } get_device(&phy->dev); +out_unlock: + mutex_unlock(&phy_provider_mutex); return phy; } EXPORT_SYMBOL_GPL(of_phy_get); @@ -743,6 +748,7 @@ struct phy *phy_get(struct device *dev, struct phy *phy; struct device_link *link; + mutex_lock(&phy_provider_mutex); if (dev->of_node) { if (string) index = of_property_match_string(dev->of_node, "phy-names", @@ -753,15 +759,18 @@ struct phy *phy_get(struct device *dev, } else { if (string == NULL) { dev_WARN(dev, "missing string\n"); - return ERR_PTR(-EINVAL); + phy = ERR_PTR(-EINVAL); + goto out_unlock; } phy = phy_find(dev, string); } if (IS_ERR(phy)) - return phy; + goto out_unlock; - if (!try_module_get(phy->ops->owner)) - return ERR_PTR(-EPROBE_DEFER); + if (!try_module_get(phy->ops->owner)) { + phy = ERR_PTR(-EPROBE_DEFER); + goto out_unlock; + } get_device(&phy->dev); @@ -770,6 +779,8 @@ struct phy *phy_get(struct device *dev, dev_dbg(dev, "failed to create device link to %s\n", dev_name(phy->dev.parent)); +out_unlock: + mutex_unlock(&phy_provider_mutex); return phy; } EXPORT_SYMBOL_GPL(phy_get); @@ -918,15 +929,17 @@ struct phy *devm_of_phy_get_by_index(str if (!ptr) return ERR_PTR(-ENOMEM); + mutex_lock(&phy_provider_mutex); phy = _of_phy_get(np, index); if (IS_ERR(phy)) { devres_free(ptr); - return phy; + goto out_unlock; } if (!try_module_get(phy->ops->owner)) { devres_free(ptr); - return ERR_PTR(-EPROBE_DEFER); + phy = ERR_PTR(-EPROBE_DEFER); + goto out_unlock; } get_device(&phy->dev); @@ -939,6 +952,8 @@ struct phy *devm_of_phy_get_by_index(str dev_dbg(dev, "failed to create device link to %s\n", dev_name(phy->dev.parent)); +out_unlock: + mutex_unlock(&phy_provider_mutex); return phy; } EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index);