From patchwork Wed Sep 22 23:35:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vladimir Zapolskiy X-Patchwork-Id: 514520 Delivered-To: patch@linaro.org Received: by 2002:a17:906:f46:0:0:0:0 with SMTP id h6csp994985ejj; Wed, 22 Sep 2021 16:35:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyNM/LdjY16xgcwXlpfcGY0SqjrzKsUDE4ikWxUvdX2RHy41nGBYfU17PQaYxPHJK48WTv3 X-Received: by 2002:a05:6638:2393:: with SMTP id q19mr1389988jat.109.1632353755705; Wed, 22 Sep 2021 16:35:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632353755; cv=none; d=google.com; s=arc-20160816; b=kQ09b6MgIwQeEE0IfWvTRNLuSusf45UQgNvcpnchOrNlUdkEB+HzjsjJvv5ipXarQL ZHpDmGxjmBAtRTsmOeEdOyUtmOhr0mC3FgAFJPaxqT8qrwedAfyoBFNUGP/+KNUtm2AC +r6sZhL3IxwoYKtskM7m5HwSDgKS02z9LPAjRZtfEzVoD92Xkei5gRgboE385+l8ByY1 0IrAVrZYLRNI2dFKHmu/m/zQKTY8b9Yzu7+cQSm6Vglyzv2pRBQUWBqxGj1rMR0Vakh5 53zf2yztbIjhNKxo+F4AEYaWgohY55v7nFV+R8LW5PWPVT0/5Xoae+kXfHlY5+eKKcVu bYlA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=JvK7nAAVSpgD5oIumQfPNaMMDtIPTVj4JW6h6SRFBiI=; b=ZDpHK2yAG8vGtAHEsBaKDFmR46IywnCrp+o3R8aHd03gFuhd7qWVEYNnSbU3kZUYjX zfPYtdn1/RHTFW0riimI+ItTcyA/t4HxLfs20JSmAABkqUNw3TwB/eL5jD3VWFDkJh0m zwf1XgFbc0QcydCBy5bCyHw4TdDflFGxHy42sxe6DjMSjR6T8A+Qs3kXrBULUgtUY9vV mxdx87gkZ8crxAmP7fU6eN3CcPp8HzYR1KL2iCUkxPVc+QjFNViM5KURnHm2YzLWhDZF iZzQ3eE7GfxEpi08woCAZOYedwjSFVOZtLjoz6unHb5W4zD2s/uJBExyAjrIAjbNJze0 IMvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=m7Ws3JYf; spf=pass (google.com: domain of linux-arm-msm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-arm-msm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p31si5183536jac.95.2021.09.22.16.35.55; Wed, 22 Sep 2021 16:35:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-arm-msm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=m7Ws3JYf; spf=pass (google.com: domain of linux-arm-msm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-arm-msm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238455AbhIVXhY (ORCPT + 17 others); Wed, 22 Sep 2021 19:37:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231259AbhIVXhY (ORCPT ); Wed, 22 Sep 2021 19:37:24 -0400 Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D201C061574 for ; Wed, 22 Sep 2021 16:35:53 -0700 (PDT) Received: by mail-lf1-x131.google.com with SMTP id m3so18900288lfu.2 for ; Wed, 22 Sep 2021 16:35:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=JvK7nAAVSpgD5oIumQfPNaMMDtIPTVj4JW6h6SRFBiI=; b=m7Ws3JYfXgj+8PH6Cag5WpV2j8JcFJnqRzZ68LxwCGf2HaDFfNhmeZ7GD5HhEVEmHr 2wdPhokOrDyfZ4kfdurEt1drytZc88nqomP8V70TiyK3ukpJ4njjqOllO3cpzVFcjcx8 SvvhudRrNmGLbqmBJvGThmenRpFYW8lOhCeWyTze63VvwstcABuKR2/XjU0wehMkiOIv M2FdSAOIn+cy5ypv4P5Y094wPzuXhmG4/KmvJ1Uw1i/OtBvwmSHemheKyd3qZcbqJbbz 4pfU3ojQzxnsjNguwUj6yIrP+RcaZ4PMLyoz1XpRnd4RMVrjvh2ekbdqQh+UXDGEjyTV WIZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=JvK7nAAVSpgD5oIumQfPNaMMDtIPTVj4JW6h6SRFBiI=; b=orRBaI7X8FJmjl3dlD+IDAxC9vc+yyI3feux2wwi+Okm98h8JBlU8+ey47E+NQsvib AhvvUIwUfN6CefBQbxEhQjDSH9lf+5eO3/IGPvJyLfuVoYx/xTmpozGzY4bYH57A8qFO GxnjiD4UJnYlKeBGqJSXMh2KLp6AsSbBtLZftEwF1zj/BlPTziPj8oBbsneGnWHkftzX CEouhrTcIzZrUzBoUeDbiGB5JLdpt4/vI0FPmDDsMZlY0Vzf/0q5qarj2DGKeQxS0ewb tMAbAytnFfjRvtjp8Ub4BeJv8LrrqHc1IxvNCstEf+5JC0ryWXT41hjvz+hHbGJAzA+y GbKQ== X-Gm-Message-State: AOAM530298hsCfEYocaPGMJWj+6lsQpYK/yqJaufdZs4brWiP/I/Xbn6 YwS8q8m7Qy5H25juhtf3m0dzvQ== X-Received: by 2002:a2e:9707:: with SMTP id r7mr1272013lji.121.1632353751921; Wed, 22 Sep 2021 16:35:51 -0700 (PDT) Received: from localhost.localdomain (88-112-130-172.elisa-laajakaista.fi. [88.112.130.172]) by smtp.gmail.com with ESMTPSA id x204sm287460lff.301.2021.09.22.16.35.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Sep 2021 16:35:51 -0700 (PDT) From: Vladimir Zapolskiy To: Bjorn Andersson , Andy Gross , Kishon Vijay Abraham I , Vinod Koul , Vivek Gautam Cc: linux-arm-msm@vger.kernel.org, linux-phy@lists.infradead.org Subject: [PATCH v2] phy: qcom-qusb2: Fix a memory leak on probe Date: Thu, 23 Sep 2021 02:35:48 +0300 Message-Id: <20210922233548.2150244-1-vladimir.zapolskiy@linaro.org> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org On success nvmem_cell_read() returns a pointer to a dynamically allocated buffer, and therefore it shall be freed after usage. The issue is reported by kmemleak: # cat /sys/kernel/debug/kmemleak unreferenced object 0xffff3b3803e4b280 (size 128): comm "kworker/u16:1", pid 107, jiffies 4294892861 (age 94.120s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace: [<000000007739afdc>] __kmalloc+0x27c/0x41c [<0000000071c0fbf8>] nvmem_cell_read+0x40/0xe0 [<00000000e803ef1f>] qusb2_phy_init+0x258/0x5bc [<00000000fc81fcfa>] phy_init+0x70/0x110 [<00000000e3d48a57>] dwc3_core_soft_reset+0x4c/0x234 [<0000000027d1dbd4>] dwc3_core_init+0x68/0x990 [<000000001965faf9>] dwc3_probe+0x4f4/0x730 [<000000002f7617ca>] platform_probe+0x74/0xf0 [<00000000a2576cac>] really_probe+0xc4/0x470 [<00000000bc77f2c5>] __driver_probe_device+0x11c/0x190 [<00000000130db71f>] driver_probe_device+0x48/0x110 [<0000000019f36c2b>] __device_attach_driver+0xa4/0x140 [<00000000e5812ff7>] bus_for_each_drv+0x84/0xe0 [<00000000f4bac574>] __device_attach+0xe4/0x1c0 [<00000000d3beb631>] device_initial_probe+0x20/0x30 [<000000008019b9db>] bus_probe_device+0xa4/0xb0 Fixes: ca04d9d3e1b1 ("phy: qcom-qusb2: New driver for QUSB2 PHY on Qcom chips") Signed-off-by: Vladimir Zapolskiy --- Changes from v1 to v2: * fixed a memory leak in case of reading a zero value and return, * corrected the fixed commit, the memory leak is present before a rename. drivers/phy/qualcomm/phy-qcom-qusb2.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) -- 2.33.0 Reviewed-by: Bjorn Andersson diff --git a/drivers/phy/qualcomm/phy-qcom-qusb2.c b/drivers/phy/qualcomm/phy-qcom-qusb2.c index 3c1d3b71c825..f1d97fbd1331 100644 --- a/drivers/phy/qualcomm/phy-qcom-qusb2.c +++ b/drivers/phy/qualcomm/phy-qcom-qusb2.c @@ -561,7 +561,7 @@ static void qusb2_phy_set_tune2_param(struct qusb2_phy *qphy) { struct device *dev = &qphy->phy->dev; const struct qusb2_phy_cfg *cfg = qphy->cfg; - u8 *val; + u8 *val, hstx_trim; /* efuse register is optional */ if (!qphy->cell) @@ -575,7 +575,13 @@ static void qusb2_phy_set_tune2_param(struct qusb2_phy *qphy) * set while configuring the phy. */ val = nvmem_cell_read(qphy->cell, NULL); - if (IS_ERR(val) || !val[0]) { + if (IS_ERR(val)) { + dev_dbg(dev, "failed to read a valid hs-tx trim value\n"); + return; + } + hstx_trim = val[0]; + kfree(val); + if (!hstx_trim) { dev_dbg(dev, "failed to read a valid hs-tx trim value\n"); return; } @@ -583,12 +589,10 @@ static void qusb2_phy_set_tune2_param(struct qusb2_phy *qphy) /* Fused TUNE1/2 value is the higher nibble only */ if (cfg->update_tune1_with_efuse) qusb2_write_mask(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE1], - val[0] << HSTX_TRIM_SHIFT, - HSTX_TRIM_MASK); + hstx_trim << HSTX_TRIM_SHIFT, HSTX_TRIM_MASK); else qusb2_write_mask(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE2], - val[0] << HSTX_TRIM_SHIFT, - HSTX_TRIM_MASK); + hstx_trim << HSTX_TRIM_SHIFT, HSTX_TRIM_MASK); } static int qusb2_phy_set_mode(struct phy *phy,