Received: by 2002:a05:622a:251a:b0:39a:b4a2:e86 with SMTP id cm26csp314374qtb; Wed, 19 Oct 2022 02:39:06 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7nimNvvi/XEpEy+EYd/o0smVmliWsX1bbDRb7GnhLAgARsmBwBeOd2x6zRPdMWiQlo0W6d X-Received: by 2002:a17:90b:4f90:b0:20d:43be:11a1 with SMTP id qe16-20020a17090b4f9000b0020d43be11a1mr9036929pjb.66.1666172336381; Wed, 19 Oct 2022 02:38:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666172336; cv=none; d=google.com; s=arc-20160816; b=ezL9jKHr+Uvo3xb7KU2QbCmKyCt1CVKycusXqRbv9Zh8cG7Zz9iyXBBgyGsqwli2nH bacimPFmtgMI7rdAp+vuT8qTEjIz7eCRvIh1PiRb8JpLTt2pKEi2BG0rl7GMnEd9RGCW RdxKTr8XoqCEjG1goZwoNUz6UfsL2839hgfr/k2xy7vXhW8uYf9XDYoAv5HJJogFwc7T V54MqfxgM4hWRH5t0Pf70c6lZRHf0XXuH8aqxj3KMnXPgNj+9aWQyXIgHsqx24UryI3F cCASxFjUxcXC73+Fbqu72Psy3veeV4jsw9mPDXFxJlfnjuPKdCTbglMggQ+RRkepbWlp X3dw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=TDcLYGcGAZCv5z2b3pIG9y886fk70+uka9vclmDwfkM=; b=JHtNAAEUjRh0Jsk818o/0FkMvSLmM2ZGHpBhkSTmSD1iuQYfEBbFOZIHWpWK+aKfu4 oGZmhaeaHM/DVVd5CQqP2lFRfTX2+UYVVvFgUl+tPRxVWuIrS3iDPWSmY9i1crhyOKzx 2XAu3GVIHoZb8CoqdEpjlmuK9/Aro7MRlGR9LQocolME3pRlj6fhEbmRCBPRpcQXvQvH jfuwxP+LZCtVtrr6zyT66376Ra5YkXyAE5m4+SrBM7kC26YkLS90q2LSRKWVvjkMjeym mWczIbZEsvBPOnO/OQfa9ap5hLIb9BP3EQXAeCvE+V+4N+3Ow7PYPpJ3AhvJPmFNLdGM rJsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0TIR2ShH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d19-20020a170902e15300b00179e261e4f8si15375391pla.90.2022.10.19.02.38.40; Wed, 19 Oct 2022 02:38:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0TIR2ShH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233268AbiJSJUX (ORCPT + 99 others); Wed, 19 Oct 2022 05:20:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233396AbiJSJTJ (ORCPT ); Wed, 19 Oct 2022 05:19:09 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6103289CC6; Wed, 19 Oct 2022 02:08:06 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9F7C26174B; Wed, 19 Oct 2022 09:03:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B43EFC433D7; Wed, 19 Oct 2022 09:03:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666170199; bh=jqy8C+Kc3rOan3G2Kng9jfcs+w4MNPapy2AHnhT+17U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0TIR2ShH4eB2MRY7LKAQ/SJiEYSCBVw3TVBiyEtVbsaUDCMGe2DEhU5z7c4wOpXF7 NB0FCm9Cw4/HJcsu3FokC48KEhpK+w7YzNUxWt6XHQXotyXT4Ihy+rGDsX68x3aE3b 0RTpY694rQAxuZb23yhN2oF+ybH0f+fanY+pVZNA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Johan Hovold , Vinod Koul , Sasha Levin Subject: [PATCH 6.0 528/862] phy: qcom-qmp-usb: fix memleak on probe deferral Date: Wed, 19 Oct 2022 10:30:15 +0200 Message-Id: <20221019083313.304749902@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221019083249.951566199@linuxfoundation.org> References: <20221019083249.951566199@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Johan Hovold [ Upstream commit a5d6b1ac56cbd6b5850a3a54e35f1cb71e8e8cdd ] Switch to using the device-managed of_iomap helper to avoid leaking memory on probe deferral and driver unbind. Note that this helper checks for already reserved regions and may fail if there are multiple devices claiming the same memory. Two bindings currently rely on overlapping mappings for the PCS region so fallback to non-exclusive mappings for those for now. Fixes: e78f3d15e115 ("phy: qcom-qmp: new qmp phy driver for qcom-chipsets") Signed-off-by: Johan Hovold Link: https://lore.kernel.org/r/20220916102340.11520-7-johan+linaro@kernel.org Signed-off-by: Vinod Koul Signed-off-by: Sasha Levin --- drivers/phy/qualcomm/phy-qcom-qmp-usb.c | 61 +++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 17 deletions(-) --- a/drivers/phy/qualcomm/phy-qcom-qmp-usb.c +++ b/drivers/phy/qualcomm/phy-qcom-qmp-usb.c @@ -2489,6 +2489,21 @@ static const struct phy_ops qcom_qmp_phy .owner = THIS_MODULE, }; +static void __iomem *qmp_usb_iomap(struct device *dev, struct device_node *np, + int index, bool exclusive) +{ + struct resource res; + + if (!exclusive) { + if (of_address_to_resource(np, index, &res)) + return IOMEM_ERR_PTR(-EINVAL); + + return devm_ioremap(dev, res.start, resource_size(&res)); + } + + return devm_of_iomap(dev, np, index, NULL); +} + static int qcom_qmp_phy_usb_create(struct device *dev, struct device_node *np, int id, void __iomem *serdes, const struct qmp_phy_cfg *cfg) @@ -2497,8 +2512,18 @@ int qcom_qmp_phy_usb_create(struct devic struct phy *generic_phy; struct qmp_phy *qphy; char prop_name[MAX_PROP_NAME]; + bool exclusive = true; int ret; + /* + * FIXME: These bindings should be fixed to not rely on overlapping + * mappings for PCS. + */ + if (of_device_is_compatible(dev->of_node, "qcom,sdx65-qmp-usb3-uni-phy")) + exclusive = false; + if (of_device_is_compatible(dev->of_node, "qcom,sm8350-qmp-usb3-uni-phy")) + exclusive = false; + qphy = devm_kzalloc(dev, sizeof(*qphy), GFP_KERNEL); if (!qphy) return -ENOMEM; @@ -2511,17 +2536,17 @@ int qcom_qmp_phy_usb_create(struct devic * For dual lane PHYs: tx2 -> 3, rx2 -> 4, pcs_misc (optional) -> 5 * For single lane PHYs: pcs_misc (optional) -> 3. */ - qphy->tx = of_iomap(np, 0); - if (!qphy->tx) - return -ENOMEM; - - qphy->rx = of_iomap(np, 1); - if (!qphy->rx) - return -ENOMEM; - - qphy->pcs = of_iomap(np, 2); - if (!qphy->pcs) - return -ENOMEM; + qphy->tx = devm_of_iomap(dev, np, 0, NULL); + if (IS_ERR(qphy->tx)) + return PTR_ERR(qphy->tx); + + qphy->rx = devm_of_iomap(dev, np, 1, NULL); + if (IS_ERR(qphy->rx)) + return PTR_ERR(qphy->rx); + + qphy->pcs = qmp_usb_iomap(dev, np, 2, exclusive); + if (IS_ERR(qphy->pcs)) + return PTR_ERR(qphy->pcs); if (cfg->pcs_usb_offset) qphy->pcs_usb = qphy->pcs + cfg->pcs_usb_offset; @@ -2533,9 +2558,9 @@ int qcom_qmp_phy_usb_create(struct devic * offset from the first lane. */ if (cfg->is_dual_lane_phy) { - qphy->tx2 = of_iomap(np, 3); - qphy->rx2 = of_iomap(np, 4); - if (!qphy->tx2 || !qphy->rx2) { + qphy->tx2 = devm_of_iomap(dev, np, 3, NULL); + qphy->rx2 = devm_of_iomap(dev, np, 4, NULL); + if (IS_ERR(qphy->tx2) || IS_ERR(qphy->rx2)) { dev_warn(dev, "Underspecified device tree, falling back to legacy register regions\n"); @@ -2545,15 +2570,17 @@ int qcom_qmp_phy_usb_create(struct devic qphy->rx2 = qphy->rx + QMP_PHY_LEGACY_LANE_STRIDE; } else { - qphy->pcs_misc = of_iomap(np, 5); + qphy->pcs_misc = devm_of_iomap(dev, np, 5, NULL); } } else { - qphy->pcs_misc = of_iomap(np, 3); + qphy->pcs_misc = devm_of_iomap(dev, np, 3, NULL); } - if (!qphy->pcs_misc) + if (IS_ERR(qphy->pcs_misc)) { dev_vdbg(dev, "PHY pcs_misc-reg not used\n"); + qphy->pcs_misc = NULL; + } snprintf(prop_name, sizeof(prop_name), "pipe%d", id); qphy->pipe_clk = devm_get_clk_from_child(dev, np, prop_name);