mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>
To: Mukesh Ojha <mukesh.ojha@oss.qualcomm.com>,
	Bjorn Andersson <andersson@kernel.org>,
	Konrad Dybcio <konradybcio@kernel.org>,
	Bartosz Golaszewski <brgl@kernel.org>,
	Guru Das Srinagesh <linux@gurudas.dev>,
	Sibi Sankar <sibi.sankar@oss.qualcomm.com>,
	Elliot Berman <quic_eberman@quicinc.com>,
	Andrew Halaney <ahalaney@redhat.com>
Cc: linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/3] firmware: qcom: scm: Fix reserved memory cleanup on probe failure
Date: Tue, 30 Jun 2026 12:07:26 +0200	[thread overview]
Message-ID: <dfa822b8-5bec-4ad3-b9c4-ead79e0c74d6@oss.qualcomm.com> (raw)
In-Reply-To: <20260629141750.996853-3-mukesh.ojha@oss.qualcomm.com>

On 6/29/26 4:17 PM, Mukesh Ojha wrote:
> of_reserved_mem_device_init() adds an entry to a global list with no
> devres counterpart. If qcom_scm_probe() fails after the call the
> assignment is never cleaned up. A probe retry would add a duplicate
> entry, leaking the original one permanently.
> 
> Add an err_rmem label that calls of_reserved_mem_device_release() and
> route all error paths after of_reserved_mem_device_init() through it.
> of_reserved_mem_device_release() is safe to call unconditionally as it
> simply walks an empty list when nothing was assigned.
> 
> Fixes: a33b2579c8d3 ("firmware: qcom: scm: add support for SHM bridge memory carveout")
> Signed-off-by: Mukesh Ojha <mukesh.ojha@oss.qualcomm.com>
> ---
>  drivers/firmware/qcom/qcom_scm.c | 21 +++++++++++++++------
>  1 file changed, 15 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/firmware/qcom/qcom_scm.c b/drivers/firmware/qcom/qcom_scm.c
> index 464ae3b4ca43..f0e19fc314b4 100644
> --- a/drivers/firmware/qcom/qcom_scm.c
> +++ b/drivers/firmware/qcom/qcom_scm.c
> @@ -2785,9 +2785,11 @@ static int qcom_scm_probe(struct platform_device *pdev)
>  				     "Failed to setup the reserved memory region for TZ mem\n");
>  
>  	ret = qcom_tzmem_enable(scm->dev);
> -	if (ret)
> -		return dev_err_probe(scm->dev, ret,
> -				     "Failed to enable the TrustZone memory allocator\n");
> +	if (ret) {
> +		dev_err_probe(scm->dev, ret,
> +			      "Failed to enable the TrustZone memory allocator\n");

Assign ret = dev_err_probe to preserve the usefulness of this API

Alternatively, I cobbled this together, feel free to take it forward
(compile-tested only):

From 917c849fd21c6660ba0bd55f8b6ce4cb5dfc8299 Mon Sep 17 00:00:00 2001
From: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>
Date: Tue, 30 Jun 2026 12:02:16 +0200
Subject: [PATCH] of: reserved_mem: Introduce devres-managed initialization
 functions

Introduce devres-based helpers for of_reserved_mem_device_init(_by_idx)
to help fight dangling references and ever so slightly reduce the
number of boilerplate deinitialization calls.

Signed-off-by: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>
---
 drivers/of/of_reserved_mem.c    | 37 +++++++++++++++++++++++++++++++++
 include/linux/of_reserved_mem.h | 25 ++++++++++++++++++++++
 2 files changed, 62 insertions(+)

diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
index 82222bd45ac6..79fa04d4cf04 100644
--- a/drivers/of/of_reserved_mem.c
+++ b/drivers/of/of_reserved_mem.c
@@ -787,6 +787,43 @@ void of_reserved_mem_device_release(struct device *dev)
 }
 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);
 
