Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp2039658rdb; Tue, 3 Oct 2023 08:30:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEVIlLJbvYG402vlwpVLpX8xu3CUEF3UClqzq6lUzTXb3ovrTHsLE3n6JDJeYvRk/8gZ0p2 X-Received: by 2002:a05:6a00:c85:b0:68a:5449:7436 with SMTP id a5-20020a056a000c8500b0068a54497436mr17039908pfv.32.1696347047581; Tue, 03 Oct 2023 08:30:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696347047; cv=none; d=google.com; s=arc-20160816; b=UFFJg3VZSeoEL1k4+kTFHPkZQ1OF99BkXH0LGrTs+fImU8v0NnyoHetmglZXvAMluj M04t27gTvW2/G/yb63F+wRUzF3AvkF8UnWZrPlBroX7iANcsJMCWYYrQbEcoZ5qkSsPx 7PqSlOdoHD2K6NnZsmo7+LIjSkeiOWmDp7WvlP6veWWpkrnuzCnmTHPhhdBv+LcWrTHe sy2ePYbKOEF5sgfOwEwjjjgEIDGevFZ/VZFvtVvNBDDRFSIUhBuuvVwVGjZ0KS1C32/9 Ef4TNk6J9pc3nAnEdO1XAeAv66WjvxGtvTdp7yeuP23/mSGUUY4Lq8tZLodhQzDjBleT ONaQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=bI3A4drhNK4r/KWLdWdC8bfV1+hFbw518/inPNmk2wg=; fh=YWm8QJAM0FHN5RikUov0yPYTmJ6LMVE7JeGkozvOeYU=; b=ePChcsOXtmdIW94xXUz7f+qFbcW9Qp5o72hywHo7L5iddt7ZxLOJzmtLn3EQZIhGKu Cz3n5U24ETC0Mj0iN7+pZ3PVm6fct2CaiMvgk7oFnEix3K9M83EXVtkgqtl2Xj9mK6ZP PlQIAmurq56W/YHnfWo7K3N3eYdX08Yf4Ck/v6lODjgKru24mda5A+C01ozVv0qYxjjS CKfwQSYOsoTxzEIZ+geRz4RgZy3S9E+ks5RGcKNkeY3AJLCFCBGW6f88cxGJDljnjoZP PCHq8OnW4q4uuPdYCqefqRbj+LlP3ES9R36x+rih01PV72PFyWhXtS3g2eapjUMJAcig SZQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=MtpKhBPf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id g62-20020a636b41000000b00569dfeb8123si1552606pgc.189.2023.10.03.08.30.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 08:30:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=MtpKhBPf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 8DA878089848; Tue, 3 Oct 2023 08:30:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239313AbjJCP3s (ORCPT + 99 others); Tue, 3 Oct 2023 11:29:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231443AbjJCP3i (ORCPT ); Tue, 3 Oct 2023 11:29:38 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5103695; Tue, 3 Oct 2023 08:29:35 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E1D75C433C8; Tue, 3 Oct 2023 15:29:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696346974; bh=tuSq7yhyHWSvaiKeFaqV7WZhQUAQixug8q4HdZYlAno=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MtpKhBPfJ74lU82drgA2oKf+YX3mFR/c11kgGMzndTWpPgOi2hEmENkWW/RUWIKv/ oh5Dc+skDITF3WJQnuoVlHm6/mVWDpQkP0KirEUByb3CB8vIdg1p6Fi22woNdGNiN9 co1KQ++cpjOaRrFfa9mbrrixxxpW1IO4bUCFUzx2a9zr1lZVVEK0+AqM/LzJjKKdqY 94P4HEV9pQQq0kFDPiZjWyPVo8TUZHU/d/LN6AXLrgRclPZV2MdYQ8T6IbkQ/BvcYa 2RHOoi0f+7TTAzD7U84ETiymULbdd37n79WUMPjOY/K/rh3LbCMLgDFuBxIZpVqfeS 176Y8xHTs98QQ== Received: from johan by xi.lan with local (Exim 4.96) (envelope-from ) id 1qnhLF-0003uL-2L; Tue, 03 Oct 2023 17:29:45 +0200 From: Johan Hovold To: Lee Jones Cc: Andy Gross , Bjorn Andersson , Konrad Dybcio , Stephen Boyd , Caleb Connolly , linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, Johan Hovold , stable@vger.kernel.org, Dmitry Baryshkov Subject: [PATCH 1/5] mfd: qcom-spmi-pmic: fix reference leaks in revid helper Date: Tue, 3 Oct 2023 17:29:23 +0200 Message-ID: <20231003152927.15000-2-johan+linaro@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231003152927.15000-1-johan+linaro@kernel.org> References: <20231003152927.15000-1-johan+linaro@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 03 Oct 2023 08:30:40 -0700 (PDT) The Qualcomm SPMI PMIC revid implementation is broken in multiple ways. First, it totally ignores struct device_node reference counting and leaks references to the parent bus node as well as each child it iterates over using an open-coded for_each_child_of_node(). Second, it leaks references to each spmi device on the bus that it iterates over by failing to drop the reference taken by the spmi_device_from_of() helper. Fix the struct device_node leaks by reimplementing the lookup using for_each_child_of_node() and adding the missing reference count decrements. Fix the sibling struct device leaks by dropping the unnecessary lookups of devices with the wrong USID. Note that this still leaves one struct device reference leak in case a base device is found but it is not the parent of the device used for the lookup. This will be addressed in a follow-on patch. Fixes: e9c11c6e3a0e ("mfd: qcom-spmi-pmic: expose the PMIC revid information to clients") Cc: stable@vger.kernel.org # 6.0 Cc: Caleb Connolly Cc: Dmitry Baryshkov Signed-off-by: Johan Hovold --- drivers/mfd/qcom-spmi-pmic.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/drivers/mfd/qcom-spmi-pmic.c b/drivers/mfd/qcom-spmi-pmic.c index 7e2cd79d17eb..47738f7e492c 100644 --- a/drivers/mfd/qcom-spmi-pmic.c +++ b/drivers/mfd/qcom-spmi-pmic.c @@ -81,7 +81,7 @@ static struct spmi_device *qcom_pmic_get_base_usid(struct device *dev) struct spmi_device *sdev; struct qcom_spmi_dev *ctx; struct device_node *spmi_bus; - struct device_node *other_usid = NULL; + struct device_node *child; int function_parent_usid, ret; u32 pmic_addr; @@ -105,28 +105,34 @@ static struct spmi_device *qcom_pmic_get_base_usid(struct device *dev) * device for USID 2. */ spmi_bus = of_get_parent(sdev->dev.of_node); - do { - other_usid = of_get_next_child(spmi_bus, other_usid); - - ret = of_property_read_u32_index(other_usid, "reg", 0, &pmic_addr); - if (ret) - return ERR_PTR(ret); + sdev = ERR_PTR(-ENODATA); + for_each_child_of_node(spmi_bus, child) { + ret = of_property_read_u32_index(child, "reg", 0, &pmic_addr); + if (ret) { + of_node_put(child); + sdev = ERR_PTR(ret); + break; + } - sdev = spmi_device_from_of(other_usid); if (pmic_addr == function_parent_usid - (ctx->num_usids - 1)) { - if (!sdev) + sdev = spmi_device_from_of(child); + if (!sdev) { /* * If the base USID for this PMIC hasn't probed yet * but the secondary USID has, then we need to defer * the function driver so that it will attempt to * probe again when the base USID is ready. */ - return ERR_PTR(-EPROBE_DEFER); - return sdev; + sdev = ERR_PTR(-EPROBE_DEFER); + } + of_node_put(child); + break; } - } while (other_usid->sibling); + } + + of_node_put(spmi_bus); - return ERR_PTR(-ENODATA); + return sdev; } static int pmic_spmi_load_revid(struct regmap *map, struct device *dev, -- 2.41.0