From patchwork Tue Oct 3 15:29:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johan Hovold X-Patchwork-Id: 147964 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp2165886vqb; Tue, 3 Oct 2023 08:30:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEN8hiGLOG+2jaL29aC1O5TDZGeusrxjvz4d5ggc49u4V//OLTIUmP6pYvY1ue7Ccnf81v1 X-Received: by 2002:a17:902:e850:b0:1c3:19d6:3559 with SMTP id t16-20020a170902e85000b001c319d63559mr5035956plg.1.1696347055468; Tue, 03 Oct 2023 08:30:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696347055; cv=none; d=google.com; s=arc-20160816; b=X1cuQl/gyh1mJCJ4dO6EZtbvKGMO0paddr923mEFt3MrxD93dLS2T8Zsdhl/3xTKrq Laf7F30OnXQhelJXK2Ya80cDy3oFnZ1VIN8+ZQ3D3/3JklLVrk/VGiwMw2LCPiAoGi4O JneEyWnHJ+rILnhy8UE3/aHp9BdxhO/ZBmGRR/ETLwiq54Q/8gelPNCNVvbzC5BsLGaH fKs7ce3WwDAzPkAVMYflftqHavtL2cP/b/KE4S9wBlSAk9JSj8ECUvUcWcDWz7p7D6Ye MqQNDLvvRr0rw4YbFgAPxJJiyuuOzC4TuW8PJ2zgNoSfZe4r9fIK9p47/vlBV+Zo5wLQ ZE/Q== 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=+bV0z5OR3NMwAKriIl/wAsShbifXj0uUQf1jl616idE=; fh=YWm8QJAM0FHN5RikUov0yPYTmJ6LMVE7JeGkozvOeYU=; b=Wg2ivixqpXz8zxGAO3lswYmyqlW9u8mChRuOry5nZ3ALtvqmYbLp/YH1W4N7HUKGVh WI/bwtAT8kRLiIaxulwR+ftv4koXKdwbu9CuLPQx5k/wcXzkB/KBNZiI8QWZFlQq0aaJ IG8qa++qjk8JIEXYUqL6XIbB2Nn4bICHIxhHU0d8XFXn0BTn3r1FZiiKnvYLeCISdeVr kGUjot7TFHl2KF2Don4yTAO0xUNoH2g9SoyP09LIGq+jLGTzHEDnDQtJ6yAWZuq8H9Ve XUFw7UnOrkfmlizZBpzi1HPFkNYNzPHVQoENww7JZiLuU0UpMfmBPQb3W+VLh2fW8hmH CyBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="u/bqTdux"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id a15-20020a170902710f00b001bbd0450af8si1524152pll.187.2023.10.03.08.30.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 08:30:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="u/bqTdux"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id 728A481441A5; Tue, 3 Oct 2023 08:30:01 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238215AbjJCP3m (ORCPT + 17 others); Tue, 3 Oct 2023 11:29:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232156AbjJCP3i (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 57E5BAB; Tue, 3 Oct 2023 08:29:35 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF7FAC433CB; 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=1696346975; bh=w6oW3CA5lhvBRe2ooxRAdYwa2HiP0AiSXwLhfx4hsQ4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=u/bqTduxmjG7fM8rnm8Fuso35m9V9ijdiz/GM66mSY81rVf16I8i2ceQn4yr41eKh hu5DQUBbpwSsvpG9fKoOBsXtgBcGTxIb4zDnOGGbdDDhAIpde18qyutjeClG78vm4X 3H7Pp3vrDinug7UMjQ4eazdvqczupeszGb+RtBDUtYgymoNchYMOkuEBN6Z8ZJ5nrX TBvyJJ8mZskREjk66lsGFGU1CJmn4FmhLvpZ+Bs6QmI05AzvnPswdEbpmcN/I7CPyB AIZtygRliI0v9KWrzXpagYIDHCcJrdSZtLPU1RWcIbgOlQfcZZm2ICUCL+QEbWzdVY SmhZUixDbKppw== Received: from johan by xi.lan with local (Exim 4.96) (envelope-from ) id 1qnhLF-0003uN-2a; 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 2/5] mfd: qcom-spmi-pmic: fix revid implementation Date: Tue, 3 Oct 2023 17:29:24 +0200 Message-ID: <20231003152927.15000-3-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 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 agentk.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 (agentk.vger.email [0.0.0.0]); Tue, 03 Oct 2023 08:30:01 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778748731198357509 X-GMAIL-MSGID: 1778748809815975171 The Qualcomm SPMI PMIC revid implementation is broken in multiple ways. First, it assumes that just because the sibling base device has been registered that means that it is also bound to a driver, which may not be the case (e.g. due to probe deferral or asynchronous probe). This could trigger a NULL-pointer dereference when attempting to access the driver data of the unbound device. Second, it accesses driver data of a sibling device directly and without any locking, which means that the driver data may be freed while it is being accessed (e.g. on driver unbind). Third, it leaks a struct device reference to the sibling device which is looked up using the spmi_device_from_of() every time a function (child) device is calling the revid function (e.g. on probe). Fix this mess by reimplementing the revid lookup so that it is done only at probe of the PMIC device; the base device fetches the revid info from the hardware, while any secondary SPMI device fetches the information from the base device and caches it so that it can be accessed safely from its children. If the base device has not been probed yet then probe of a secondary device is deferred. 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 | 69 +++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 16 deletions(-) diff --git a/drivers/mfd/qcom-spmi-pmic.c b/drivers/mfd/qcom-spmi-pmic.c index 47738f7e492c..8e449cff5cec 100644 --- a/drivers/mfd/qcom-spmi-pmic.c +++ b/drivers/mfd/qcom-spmi-pmic.c @@ -30,6 +30,8 @@ struct qcom_spmi_dev { struct qcom_spmi_pmic pmic; }; +static DEFINE_MUTEX(pmic_spmi_revid_lock); + #define N_USIDS(n) ((void *)n) static const struct of_device_id pmic_spmi_id_table[] = { @@ -76,24 +78,21 @@ static const struct of_device_id pmic_spmi_id_table[] = { * * This only supports PMICs with 1 or 2 USIDs. */ -static struct spmi_device *qcom_pmic_get_base_usid(struct device *dev) +static struct spmi_device *qcom_pmic_get_base_usid(struct spmi_device *sdev, struct qcom_spmi_dev *ctx) { - struct spmi_device *sdev; - struct qcom_spmi_dev *ctx; struct device_node *spmi_bus; struct device_node *child; int function_parent_usid, ret; u32 pmic_addr; - sdev = to_spmi_device(dev); - ctx = dev_get_drvdata(&sdev->dev); - /* * Quick return if the function device is already in the base * USID. This will always be hit for PMICs with only 1 USID. */ - if (sdev->usid % ctx->num_usids == 0) + if (sdev->usid % ctx->num_usids == 0) { + get_device(&sdev->dev); return sdev; + } function_parent_usid = sdev->usid; @@ -118,10 +117,8 @@ static struct spmi_device *qcom_pmic_get_base_usid(struct device *dev) 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. + * If the base USID for this PMIC hasn't been + * registered yet then we need to defer. */ sdev = ERR_PTR(-EPROBE_DEFER); } @@ -135,6 +132,35 @@ static struct spmi_device *qcom_pmic_get_base_usid(struct device *dev) return sdev; } +static int pmic_spmi_get_base_revid(struct spmi_device *sdev, struct qcom_spmi_dev *ctx) +{ + struct qcom_spmi_dev *base_ctx; + struct spmi_device *base; + int ret = 0; + + base = qcom_pmic_get_base_usid(sdev, ctx); + if (IS_ERR(base)) + return PTR_ERR(base); + + /* + * Copy revid info from base device if it has probed and is still + * bound to its driver. + */ + mutex_lock(&pmic_spmi_revid_lock); + base_ctx = spmi_device_get_drvdata(base); + if (!base_ctx) { + ret = -EPROBE_DEFER; + goto out_unlock; + } + memcpy(&ctx->pmic, &base_ctx->pmic, sizeof(ctx->pmic)); +out_unlock: + mutex_unlock(&pmic_spmi_revid_lock); + + put_device(&base->dev); + + return ret; +} + static int pmic_spmi_load_revid(struct regmap *map, struct device *dev, struct qcom_spmi_pmic *pmic) { @@ -210,11 +236,7 @@ const struct qcom_spmi_pmic *qcom_pmic_get(struct device *dev) if (!of_match_device(pmic_spmi_id_table, dev->parent)) return ERR_PTR(-EINVAL); - sdev = qcom_pmic_get_base_usid(dev->parent); - - if (IS_ERR(sdev)) - return ERR_CAST(sdev); - + sdev = to_spmi_device(dev->parent); spmi = dev_get_drvdata(&sdev->dev); return &spmi->pmic; @@ -249,16 +271,31 @@ static int pmic_spmi_probe(struct spmi_device *sdev) ret = pmic_spmi_load_revid(regmap, &sdev->dev, &ctx->pmic); if (ret < 0) return ret; + } else { + ret = pmic_spmi_get_base_revid(sdev, ctx); + if (ret) + return ret; } + + mutex_lock(&pmic_spmi_revid_lock); spmi_device_set_drvdata(sdev, ctx); + mutex_unlock(&pmic_spmi_revid_lock); return devm_of_platform_populate(&sdev->dev); } +static void pmic_spmi_remove(struct spmi_device *sdev) +{ + mutex_lock(&pmic_spmi_revid_lock); + spmi_device_set_drvdata(sdev, NULL); + mutex_unlock(&pmic_spmi_revid_lock); +} + MODULE_DEVICE_TABLE(of, pmic_spmi_id_table); static struct spmi_driver pmic_spmi_driver = { .probe = pmic_spmi_probe, + .remove = pmic_spmi_remove, .driver = { .name = "pmic-spmi", .of_match_table = pmic_spmi_id_table,