+static void devm_of_reserved_mem_device_release(struct device *dev, void *res)
+{
+	of_reserved_mem_device_release(*(struct device **)res);
+}
+
+/**
+ * devm_of_reserved_mem_device_init_by_idx - Resource managed of_reserved_mem_device_init_by_idx()
+ * @dev: Pointer to the device to configure
+ *
+ * This function assigns respective DMA-mapping operations based on the first
+ * reserved memory region specified by 'memory-region' property in device tree
+ * node of the given device.
+ *
+ * Returns: Negative errno on failure or zero on success.
+ */
+int devm_of_reserved_mem_device_init_by_idx(struct device *dev, struct device_node *np, int idx)
+{
+	struct device **ptr;
+	int ret;
+
+	ptr = devres_alloc(devm_of_reserved_mem_device_release, sizeof(*ptr), GFP_KERNEL);
+	if (!ptr)
+		return -ENOMEM;
+
+	ret = of_reserved_mem_device_init_by_idx(dev, np, idx);
+	if (ret) {
+		devres_free(ptr);
+		return ret;
+	}
+
+	*ptr = dev;
+	devres_add(dev, ptr);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(devm_of_reserved_mem_device_init_by_idx);
+
 /**
  * of_reserved_mem_lookup() - acquire reserved_mem from a device node
  * @np:		node pointer of the desired reserved-memory region
diff --git a/include/linux/of_reserved_mem.h b/include/linux/of_reserved_mem.h
index e8b20b29fa68..28beeeb91f4e 100644
--- a/include/linux/of_reserved_mem.h
+++ b/include/linux/of_reserved_mem.h
@@ -47,6 +47,8 @@ int of_reserved_mem_region_to_resource_byname(const struct device_node *np,
 					      const char *name, struct resource *res);
 int of_reserved_mem_region_count(const struct device_node *np);
 
+int devm_of_reserved_mem_device_init_by_idx(struct device *dev, struct device_node *np, int idx);
+
 #else
 
 #define RESERVEDMEM_OF_DECLARE(name, compat, ops)			\
@@ -91,6 +93,14 @@ static inline int of_reserved_mem_region_count(const struct device_node *np)
 {
 	return 0;
 }
+
+static inline int devm_of_reserved_mem_device_init_by_idx(struct device *dev,
+							  struct device_node *np,
+							  int idx)
+{
+	return -ENOSYS;
+}
+
 #endif
 
 /**
@@ -108,4 +118,19 @@ static inline int of_reserved_mem_device_init(struct device *dev)
 	return of_reserved_mem_device_init_by_idx(dev, dev->of_node, 0);
 }
 
+/**
+ * of_reserved_mem_device_init() - Devres-managed version of of_reserved_mem_device_init()
+ * @dev:	Pointer to the device to configure
+ *
+ * This function assigns respective DMA-mapping operations based on the first
+ * reserved memory region specified by 'memory-region' property in device tree
+ * node of the given device.
+ *
+ * Returns error code or zero on success.
+ */
+static inline int devm_of_reserved_mem_device_init(struct device *dev)
+{
+	return devm_of_reserved_mem_device_init_by_idx(dev, dev->of_node, 0);
+}
+
 #endif /* __OF_RESERVED_MEM_H */
-- 
2.54.0


Konrad

  reply	other threads:[~2026-06-30 10:07 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-29 14:17 [PATCH v2 0/3] Misc. SCM driver fixes Mukesh Ojha
2026-06-29 14:17 ` [PATCH v2 1/3] firmware: qcom: scm: Fix NULL dereference in IRQ handler before __scm is published Mukesh Ojha
2026-06-30  9:50   ` Konrad Dybcio
2026-06-30 10:07     ` Mukesh Ojha
2026-06-29 14:17 ` [PATCH v2 2/3] firmware: qcom: scm: Fix reserved memory cleanup on probe failure Mukesh Ojha
2026-06-30 10:07   ` Konrad Dybcio [this message]
2026-07-02 10:28     ` Mukesh Ojha
2026-06-29 14:17 ` [PATCH v2 3/3] firmware: qcom: scm: Fix tzmem state on probe retry Mukesh Ojha
2026-06-30 10:12   ` Konrad Dybcio
2026-06-30 13:50 ` [PATCH v2 0/3] Misc. SCM driver fixes Bartosz Golaszewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=dfa822b8-5bec-4ad3-b9c4-ead79e0c74d6@oss.qualcomm.com \
    --to=konrad.dybcio@oss.qualcomm.com \
    --cc=ahalaney@redhat.com \
    --cc=andersson@kernel.org \
    --cc=brgl@kernel.org \
    --cc=konradybcio@kernel.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@gurudas.dev \
    --cc=mukesh.ojha@oss.qualcomm.com \
    --cc=quic_eberman@quicinc.com \
    --cc=sibi.sankar@oss.qualcomm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®