Skip to contentSkip to conversation

Migrate PAS clients to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

PATCH In review Mukesh Ojha 4 participants 24 messages Nov 16 – Nov 27

Subsystems (15)
lore
Share & tools
lore
#0 lore raw

v3 cover Migrate PAS clients to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

In preparation to remove qcom_mdt_load() and qcom_pas_auth_and_reset() public function, migrate every user to the new PAS context-aware API qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset().

This series migrates every remaining caller of the deprecated APIs (qcom_wcnss, adreno zap shader, iris, venus, IPA, ath12k) and then removes qcom_mdt_load() and qcom_pas_auth_and_reset() entirely.

Requesting the Ack from subsystem owners to get this series merge through Bjorn's tree.

Changes in v3:
- Added Acked by tag.
- Fixed build warning.
- Link to v2: https://lore.kernel.org/lkml/[email protected]/

Changes in v2:
- Addressed Sasiko comments.
- Addressed Alex comment and added 1/9 as new patch in this
series and use the API to other patches.
- Commit description change.
- Link to v1: https://lore.kernel.org/lkml/[email protected]/

Mukesh Ojha (9):
firmware: qcom: pas: add qcom_pas_context_init() for pre-allocated
context
remoteproc: qcom_wcnss: migrate to qcom_mdt_pas_load() and
qcom_pas_prepare_and_auth_reset()
drm/msm/adreno: migrate zap shader loading to qcom_mdt_pas_load() and
qcom_pas_prepare_and_auth_reset()
media: iris: migrate firmware loading to qcom_mdt_pas_load() and
qcom_pas_prepare_and_auth_reset()
media: venus: migrate firmware loading to qcom_mdt_pas_load() and
qcom_pas_prepare_and_auth_reset()
net: ipa: migrate firmware loading to qcom_mdt_pas_load() and
qcom_pas_prepare_and_auth_reset()
wifi: ath12k: migrate firmware loading to qcom_mdt_pas_load() and
qcom_pas_prepare_and_auth_reset()
soc: qcom: mdt_loader: remove qcom_mdt_load()
firmware: qcom: pas: remove qcom_pas_auth_and_reset()

 drivers/firmware/qcom/qcom_pas.c              | 42 +++++++------
 drivers/firmware/qcom/qcom_pas.h              |  3 -
 drivers/firmware/qcom/qcom_pas_tee.c          |  6 --
 drivers/firmware/qcom/qcom_scm.c              |  7 +--
 drivers/gpu/drm/msm/adreno/adreno_gpu.c       | 22 ++-----
 drivers/media/platform/qcom/iris/iris_core.h  |  2 +
 .../media/platform/qcom/iris/iris_firmware.c  | 27 ++++----
 drivers/media/platform/qcom/venus/core.h      |  2 +
 drivers/media/platform/qcom/venus/firmware.c  | 29 ++++-----
 drivers/net/ipa/ipa_main.c                    | 23 +++----
 drivers/net/wireless/ath/ath12k/ahb.c         | 23 ++++---
 drivers/remoteproc/qcom_wcnss.c               | 12 ++--
 drivers/soc/qcom/mdt_loader.c                 | 63 +++++++------------
 include/linux/firmware/qcom/qcom_pas.h        |  3 +-
 include/linux/soc/qcom/mdt_loader.h           | 12 ----
 15 files changed, 116 insertions(+), 160 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH v3 1/9 firmware: qcom: pas: add qcom_pas_context_init() for pre-allocated context

devm_qcom_pas_context_alloc() allocates and initializes a struct qcom_pas_context using devres. Several callers, however, either live entirely on the stack (short-lived helper functions) or embed the context in a larger structure that is already managed by other means. Forcing those callers through the devm allocation path is wasteful and, in the case of functions called on every power cycle, causes a devres entry to accumulate on every invocation.

Add qcom_pas_context_init() that initializes a caller-supplied struct qcom_pas_context in place using memset + field assignment, with no allocation at all. Rework devm_qcom_pas_context_alloc() to delegate to qcom_pas_context_init() after the kzalloc, so both helpers share the same initialization logic.

Update the kernel-doc for qcom_mdt_pas_load() to reflect that the PAS context may now be initialized by either qcom_pas_context_init() or devm_qcom_pas_context_alloc().

---
 drivers/firmware/qcom/qcom_pas.c       | 26 ++++++++++++++++++++++----
 drivers/soc/qcom/mdt_loader.c          |  4 ++--
 include/linux/firmware/qcom/qcom_pas.h |  2 ++
 3 files changed, 26 insertions(+), 6 deletions(-)
drivers/firmware/qcom/qcom_pas.c +22 -4
index d74dd7b4f332..fdf62003fbf1 100644
@@ -14,6 +14,27 @@
 
 static struct qcom_pas_ops *ops_ptr;
 
+/**
+ * qcom_pas_context_init() - Initialize a peripheral authentication service
+ *			     context for a given peripheral
+ *
+ * @ctx:	  Caller-provided PAS context to initialize
+ * @dev:	  PAS firmware device
+ * @pas_id:	  peripheral authentication service id
+ * @mem_phys:	  Subsystem reserve memory start address
+ * @mem_size:	  Subsystem reserve memory size
+ */
+void qcom_pas_context_init(struct qcom_pas_context *ctx, struct device *dev,
+			   u32 pas_id, phys_addr_t mem_phys, size_t mem_size)
+{
+	memset(ctx, 0, sizeof(*ctx));
+	ctx->dev = dev;
+	ctx->pas_id = pas_id;
+	ctx->mem_phys = mem_phys;
+	ctx->mem_size = mem_size;
+}
+EXPORT_SYMBOL_GPL(qcom_pas_context_init);
+
 /**
  * devm_qcom_pas_context_alloc() - Allocate peripheral authentication service
  *				   context for a given peripheral
@@ -39,10 +60,7 @@ struct qcom_pas_context *devm_qcom_pas_context_alloc(struct device *dev,
 	if (!ctx)
 		return ERR_PTR(-ENOMEM);
 
-	ctx->dev = dev;
-	ctx->pas_id = pas_id;
-	ctx->mem_phys = mem_phys;
-	ctx->mem_size = mem_size;
+	qcom_pas_context_init(ctx, dev, pas_id, mem_phys, mem_size);
 
 	return ctx;
 }
drivers/soc/qcom/mdt_loader.c +2 -2
index 0d55e9e0af3f..c6ec67e1da09 100644
@@ -476,8 +476,8 @@ EXPORT_SYMBOL_GPL(qcom_mdt_load);
  * firmware segments (e.g., .bXX files). Authentication of the segments done
  * by a separate call.
  *
- * The PAS context must be initialized using devm_qcom_pas_context_alloc()
- * prior to invoking this function.
+ * The PAS context must be initialized using qcom_pas_context_init() or
+ * devm_qcom_pas_context_alloc() prior to invoking this function.
  *
  * @ctx:        Pointer to the PAS (Peripheral Authentication Service) context
  * @fw:         Firmware object representing the .mdt file
include/linux/firmware/qcom/qcom_pas.h +2 -0
index 1d132e89536e..458075868bf5 100644
@@ -36,6 +36,8 @@ static inline void __iomem *qcom_pas_ctx_map(struct qcom_pas_context *ctx)
 }
 
 bool qcom_pas_is_available(void);
+void qcom_pas_context_init(struct qcom_pas_context *ctx, struct device *dev,
+			   u32 pas_id, phys_addr_t mem_phys, size_t mem_size);
 struct qcom_pas_context *devm_qcom_pas_context_alloc(struct device *dev,
 						     u32 pas_id,
 						     phys_addr_t mem_phys,
Signature · 1 line
2.55.0
#2 lore raw

PATCH v3 2/9 remoteproc: qcom_wcnss: migrate to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

In reply to Mukesh Ojha

wcnss_load() calls qcom_mdt_load(), which bundles metadata init, memory setup, and segment loading into a single API that does not expose a PAS context, and wcnss_start() calls qcom_pas_auth_and_reset() for TrustZone authentication, which is the intended behaviour. However, a new qcom_mdt_pas_load() has recently been introduced that works with a PAS context and can handle everything qcom_mdt_load() provides, along with additional functionality such as shmbridge creation and retrieving memory from the shmbridge buffer when the same remote processor needs to run while Linux runs at EL2.

Allocate a qcom_pas_context in wcnss_probe() via devm_qcom_pas_context_alloc() and store it in struct qcom_wcnss. Replace qcom_mdt_load() with qcom_mdt_pas_load() in wcnss_load() and replace qcom_pas_auth_and_reset() with qcom_pas_prepare_and_auth_reset() in wcnss_start(). The existing devm_ioremap_wc() mapping is retained for wcnss_da_to_va() address translation.

---
 drivers/remoteproc/qcom_wcnss.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/remoteproc/qcom_wcnss.c +8 -4
index 02f38fe29f20..840fa67a6928 100644
@@ -96,6 +96,7 @@ struct qcom_wcnss {
 	phys_addr_t mem_reloc;
 	void __iomem *mem_region;
 	size_t mem_size;
+	struct qcom_pas_context *pas_ctx;
 
 	struct qcom_rproc_subdev smd_subdev;
 	struct qcom_sysmon *sysmon;
@@ -157,9 +158,7 @@ static int wcnss_load(struct rproc *rproc, const struct firmware *fw)
 	struct qcom_wcnss *wcnss = rproc->priv;
 	int ret;
 
-	ret = qcom_mdt_load(wcnss->dev, fw, rproc->firmware, WCNSS_PAS_ID,
-			    (__force void *)wcnss->mem_region, wcnss->mem_phys,
-			    wcnss->mem_size, &wcnss->mem_reloc);
+	ret = qcom_mdt_pas_load(wcnss->pas_ctx, fw, rproc->firmware, &wcnss->mem_reloc);
 	if (ret)
 		return ret;
 
@@ -257,7 +256,7 @@ static int wcnss_start(struct rproc *rproc)
 	wcnss_indicate_nv_download(wcnss);
 	wcnss_configure_iris(wcnss);
 
-	ret = qcom_pas_auth_and_reset(WCNSS_PAS_ID);
+	ret = qcom_pas_prepare_and_auth_reset(wcnss->pas_ctx);
 	if (ret) {
 		dev_err(wcnss->dev,
 			"failed to authenticate image and release reset\n");
@@ -599,6 +598,11 @@ static int wcnss_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
+	wcnss->pas_ctx = devm_qcom_pas_context_alloc(wcnss->dev, WCNSS_PAS_ID,
+						     wcnss->mem_phys, wcnss->mem_size);
+	if (IS_ERR(wcnss->pas_ctx))
+		return PTR_ERR(wcnss->pas_ctx);
+
 	wcnss->pmu_cfg = mmio + data->pmu_offset;
 	wcnss->spare_out = mmio + data->spare_offset;
 
Signature · 1 line
2.55.0
#3 lore raw

PATCH v3 3/9 drm/msm/adreno: migrate zap shader loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

In reply to Mukesh Ojha

zap_shader_load_mdt() calls qcom_mdt_load(), which bundles metadata initialization, memory setup, and segment loading into a single API that does not expose a PAS context. zap_shader_load_mdt() also calls qcom_pas_auth_and_reset() for TrustZone authentication, which is the intended behaviour. However, a new qcom_mdt_pas_load() has recently been introduced that works with a PAS context and can handle everything qcom_mdt_load() provides, along with additional functionality such as shmbridge creation and retrieving memory from the shmbridge buffer when the same remote processor needs to run while Linux runs at EL2.

Replace both with qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset() using a stack-allocated struct qcom_pas_context initialized by qcom_pas_context_init(). zap_shader_load_mdt() is guarded by a 'static bool loaded' flag so it only runs once; the stack allocation is sufficient and avoids any heap overhead. The manual memremap/memunmap pair is removed because qcom_mdt_pas_load() handles memory mapping internally.

---
 drivers/gpu/drm/msm/adreno/adreno_gpu.c | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)
drivers/gpu/drm/msm/adreno/adreno_gpu.c +6 -16
index 54f337475231..8e4aa8e22108 100644
@@ -34,11 +34,11 @@ static int zap_shader_load_mdt(struct msm_gpu *gpu, const char *fwname,
 	struct device *dev = &gpu->pdev->dev;
 	const struct firmware *fw;
 	const char *signed_fwname = NULL;
+	struct qcom_pas_context ctx;
 	struct device_node *np;
 	struct resource r;
 	phys_addr_t mem_phys;
 	ssize_t mem_size;
-	void *mem_region = NULL;
 	int ret;
 
 	if (!IS_ENABLED(CONFIG_ARCH_QCOM)) {
@@ -121,39 +121,32 @@ static int zap_shader_load_mdt(struct msm_gpu *gpu, const char *fwname,
 		goto out;
 	}
 
-	/* Allocate memory for the firmware image */
-	mem_region = memremap(mem_phys, mem_size,  MEMREMAP_WC);
-	if (!mem_region) {
-		ret = -ENOMEM;
-		goto out;
-	}
+	qcom_pas_context_init(&ctx, dev, pasid, mem_phys, mem_size);
 
 	/*
 	 * Load the rest of the MDT
 	 *
 	 * Note that we could be dealing with two different paths, since
 	 * with upstream linux-firmware it would be in a qcom/ subdir..
-	 * adreno_request_fw() handles this, but qcom_mdt_load() does
+	 * adreno_request_fw() handles this, but qcom_mdt_pas_load() does
 	 * not.  But since we've already gotten through adreno_request_fw()
 	 * we know which of the two cases it is:
 	 */
 	if (signed_fwname || (to_adreno_gpu(gpu)->fwloc == FW_LOCATION_LEGACY)) {
-		ret = qcom_mdt_load(dev, fw, fwname, pasid,
-				mem_region, mem_phys, mem_size, NULL);
+		ret = qcom_mdt_pas_load(&ctx, fw, fwname, NULL);
 	} else {
 		char *newname;
 
 		newname = kasprintf(GFP_KERNEL, "qcom/%s", fwname);
 
-		ret = qcom_mdt_load(dev, fw, newname, pasid,
-				mem_region, mem_phys, mem_size, NULL);
+		ret = qcom_mdt_pas_load(&ctx, fw, newname, NULL);
 		kfree(newname);
 	}
 	if (ret)
 		goto out;
 
 	/* Send the image to the secure world */
-	ret = qcom_pas_auth_and_reset(pasid);
+	ret = qcom_pas_prepare_and_auth_reset(&ctx);
 
 	/*
 	 * If the pas call returns -EOPNOTSUPP we assume that this target
@@ -165,9 +158,6 @@ static int zap_shader_load_mdt(struct msm_gpu *gpu, const char *fwname,
 		DRM_DEV_ERROR(dev, "Unable to authorize the image\n");
 
 out:
-	if (mem_region)
-		memunmap(mem_region);
-
 	release_firmware(fw);
 
 	return ret;
Signature · 1 line
2.55.0
#4 lore raw

Re: [PATCH v3 3/9] drm/msm/adreno: migrate zap shader loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/msm/adreno: missing qcom_pas_metadata_release() in zap_shader_load_mdt() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/msm/adreno: missing qcom_pas_metadata_release() in zap_shader_load_mdt()
--

commit d13b2477d9bf69676164fdb8068bcf949f328d1e
Author: Mukesh Ojha <[email protected]>

drm/msm/adreno: migrate zap shader loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

This commit replaces qcom_mdt_load() and qcom_pas_auth_and_reset() in zap_shader_load_mdt() with qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset() using a stack-allocated struct qcom_pas_context.

Quoted 63 lines
drivers/gpu/drm/msm/adreno/adreno_gpu.c +6 -13
index dadfe4c694384..12e35841763d8 100644
@@ -34,11 +34,11 @@ static int zap_shader_load_mdt(struct msm_gpu *gpu, const char *fwname,
 	struct device *dev = &gpu->pdev->dev;
 	const struct firmware *fw;
 	const char *signed_fwname = NULL;
+	struct qcom_pas_context ctx;
 	struct device_node *np;
 	struct resource r;
 	phys_addr_t mem_phys;
 	ssize_t mem_size;
-	void *mem_region = NULL;
 	int ret;
 
 	if (!IS_ENABLED(CONFIG_ARCH_QCOM)) {
@@ -121,39 +121,32 @@ static int zap_shader_load_mdt(struct msm_gpu *gpu, const char *fwname,
 		goto out;
 	}
 
-	/* Allocate memory for the firmware image */
-	mem_region = memremap(mem_phys, mem_size,  MEMREMAP_WC);
-	if (!mem_region) {
-		ret = -ENOMEM;
-		goto out;
-	}
+	qcom_pas_context_init(&ctx, dev, pasid, mem_phys, mem_size);
 
 	/*
 	 * Load the rest of the MDT
 	 *
 	 * Note that we could be dealing with two different paths, since
 	 * with upstream linux-firmware it would be in a qcom/ subdir..
-	 * adreno_request_fw() handles this, but qcom_mdt_load() does
+	 * adreno_request_fw() handles this, but qcom_mdt_pas_load() does
 	 * not.  But since we've already gotten through adreno_request_fw()
 	 * we know which of the two cases it is:
 	 */
 	if (signed_fwname || (to_adreno_gpu(gpu)->fwloc == FW_LOCATION_LEGACY)) {
-		ret = qcom_mdt_load(dev, fw, fwname, pasid,
-				mem_region, mem_phys, mem_size, NULL);
+		ret = qcom_mdt_pas_load(&ctx, fw, fwname, NULL);
 	} else {
 		char *newname;
 
 		newname = kasprintf(GFP_KERNEL, "qcom/%s", fwname);
 
-		ret = qcom_mdt_load(dev, fw, newname, pasid,
-				mem_region, mem_phys, mem_size, NULL);
+		ret = qcom_mdt_pas_load(&ctx, fw, newname, NULL);
 		kfree(newname);
 	}
 	if (ret)
 		goto out;
 
 	/* Send the image to the secure world */
-	ret = qcom_pas_auth_and_reset(pasid);
+	ret = qcom_pas_prepare_and_auth_reset(&ctx);
 
 	/*
 	 * If the pas call returns -EOPNOTSUPP we assume that this target

drivers/gpu/drm/msm/adreno/adreno_gpu.c static int zap_shader_load_mdt(struct msm_gpu *gpu, const char *fwname, · line 163

Quoted 9 lines
drivers/gpu/drm/msm/adreno/adreno_gpu.c +0 -3
@@ -165,9 +158,6 @@ static int zap_shader_load_mdt(struct msm_gpu *gpu, const char *fwname,
 		DRM_DEV_ERROR(dev, "Unable to authorize the image\n");
 
 out:
-	if (mem_region)
-		memunmap(mem_region);
-
 	release_firmware(fw);
 
 	return ret;

[Severity: High]
Does zap_shader_load_mdt() leak the PAS metadata buffer stored in ctx.ptr?

Previously, qcom_mdt_load() passed a NULL struct qcom_pas_context to __qcom_mdt_pas_init(), which caused qcom_scm_pas_init_image() and qcom_pas_tee_init_image() to free the metadata buffer immediately. Passing non-NULL &ctx via qcom_mdt_pas_load() retains the metadata allocation in ctx.ptr, which is leaked when ctx goes out of scope unless qcom_pas_metadata_release(&ctx) is called before returning. Note that 2aa8fafc08e7 ("firmware: qcom: pas: Make MDT buffer retention explicit") changes qcom_pas_init_image() to release metadata unless keep_mdt_buf is set, but that commit is not present in this tree.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#5 lore raw

Re: [PATCH v3 3/9] drm/msm/adreno: migrate zap shader loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

Quoted 92 lines · On Tue, Nov 17, 2026 at 04:24:10PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/msm/adreno: missing qcom_pas_metadata_release() in zap_shader_load_mdt()
--

commit d13b2477d9bf69676164fdb8068bcf949f328d1e
Author: Mukesh Ojha <[email protected]>

drm/msm/adreno: migrate zap shader loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

This commit replaces qcom_mdt_load() and qcom_pas_auth_and_reset() in zap_shader_load_mdt() with qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset() using a stack-allocated struct qcom_pas_context.

Quoted 63 lines
drivers/gpu/drm/msm/adreno/adreno_gpu.c +6 -13
index dadfe4c694384..12e35841763d8 100644
@@ -34,11 +34,11 @@ static int zap_shader_load_mdt(struct msm_gpu *gpu, const char *fwname,
 	struct device *dev = &gpu->pdev->dev;
 	const struct firmware *fw;
 	const char *signed_fwname = NULL;
+	struct qcom_pas_context ctx;
 	struct device_node *np;
 	struct resource r;
 	phys_addr_t mem_phys;
 	ssize_t mem_size;
-	void *mem_region = NULL;
 	int ret;
 
 	if (!IS_ENABLED(CONFIG_ARCH_QCOM)) {
@@ -121,39 +121,32 @@ static int zap_shader_load_mdt(struct msm_gpu *gpu, const char *fwname,
 		goto out;
 	}
 
-	/* Allocate memory for the firmware image */
-	mem_region = memremap(mem_phys, mem_size,  MEMREMAP_WC);
-	if (!mem_region) {
-		ret = -ENOMEM;
-		goto out;
-	}
+	qcom_pas_context_init(&ctx, dev, pasid, mem_phys, mem_size);
 
 	/*
 	 * Load the rest of the MDT
 	 *
 	 * Note that we could be dealing with two different paths, since
 	 * with upstream linux-firmware it would be in a qcom/ subdir..
-	 * adreno_request_fw() handles this, but qcom_mdt_load() does
+	 * adreno_request_fw() handles this, but qcom_mdt_pas_load() does
 	 * not.  But since we've already gotten through adreno_request_fw()
 	 * we know which of the two cases it is:
 	 */
 	if (signed_fwname || (to_adreno_gpu(gpu)->fwloc == FW_LOCATION_LEGACY)) {
-		ret = qcom_mdt_load(dev, fw, fwname, pasid,
-				mem_region, mem_phys, mem_size, NULL);
+		ret = qcom_mdt_pas_load(&ctx, fw, fwname, NULL);
 	} else {
 		char *newname;
 
 		newname = kasprintf(GFP_KERNEL, "qcom/%s", fwname);
 
-		ret = qcom_mdt_load(dev, fw, newname, pasid,
-				mem_region, mem_phys, mem_size, NULL);
+		ret = qcom_mdt_pas_load(&ctx, fw, newname, NULL);
 		kfree(newname);
 	}
 	if (ret)
 		goto out;
 
 	/* Send the image to the secure world */
-	ret = qcom_pas_auth_and_reset(pasid);
+	ret = qcom_pas_prepare_and_auth_reset(&ctx);
 
 	/*
 	 * If the pas call returns -EOPNOTSUPP we assume that this target

drivers/gpu/drm/msm/adreno/adreno_gpu.c static int zap_shader_load_mdt(struct msm_gpu *gpu, const char *fwname, · line 163

Quoted 9 lines
drivers/gpu/drm/msm/adreno/adreno_gpu.c +0 -3
@@ -165,9 +158,6 @@ static int zap_shader_load_mdt(struct msm_gpu *gpu, const char *fwname,
 		DRM_DEV_ERROR(dev, "Unable to authorize the image\n");
 
 out:
-	if (mem_region)
-		memunmap(mem_region);
-
 	release_firmware(fw);
 
 	return ret;

[Severity: High]
Does zap_shader_load_mdt() leak the PAS metadata buffer stored in ctx.ptr?

Quoted 9 lines

Previously, qcom_mdt_load() passed a NULL struct qcom_pas_context to __qcom_mdt_pas_init(), which caused qcom_scm_pas_init_image() and qcom_pas_tee_init_image() to free the metadata buffer immediately. Passing non-NULL &ctx via qcom_mdt_pas_load() retains the metadata allocation in ctx.ptr, which is leaked when ctx goes out of scope unless qcom_pas_metadata_release(&ctx) is called before returning. Note that 2aa8fafc08e7 ("firmware: qcom: pas: Make MDT buffer retention explicit") changes qcom_pas_init_image() to release metadata unless keep_mdt_buf is set, but that commit is not present in this tree.

Yes, that commit is present in the next; so no issue.

Quoted 3 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Signature · 1 line
-Mukesh Ojha
#6 lore raw

PATCH v3 4/9 media: iris: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

In reply to Mukesh Ojha

iris_load_fw_to_memory() calls qcom_mdt_load(), which bundles metadata initialization, memory setup, and segment loading into a single API that does not expose a PAS context. iris_fw_load() calls qcom_pas_auth_and_reset() for TrustZone authentication, which is the intended behaviour. However, a new qcom_mdt_pas_load() has recently been introduced that works with a PAS context and can handle everything qcom_mdt_load() provides, along with additional functionality such as shmbridge creation and retrieving memory from the shmbridge buffer when the same remote processor needs to run while Linux runs at EL2.

Embed a struct qcom_pas_context directly in struct iris_core and initialize it with qcom_pas_context_init() inside iris_load_fw_to_memory() on each call. Replace qcom_mdt_load() with qcom_mdt_pas_load() and replace qcom_pas_auth_and_reset() with qcom_pas_prepare_and_auth_reset(). The iris_detect_firmware() scan retains a separate memremap window after qcom_mdt_pas_load() completes, since it needs to read back the loaded image data.

---
 drivers/media/platform/qcom/iris/iris_core.h  |  2 ++
 .../media/platform/qcom/iris/iris_firmware.c  | 27 +++++++++----------
 2 files changed, 15 insertions(+), 14 deletions(-)
drivers/media/platform/qcom/iris/iris_core.h +2 -0
index 3e4cf652bf39..ac73bda072f8 100644
@@ -9,6 +9,7 @@
 #include <linux/types.h>
 #include <linux/dma-mapping.h>
 #include <linux/pm_domain.h>
+#include <linux/firmware/qcom/qcom_pas.h>
 #include <media/v4l2-device.h>
 
 #include "iris_hfi_common.h"
@@ -129,6 +130,7 @@ struct iris_core {
 	struct platform_inst_fw_cap		inst_fw_caps_dec[INST_FW_CAP_MAX];
 	struct platform_inst_fw_cap		inst_fw_caps_enc[INST_FW_CAP_MAX];
 	struct dma_iova_state			*iova_state;
+	struct qcom_pas_context			pas_ctx;
 };
 
 int iris_core_init(struct iris_core *core);
drivers/media/platform/qcom/iris/iris_firmware.c +13 -14
index 21f57d37da18..b701a7f43514 100644
@@ -201,21 +201,20 @@ static int iris_load_fw_to_memory(struct iris_core *core)
 		goto err_release_fw;
 	}
 
-	mem_virt = memremap(mem_phys, res_size, MEMREMAP_WC);
-	if (!mem_virt) {
-		ret = -ENOMEM;
-		goto err_release_fw;
-	}
-
-	memset(mem_virt, 0, res_size);
-
-	ret = qcom_mdt_load(dev, firmware, fw_name,
-			    IRIS_PAS_ID, mem_virt, mem_phys, res_size, NULL);
+	qcom_pas_context_init(&core->pas_ctx, dev, IRIS_PAS_ID, mem_phys,
+			      res_size);
+
+	ret = qcom_mdt_pas_load(&core->pas_ctx, firmware, fw_name, NULL);
+	if (!ret) {
+		mem_virt = memremap(mem_phys, res_size, MEMREMAP_WC);
+		if (!mem_virt) {
+			ret = -ENOMEM;
+			goto err_release_fw;
+		}
 
-	if (!ret)
 		iris_detect_firmware(core, fw_name, mem_virt, res_size, desc);
-
-	memunmap(mem_virt);
+		memunmap(mem_virt);
+	}
 err_release_fw:
 	release_firmware(firmware);
 
@@ -233,7 +232,7 @@ int iris_fw_load(struct iris_core *core)
 		return ret;
 	}
 
-	ret = qcom_pas_auth_and_reset(IRIS_PAS_ID);
+	ret = qcom_pas_prepare_and_auth_reset(&core->pas_ctx);
 	if (ret)  {
 		dev_err(core->dev, "auth and reset failed: %d\n", ret);
 		return ret;
Signature · 1 line
2.55.0
#7 lore raw

Re: [PATCH v3 4/9] media: iris: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] media: iris: missing qcom_pas_metadata_release() in iris_fw_load() - [Medium] media: iris: unzeroed…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] media: iris: missing qcom_pas_metadata_release() in iris_fw_load()
- [Medium] media: iris: unzeroed carveout memory scanned by iris_detect_firmware()
- [Low] media: iris: missing kernel-doc entry for pas_ctx in struct iris_core
--

commit 06c574841baf80a5b2f096e918c8cad3e79ad9f0
Author: Mukesh Ojha <[email protected]>

media: iris: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

This commit embeds a struct qcom_pas_context in struct iris_core and migrates Iris firmware loading from qcom_mdt_load() and qcom_pas_auth_and_reset() to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset().

Quoted 4 lines
drivers/media/platform/qcom/iris/iris_core.h +0 -0
index 3e4cf652bf39a..ac73bda072f85 100644

[ ... ]

drivers/media/platform/qcom/iris/iris_core.h struct iris_core { · line 134

Quoted 6 lines
drivers/media/platform/qcom/iris/iris_core.h +1 -0
@@ -129,6 +130,7 @@ struct iris_core {
 	struct platform_inst_fw_cap		inst_fw_caps_dec[INST_FW_CAP_MAX];
 	struct platform_inst_fw_cap		inst_fw_caps_enc[INST_FW_CAP_MAX];
 	struct dma_iova_state			*iova_state;
+	struct qcom_pas_context			pas_ctx;
 };

[Severity: Low]
struct iris_core has a kernel-doc comment block describing its members, so adding pas_ctx without an @pas_ctx entry will trigger a kernel-doc warning.

Quoted 25 lines
drivers/media/platform/qcom/iris/iris_firmware.c +7 -10
index 21f57d37da187..b701a7f43514b 100644
@@ -201,21 +201,20 @@ static int iris_load_fw_to_memory(struct iris_core *core)
 		goto err_release_fw;
 	}
 
-	mem_virt = memremap(mem_phys, res_size, MEMREMAP_WC);
-	if (!mem_virt) {
-		ret = -ENOMEM;
-		goto err_release_fw;
-	}
-
-	memset(mem_virt, 0, res_size);
-
-	ret = qcom_mdt_load(dev, firmware, fw_name,
-			    IRIS_PAS_ID, mem_virt, mem_phys, res_size, NULL);
+	qcom_pas_context_init(&core->pas_ctx, dev, IRIS_PAS_ID, mem_phys,
+			      res_size);
+
+	ret = qcom_mdt_pas_load(&core->pas_ctx, firmware, fw_name, NULL);
+	if (!ret) {
+		mem_virt = memremap(mem_phys, res_size, MEMREMAP_WC);
+		if (!mem_virt) {

drivers/media/platform/qcom/iris/iris_firmware.c static int iris_load_fw_to_memory(struct iris_core *core) · line 217

Quoted 10 lines
drivers/media/platform/qcom/iris/iris_firmware.c +5 -3
+			ret = -ENOMEM;
+			goto err_release_fw;
+		}
 
-	if (!ret)
 		iris_detect_firmware(core, fw_name, mem_virt, res_size, desc);
-
-	memunmap(mem_virt);
+		memunmap(mem_virt);
+	}

[Severity: Medium]
Can removing memset(mem_virt, 0, res_size) cause iris_detect_firmware() to scan uninitialized carveout memory?

qcom_mdt_pas_load() -> qcom_mdt_load_no_init() only populates individual PT_LOAD segments and their BSS tails, leaving inter-segment gaps and the trailing [fw_size, res_size) region untouched. When iris_detect_firmware() subsequently scans all res_size bytes via iris_detect_gen2_from_fwdata() on a dual-generation platform with a firmware-name override, stale data in the unwritten carveout (for example after a warm reboot or prior Gen2 load) can cause a smaller Gen1 firmware image to be misclassified as Gen2.

[ ... ]

drivers/media/platform/qcom/iris/iris_firmware.c int iris_fw_load(struct iris_core *core) · line 238

Quoted 9 lines
drivers/media/platform/qcom/iris/iris_firmware.c +1 -1
@@ -233,7 +232,7 @@ int iris_fw_load(struct iris_core *core)
 		return ret;
 	}
 
-	ret = qcom_pas_auth_and_reset(IRIS_PAS_ID);
+	ret = qcom_pas_prepare_and_auth_reset(&core->pas_ctx);
 	if (ret)  {
 		dev_err(core->dev, "auth and reset failed: %d\n", ret);
 		return ret;

[Severity: High]
As reported by Konrad Dybcio on v1 [1], does this leak the PAS metadata buffer stored in core->pas_ctx.ptr?

Unlike qcom_mdt_load(), which passes a NULL context to __qcom_mdt_pas_init(), qcom_mdt_pas_load() passes &core->pas_ctx to qcom_pas_init_image(), which retains the allocated DMA or TEE metadata buffer in core->pas_ctx.ptr:

drivers/firmware/qcom/qcom_scm.c:qcom_scm_pas_init_image() {
	...
	if (ret < 0 || !ctx) {
		dma_free_coherent(dev, size, mdata_buf, mdata_phys);
	} else if (ctx) {
		ctx->ptr = mdata_buf;
		ctx->phys = mdata_phys;
		ctx->size = size;
	}
	...
}

Neither qcom_pas_prepare_and_auth_reset() nor iris_firmware.c calls qcom_pas_metadata_release(&core->pas_ctx), so the metadata buffer is leaked on every firmware load (and overwritten when qcom_pas_context_init() zeroes core->pas_ctx on subsequent loads or error recovery).

[1] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#8 lore raw

Re: [PATCH v3 4/9] media: iris: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

Quoted 25 lines · On Tue, Nov 17, 2026 at 04:24:10PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] media: iris: missing qcom_pas_metadata_release() in iris_fw_load()
- [Medium] media: iris: unzeroed carveout memory scanned by iris_detect_firmware()
- [Low] media: iris: missing kernel-doc entry for pas_ctx in struct iris_core
--

commit 06c574841baf80a5b2f096e918c8cad3e79ad9f0
Author: Mukesh Ojha <[email protected]>

media: iris: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

This commit embeds a struct qcom_pas_context in struct iris_core and migrates Iris firmware loading from qcom_mdt_load() and qcom_pas_auth_and_reset() to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset().

Quoted 4 lines
drivers/media/platform/qcom/iris/iris_core.h +0 -0
index 3e4cf652bf39a..ac73bda072f85 100644

[ ... ]

Quoted 10 lines

drivers/media/platform/qcom/iris/iris_core.h struct iris_core { · line 134

Quoted 6 lines
drivers/media/platform/qcom/iris/iris_core.h +1 -0
@@ -129,6 +130,7 @@ struct iris_core {
 	struct platform_inst_fw_cap		inst_fw_caps_dec[INST_FW_CAP_MAX];
 	struct platform_inst_fw_cap		inst_fw_caps_enc[INST_FW_CAP_MAX];
 	struct dma_iova_state			*iova_state;
+	struct qcom_pas_context			pas_ctx;
 };

[Severity: Low]
struct iris_core has a kernel-doc comment block describing its members, so adding pas_ctx without an @pas_ctx entry will trigger a kernel-doc warning.

Ack.

Quoted 41 lines
Quoted 26 lines
drivers/media/platform/qcom/iris/iris_firmware.c +7 -10
index 21f57d37da187..b701a7f43514b 100644
@@ -201,21 +201,20 @@ static int iris_load_fw_to_memory(struct iris_core *core)
 		goto err_release_fw;
 	}
 
-	mem_virt = memremap(mem_phys, res_size, MEMREMAP_WC);
-	if (!mem_virt) {
-		ret = -ENOMEM;
-		goto err_release_fw;
-	}
-
-	memset(mem_virt, 0, res_size);
-
-	ret = qcom_mdt_load(dev, firmware, fw_name,
-			    IRIS_PAS_ID, mem_virt, mem_phys, res_size, NULL);
+	qcom_pas_context_init(&core->pas_ctx, dev, IRIS_PAS_ID, mem_phys,
+			      res_size);
+
+	ret = qcom_mdt_pas_load(&core->pas_ctx, firmware, fw_name, NULL);
+	if (!ret) {
+		mem_virt = memremap(mem_phys, res_size, MEMREMAP_WC);
+		if (!mem_virt) {

drivers/media/platform/qcom/iris/iris_firmware.c static int iris_load_fw_to_memory(struct iris_core *core) · line 217

Quoted 10 lines
drivers/media/platform/qcom/iris/iris_firmware.c +5 -3
+			ret = -ENOMEM;
+			goto err_release_fw;
+		}
 
-	if (!ret)
 		iris_detect_firmware(core, fw_name, mem_virt, res_size, desc);
-
-	memunmap(mem_virt);
+		memunmap(mem_virt);
+	}

[Severity: Medium]
Can removing memset(mem_virt, 0, res_size) cause iris_detect_firmware() to scan uninitialized carveout memory?

Quoted 8 lines

qcom_mdt_pas_load() -> qcom_mdt_load_no_init() only populates individual PT_LOAD segments and their BSS tails, leaving inter-segment gaps and the trailing [fw_size, res_size) region untouched. When iris_detect_firmware() subsequently scans all res_size bytes via iris_detect_gen2_from_fwdata() on a dual-generation platform with a firmware-name override, stale data in the unwritten carveout (for example after a warm reboot or prior Gen2 load) can cause a smaller Gen1 firmware image to be misclassified as Gen2.

I could restore it, if it is required.

Quoted 33 lines

[ ... ]

drivers/media/platform/qcom/iris/iris_firmware.c int iris_fw_load(struct iris_core *core) · line 238

Quoted 9 lines
drivers/media/platform/qcom/iris/iris_firmware.c +1 -1
@@ -233,7 +232,7 @@ int iris_fw_load(struct iris_core *core)
 		return ret;
 	}
 
-	ret = qcom_pas_auth_and_reset(IRIS_PAS_ID);
+	ret = qcom_pas_prepare_and_auth_reset(&core->pas_ctx);
 	if (ret)  {
 		dev_err(core->dev, "auth and reset failed: %d\n", ret);
 		return ret;

[Severity: High]
As reported by Konrad Dybcio on v1 [1], does this leak the PAS metadata buffer stored in core->pas_ctx.ptr?

Unlike qcom_mdt_load(), which passes a NULL context to __qcom_mdt_pas_init(), qcom_mdt_pas_load() passes &core->pas_ctx to qcom_pas_init_image(), which retains the allocated DMA or TEE metadata buffer in core->pas_ctx.ptr:

drivers/firmware/qcom/qcom_scm.c:qcom_scm_pas_init_image() {
	...
	if (ret < 0 || !ctx) {
		dma_free_coherent(dev, size, mdata_buf, mdata_phys);
	} else if (ctx) {
		ctx->ptr = mdata_buf;
		ctx->phys = mdata_phys;
		ctx->size = size;
	}
	...
}
Quoted 5 lines

Neither qcom_pas_prepare_and_auth_reset() nor iris_firmware.c calls qcom_pas_metadata_release(&core->pas_ctx), so the metadata buffer is leaked on every firmware load (and overwritten when qcom_pas_context_init() zeroes core->pas_ctx on subsequent loads or error recovery).

There is no more leakage as it is using stack variable and qcom_pas_metadata_release() call happen internal to qcom_mdt_pas_load(). So, no issue.

Quoted 4 lines

[1] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Signature · 1 line
-Mukesh Ojha
Reviewed-by on v3 4/9
#9 lore raw

Re: [PATCH v3 4/9] media: iris: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

In reply to Mukesh Ojha
Quoted 85 lines · Mukesh Ojha

iris_load_fw_to_memory() calls qcom_mdt_load(), which bundles metadata initialization, memory setup, and segment loading into a single API that does not expose a PAS context. iris_fw_load() calls qcom_pas_auth_and_reset() for TrustZone authentication, which is the intended behaviour. However, a new qcom_mdt_pas_load() has recently been introduced that works with a PAS context and can handle everything qcom_mdt_load() provides, along with additional functionality such as shmbridge creation and retrieving memory from the shmbridge buffer when the same remote processor needs to run while Linux runs at EL2.

Embed a struct qcom_pas_context directly in struct iris_core and initialize it with qcom_pas_context_init() inside iris_load_fw_to_memory() on each call. Replace qcom_mdt_load() with qcom_mdt_pas_load() and replace qcom_pas_auth_and_reset() with qcom_pas_prepare_and_auth_reset(). The iris_detect_firmware() scan retains a separate memremap window after qcom_mdt_pas_load() completes, since it needs to read back the loaded image data.

---
 drivers/media/platform/qcom/iris/iris_core.h  |  2 ++
 .../media/platform/qcom/iris/iris_firmware.c  | 27 +++++++++----------
 2 files changed, 15 insertions(+), 14 deletions(-)
drivers/media/platform/qcom/iris/iris_core.h +2 -0
index 3e4cf652bf39..ac73bda072f8 100644
@@ -9,6 +9,7 @@
 #include <linux/types.h>
 #include <linux/dma-mapping.h>
 #include <linux/pm_domain.h>
+#include <linux/firmware/qcom/qcom_pas.h>
 #include <media/v4l2-device.h>
 
 #include "iris_hfi_common.h"
@@ -129,6 +130,7 @@ struct iris_core {
 	struct platform_inst_fw_cap		inst_fw_caps_dec[INST_FW_CAP_MAX];
 	struct platform_inst_fw_cap		inst_fw_caps_enc[INST_FW_CAP_MAX];
 	struct dma_iova_state			*iova_state;
+	struct qcom_pas_context			pas_ctx;
 };
 
 int iris_core_init(struct iris_core *core);
drivers/media/platform/qcom/iris/iris_firmware.c +12 -13
index 21f57d37da18..b701a7f43514 100644
@@ -201,21 +201,20 @@ static int iris_load_fw_to_memory(struct iris_core *core)
 		goto err_release_fw;
 	}
 
-	mem_virt = memremap(mem_phys, res_size, MEMREMAP_WC);
-	if (!mem_virt) {
-		ret = -ENOMEM;
-		goto err_release_fw;
-	}
-
-	memset(mem_virt, 0, res_size);
-
-	ret = qcom_mdt_load(dev, firmware, fw_name,
-			    IRIS_PAS_ID, mem_virt, mem_phys, res_size, NULL);
+	qcom_pas_context_init(&core->pas_ctx, dev, IRIS_PAS_ID, mem_phys,
+			      res_size);
+
+	ret = qcom_mdt_pas_load(&core->pas_ctx, firmware, fw_name, NULL);
+	if (!ret) {
+		mem_virt = memremap(mem_phys, res_size, MEMREMAP_WC);
+		if (!mem_virt) {
+			ret = -ENOMEM;
+			goto err_release_fw;
+		}
 
-	if (!ret)
 		iris_detect_firmware(core, fw_name, mem_virt, res_size, desc);
-
-	memunmap(mem_virt);
+		memunmap(mem_virt);
+	}
 err_release_fw:
 	release_firmware(firmware);
 

drivers/media/platform/qcom/iris/iris_firmware.c int iris_fw_load(struct iris_core *core) · line 238

Quoted 8 lines
drivers/media/platform/qcom/iris/iris_firmware.c +1 -1
@@ -233,7 +232,7 @@ int iris_fw_load(struct iris_core *core)
 		return ret;
 	}
 
-	ret = qcom_pas_auth_and_reset(IRIS_PAS_ID);
+	ret = qcom_pas_prepare_and_auth_reset(&core->pas_ctx);
 	if (ret)  {
 		dev_err(core->dev, "auth and reset failed: %d\n", ret);
 		return ret;

Please address sashiko comments in the next revision, as they are only minor. With that,

JFYI, depending on which series lands first, either I'll rebase accordingly, or this series can be rebased on top of:
https://lore.kernel.org/all/[email protected]

#10 lore raw

Re: [PATCH v3 4/9] media: iris: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

In reply to Mukesh Ojha
Quoted 51 lines · Mukesh Ojha

iris_load_fw_to_memory() calls qcom_mdt_load(), which bundles metadata initialization, memory setup, and segment loading into a single API that does not expose a PAS context. iris_fw_load() calls qcom_pas_auth_and_reset() for TrustZone authentication, which is the intended behaviour. However, a new qcom_mdt_pas_load() has recently been introduced that works with a PAS context and can handle everything qcom_mdt_load() provides, along with additional functionality such as shmbridge creation and retrieving memory from the shmbridge buffer when the same remote processor needs to run while Linux runs at EL2.

Embed a struct qcom_pas_context directly in struct iris_core and initialize it with qcom_pas_context_init() inside iris_load_fw_to_memory() on each call. Replace qcom_mdt_load() with qcom_mdt_pas_load() and replace qcom_pas_auth_and_reset() with qcom_pas_prepare_and_auth_reset(). The iris_detect_firmware() scan retains a separate memremap window after qcom_mdt_pas_load() completes, since it needs to read back the loaded image data.

---
 drivers/media/platform/qcom/iris/iris_core.h  |  2 ++
 .../media/platform/qcom/iris/iris_firmware.c  | 27 +++++++++----------
 2 files changed, 15 insertions(+), 14 deletions(-)
drivers/media/platform/qcom/iris/iris_core.h +2 -0
index 3e4cf652bf39..ac73bda072f8 100644
@@ -9,6 +9,7 @@
 #include <linux/types.h>
 #include <linux/dma-mapping.h>
 #include <linux/pm_domain.h>
+#include <linux/firmware/qcom/qcom_pas.h>
 #include <media/v4l2-device.h>
 
 #include "iris_hfi_common.h"
@@ -129,6 +130,7 @@ struct iris_core {
 	struct platform_inst_fw_cap		inst_fw_caps_dec[INST_FW_CAP_MAX];
 	struct platform_inst_fw_cap		inst_fw_caps_enc[INST_FW_CAP_MAX];
 	struct dma_iova_state			*iova_state;
+	struct qcom_pas_context			pas_ctx;
 };
 
 int iris_core_init(struct iris_core *core);
drivers/media/platform/qcom/iris/iris_firmware.c +0 -0
index 21f57d37da18..b701a7f43514 100644

drivers/media/platform/qcom/iris/iris_firmware.c static int iris_load_fw_to_memory(struct iris_core *core) · line 210

Quoted 10 lines
drivers/media/platform/qcom/iris/iris_firmware.c +0 -7
@@ -201,21 +201,20 @@ static int iris_load_fw_to_memory(struct iris_core *core)
 		goto err_release_fw;
 	}
 
-	mem_virt = memremap(mem_phys, res_size, MEMREMAP_WC);
-	if (!mem_virt) {
-		ret = -ENOMEM;
-		goto err_release_fw;
-	}
-
-	memset(mem_virt, 0, res_size);

Restoring memset() is needed.
iris_detect_firmware() scans the whole res_size window for "QC_IMAGE_VERSION_STRING=", but the loader only writes the firmware segments, so the rest of the carveout can hold stale data. With a firmware-name override on a platform that has both Gen1 and Gen2 descriptors, this could make a Gen1 image look like Gen2.

Quoted 24 lines
drivers/media/platform/qcom/iris/iris_firmware.c +12 -6
-
-	ret = qcom_mdt_load(dev, firmware, fw_name,
-			    IRIS_PAS_ID, mem_virt, mem_phys, res_size, NULL);
+	qcom_pas_context_init(&core->pas_ctx, dev, IRIS_PAS_ID, mem_phys,
+			      res_size);
+
+	ret = qcom_mdt_pas_load(&core->pas_ctx, firmware, fw_name, NULL);
+	if (!ret) {
+		mem_virt = memremap(mem_phys, res_size, MEMREMAP_WC);
+		if (!mem_virt) {
+			ret = -ENOMEM;
+			goto err_release_fw;
+		}
 
-	if (!ret)
 		iris_detect_firmware(core, fw_name, mem_virt, res_size, desc);
-
-	memunmap(mem_virt);
+		memunmap(mem_virt);
+	}
 err_release_fw:
 	release_firmware(firmware);
 

drivers/media/platform/qcom/iris/iris_firmware.c int iris_fw_load(struct iris_core *core) · line 238

Quoted 8 lines
drivers/media/platform/qcom/iris/iris_firmware.c +1 -1
@@ -233,7 +232,7 @@ int iris_fw_load(struct iris_core *core)
 		return ret;
 	}
 
-	ret = qcom_pas_auth_and_reset(IRIS_PAS_ID);
+	ret = qcom_pas_prepare_and_auth_reset(&core->pas_ctx);
 	if (ret)  {
 		dev_err(core->dev, "auth and reset failed: %d\n", ret);
 		return ret;

- Gourav Kumar

#11 lore raw

PATCH v3 5/9 media: venus: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

In reply to Mukesh Ojha

venus_load_fw() calls qcom_mdt_load(), which bundles metadata initialization, memory setup, and segment loading into a single API that does not expose a PAS context. venus_boot() calls qcom_pas_auth_and_reset() for TrustZone authentication, which is the intended behaviour. However, a new qcom_mdt_pas_load() has recently been introduced that works with a PAS context and can handle everything qcom_mdt_load() provides, along with additional functionality such as shmbridge creation and retrieving memory from the shmbridge buffer when the same remote processor needs to run while Linux runs at EL2.

Embed a struct qcom_pas_context in struct video_firmware and initialize it with qcom_pas_context_init() inside venus_load_fw() on each call. Replace qcom_mdt_load() with qcom_mdt_pas_load() for the TZ path and replace qcom_pas_auth_and_reset() with qcom_pas_prepare_and_auth_reset(). The non-TZ path retains its memremap()/qcom_mdt_load_no_init()/memunmap() sequence since qcom_mdt_load_no_init() requires a caller-supplied mapping.

---
 drivers/media/platform/qcom/venus/core.h     |  2 ++
 drivers/media/platform/qcom/venus/firmware.c | 29 ++++++++++----------
 2 files changed, 17 insertions(+), 14 deletions(-)
drivers/media/platform/qcom/venus/core.h +2 -0
index 93fa77f05834..84a0d4a8426c 100644
@@ -10,6 +10,7 @@
 #include <linux/bitops.h>
 #include <linux/dma-mapping.h>
 #include <linux/list.h>
+#include <linux/firmware/qcom/qcom_pas.h>
 #include <media/videobuf2-v4l2.h>
 #include <media/v4l2-ctrls.h>
 #include <media/v4l2-device.h>
@@ -228,6 +229,7 @@ struct venus_core {
 		size_t mapped_mem_size;
 		phys_addr_t mem_phys;
 		size_t mem_size;
+		struct qcom_pas_context pas_ctx;
 	} fw;
 	struct mutex lock;
 	struct list_head instances;
drivers/media/platform/qcom/venus/firmware.c +15 -14
index 3a38ff985822..704343e241dc 100644
@@ -117,21 +117,22 @@ static int venus_load_fw(struct venus_core *core, const char *fwname,
 		goto err_release_fw;
 	}
 
-	mem_va = memremap(*mem_phys, *mem_size, MEMREMAP_WC);
-	if (!mem_va) {
-		dev_err(dev, "unable to map memory region %pa size %#zx\n", mem_phys, *mem_size);
-		ret = -ENOMEM;
-		goto err_release_fw;
-	}
-
-	if (core->use_tz)
-		ret = qcom_mdt_load(dev, mdt, fwname, VENUS_PAS_ID,
-				    mem_va, *mem_phys, *mem_size, NULL);
-	else
+	if (core->use_tz) {
+		qcom_pas_context_init(&core->fw.pas_ctx, dev, VENUS_PAS_ID,
+				      *mem_phys, *mem_size);
+		ret = qcom_mdt_pas_load(&core->fw.pas_ctx, mdt, fwname, NULL);
+	} else {
+		mem_va = memremap(*mem_phys, *mem_size, MEMREMAP_WC);
+		if (!mem_va) {
+			dev_err(dev, "unable to map memory region %pa size %#zx\n",
+				mem_phys, *mem_size);
+			ret = -ENOMEM;
+			goto err_release_fw;
+		}
 		ret = qcom_mdt_load_no_init(dev, mdt, fwname, mem_va,
 					    *mem_phys, *mem_size, NULL);
-
-	memunmap(mem_va);
+		memunmap(mem_va);
+	}
 err_release_fw:
 	release_firmware(mdt);
 	return ret;
@@ -237,7 +238,7 @@ int venus_boot(struct venus_core *core)
 	core->fw.mem_phys = mem_phys;
 
 	if (core->use_tz)
-		ret = qcom_pas_auth_and_reset(VENUS_PAS_ID);
+		ret = qcom_pas_prepare_and_auth_reset(&core->fw.pas_ctx);
 	else
 		ret = venus_boot_no_tz(core, mem_phys, mem_size);
 
Signature · 1 line
2.55.0
#12 lore raw

Re: [PATCH v3 5/9] media: venus: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] media: venus: PAS metadata buffer leak in venus_load_fw() and venus_boot() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] media: venus: PAS metadata buffer leak in venus_load_fw() and venus_boot()
--

commit 197c5c883871fbbcc35bb55913a4e0eb4bf2cd11
Author: Mukesh Ojha <[email protected]>

media: venus: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

This commit embeds a struct qcom_pas_context in struct video_firmware and switches the TrustZone firmware loading path in venus_load_fw() and venus_boot() to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset().

Quoted 41 lines
drivers/media/platform/qcom/venus/firmware.c +14 -13
index 3a38ff985822f..704343e241dc6 100644
@@ -117,21 +117,22 @@ static int venus_load_fw(struct venus_core *core, const char *fwname,
 		goto err_release_fw;
 	}
 
-	mem_va = memremap(*mem_phys, *mem_size, MEMREMAP_WC);
-	if (!mem_va) {
-		dev_err(dev, "unable to map memory region %pa size %#zx\n", mem_phys, *mem_size);
-		ret = -ENOMEM;
-		goto err_release_fw;
-	}
-
-	if (core->use_tz)
-		ret = qcom_mdt_load(dev, mdt, fwname, VENUS_PAS_ID,
-				    mem_va, *mem_phys, *mem_size, NULL);
-	else
+	if (core->use_tz) {
+		qcom_pas_context_init(&core->fw.pas_ctx, dev, VENUS_PAS_ID,
+				      *mem_phys, *mem_size);
+		ret = qcom_mdt_pas_load(&core->fw.pas_ctx, mdt, fwname, NULL);
+	} else {
+		mem_va = memremap(*mem_phys, *mem_size, MEMREMAP_WC);
+		if (!mem_va) {
+			dev_err(dev, "unable to map memory region %pa size %#zx\n",
+				mem_phys, *mem_size);
+			ret = -ENOMEM;
+			goto err_release_fw;
+		}
 		ret = qcom_mdt_load_no_init(dev, mdt, fwname, mem_va,
 					    *mem_phys, *mem_size, NULL);
-
-	memunmap(mem_va);
+		memunmap(mem_va);
+	}
 err_release_fw:
 	release_firmware(mdt);
 	return ret;

drivers/media/platform/qcom/venus/firmware.c int venus_boot(struct venus_core *core) · line 243

Quoted 7 lines
drivers/media/platform/qcom/venus/firmware.c +1 -1
@@ -237,7 +238,7 @@ int venus_boot(struct venus_core *core)
 	core->fw.mem_phys = mem_phys;
 
 	if (core->use_tz)
-		ret = qcom_pas_auth_and_reset(VENUS_PAS_ID);
+		ret = qcom_pas_prepare_and_auth_reset(&core->fw.pas_ctx);
 	else
 		ret = venus_boot_no_tz(core, mem_phys, mem_size);

[Severity: Medium]
As reported by Konrad Dybcio on v1 [1], does this leak the PAS metadata buffer in core->fw.pas_ctx.ptr?

Unlike qcom_mdt_load(), which passes a NULL context to qcom_pas_init_image() so the metadata buffer is freed immediately, qcom_mdt_pas_load() passes &core->fw.pas_ctx, causing qcom_pas_init_image() to retain the metadata allocation in core->fw.pas_ctx.ptr until qcom_pas_metadata_release() is called. Nothing calls qcom_pas_metadata_release(&core->fw.pas_ctx) after qcom_pas_prepare_and_auth_reset() or if qcom_mdt_pas_load() fails after qcom_pas_init_image() succeeds, and subsequent recovery boots via venus_sys_error_handler() -> venus_boot() overwrite core->fw.pas_ctx in qcom_pas_context_init().

[1] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#13 lore raw

Re: [PATCH v3 5/9] media: venus: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

Quoted 68 lines · On Tue, Nov 17, 2026 at 04:24:11PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] media: venus: PAS metadata buffer leak in venus_load_fw() and venus_boot()
--

commit 197c5c883871fbbcc35bb55913a4e0eb4bf2cd11
Author: Mukesh Ojha <[email protected]>

media: venus: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

This commit embeds a struct qcom_pas_context in struct video_firmware and switches the TrustZone firmware loading path in venus_load_fw() and venus_boot() to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset().

Quoted 41 lines
drivers/media/platform/qcom/venus/firmware.c +14 -13
index 3a38ff985822f..704343e241dc6 100644
@@ -117,21 +117,22 @@ static int venus_load_fw(struct venus_core *core, const char *fwname,
 		goto err_release_fw;
 	}
 
-	mem_va = memremap(*mem_phys, *mem_size, MEMREMAP_WC);
-	if (!mem_va) {
-		dev_err(dev, "unable to map memory region %pa size %#zx\n", mem_phys, *mem_size);
-		ret = -ENOMEM;
-		goto err_release_fw;
-	}
-
-	if (core->use_tz)
-		ret = qcom_mdt_load(dev, mdt, fwname, VENUS_PAS_ID,
-				    mem_va, *mem_phys, *mem_size, NULL);
-	else
+	if (core->use_tz) {
+		qcom_pas_context_init(&core->fw.pas_ctx, dev, VENUS_PAS_ID,
+				      *mem_phys, *mem_size);
+		ret = qcom_mdt_pas_load(&core->fw.pas_ctx, mdt, fwname, NULL);
+	} else {
+		mem_va = memremap(*mem_phys, *mem_size, MEMREMAP_WC);
+		if (!mem_va) {
+			dev_err(dev, "unable to map memory region %pa size %#zx\n",
+				mem_phys, *mem_size);
+			ret = -ENOMEM;
+			goto err_release_fw;
+		}
 		ret = qcom_mdt_load_no_init(dev, mdt, fwname, mem_va,
 					    *mem_phys, *mem_size, NULL);
-
-	memunmap(mem_va);
+		memunmap(mem_va);
+	}
 err_release_fw:
 	release_firmware(mdt);
 	return ret;

drivers/media/platform/qcom/venus/firmware.c int venus_boot(struct venus_core *core) · line 243

Quoted 7 lines
drivers/media/platform/qcom/venus/firmware.c +1 -1
@@ -237,7 +238,7 @@ int venus_boot(struct venus_core *core)
 	core->fw.mem_phys = mem_phys;
 
 	if (core->use_tz)
-		ret = qcom_pas_auth_and_reset(VENUS_PAS_ID);
+		ret = qcom_pas_prepare_and_auth_reset(&core->fw.pas_ctx);
 	else
 		ret = venus_boot_no_tz(core, mem_phys, mem_size);

[Severity: Medium]
As reported by Konrad Dybcio on v1 [1], does this leak the PAS metadata buffer in core->fw.pas_ctx.ptr?

Quoted 10 lines

Unlike qcom_mdt_load(), which passes a NULL context to qcom_pas_init_image() so the metadata buffer is freed immediately, qcom_mdt_pas_load() passes &core->fw.pas_ctx, causing qcom_pas_init_image() to retain the metadata allocation in core->fw.pas_ctx.ptr until qcom_pas_metadata_release() is called. Nothing calls qcom_pas_metadata_release(&core->fw.pas_ctx) after qcom_pas_prepare_and_auth_reset() or if qcom_mdt_pas_load() fails after qcom_pas_init_image() succeeds, and subsequent recovery boots via venus_sys_error_handler() -> venus_boot() overwrite core->fw.pas_ctx in qcom_pas_context_init().

No harm.

Change to avoid leakage https://lore.kernel.org/all/[email protected]/
is present in linux-next which takes care of this.

Quoted 5 lines

[1] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Signature · 1 line
-Mukesh Ojha
Reviewed-by on v3 5/9
#14 lore raw

Re: [PATCH v3 5/9] media: venus: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

In reply to Mukesh Ojha
Quoted 84 lines · Mukesh Ojha

venus_load_fw() calls qcom_mdt_load(), which bundles metadata initialization, memory setup, and segment loading into a single API that does not expose a PAS context. venus_boot() calls qcom_pas_auth_and_reset() for TrustZone authentication, which is the intended behaviour. However, a new qcom_mdt_pas_load() has recently been introduced that works with a PAS context and can handle everything qcom_mdt_load() provides, along with additional functionality such as shmbridge creation and retrieving memory from the shmbridge buffer when the same remote processor needs to run while Linux runs at EL2.

Embed a struct qcom_pas_context in struct video_firmware and initialize it with qcom_pas_context_init() inside venus_load_fw() on each call. Replace qcom_mdt_load() with qcom_mdt_pas_load() for the TZ path and replace qcom_pas_auth_and_reset() with qcom_pas_prepare_and_auth_reset(). The non-TZ path retains its memremap()/qcom_mdt_load_no_init()/memunmap() sequence since qcom_mdt_load_no_init() requires a caller-supplied mapping.

---
 drivers/media/platform/qcom/venus/core.h     |  2 ++
 drivers/media/platform/qcom/venus/firmware.c | 29 ++++++++++----------
 2 files changed, 17 insertions(+), 14 deletions(-)
drivers/media/platform/qcom/venus/core.h +2 -0
index 93fa77f05834..84a0d4a8426c 100644
@@ -10,6 +10,7 @@
 #include <linux/bitops.h>
 #include <linux/dma-mapping.h>
 #include <linux/list.h>
+#include <linux/firmware/qcom/qcom_pas.h>
 #include <media/videobuf2-v4l2.h>
 #include <media/v4l2-ctrls.h>
 #include <media/v4l2-device.h>
@@ -228,6 +229,7 @@ struct venus_core {
 		size_t mapped_mem_size;
 		phys_addr_t mem_phys;
 		size_t mem_size;
+		struct qcom_pas_context pas_ctx;
 	} fw;
 	struct mutex lock;
 	struct list_head instances;
drivers/media/platform/qcom/venus/firmware.c +14 -13
index 3a38ff985822..704343e241dc 100644
@@ -117,21 +117,22 @@ static int venus_load_fw(struct venus_core *core, const char *fwname,
 		goto err_release_fw;
 	}
 
-	mem_va = memremap(*mem_phys, *mem_size, MEMREMAP_WC);
-	if (!mem_va) {
-		dev_err(dev, "unable to map memory region %pa size %#zx\n", mem_phys, *mem_size);
-		ret = -ENOMEM;
-		goto err_release_fw;
-	}
-
-	if (core->use_tz)
-		ret = qcom_mdt_load(dev, mdt, fwname, VENUS_PAS_ID,
-				    mem_va, *mem_phys, *mem_size, NULL);
-	else
+	if (core->use_tz) {
+		qcom_pas_context_init(&core->fw.pas_ctx, dev, VENUS_PAS_ID,
+				      *mem_phys, *mem_size);
+		ret = qcom_mdt_pas_load(&core->fw.pas_ctx, mdt, fwname, NULL);
+	} else {
+		mem_va = memremap(*mem_phys, *mem_size, MEMREMAP_WC);
+		if (!mem_va) {
+			dev_err(dev, "unable to map memory region %pa size %#zx\n",
+				mem_phys, *mem_size);
+			ret = -ENOMEM;
+			goto err_release_fw;
+		}
 		ret = qcom_mdt_load_no_init(dev, mdt, fwname, mem_va,
 					    *mem_phys, *mem_size, NULL);
-
-	memunmap(mem_va);
+		memunmap(mem_va);
+	}
 err_release_fw:
 	release_firmware(mdt);
 	return ret;

drivers/media/platform/qcom/venus/firmware.c int venus_boot(struct venus_core *core) · line 244

Quoted 8 lines
drivers/media/platform/qcom/venus/firmware.c +1 -1
@@ -237,7 +238,7 @@ int venus_boot(struct venus_core *core)
 	core->fw.mem_phys = mem_phys;
 
 	if (core->use_tz)
-		ret = qcom_pas_auth_and_reset(VENUS_PAS_ID);
+		ret = qcom_pas_prepare_and_auth_reset(&core->fw.pas_ctx);
 	else
 		ret = venus_boot_no_tz(core, mem_phys, mem_size);
 

Thanks for addressing the earlier comments.

#15 lore raw

PATCH v3 6/9 net: ipa: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

In reply to Mukesh Ojha

ipa_firmware_load() calls qcom_mdt_load(), which bundles metadata initialization, memory setup, and segment loading into a single API that does not expose a PAS context. Later it calls qcom_pas_auth_and_reset() for TrustZone authentication, which is the intended behaviour. However, a new qcom_mdt_pas_load() has recently been introduced that works with a PAS context and can handle everything qcom_mdt_load() provides, along with additional functionality such as shmbridge creation and retrieving memory from the shmbridge buffer when the same remote processor needs to run while Linux runs at EL2.

Replace the open-coded memremap()/qcom_mdt_load()/memunmap() sequence with qcom_pas_context_init() and qcom_mdt_pas_load() and qcom_pas_auth_and_reset() with qcom_pas_prepare_and_auth_reset().

---
 drivers/net/ipa/ipa_main.c | 23 ++++++++---------------
 1 file changed, 8 insertions(+), 15 deletions(-)
drivers/net/ipa/ipa_main.c +8 -15
index 14ac2d2faf7c..0a37741c2285 100644
@@ -585,12 +585,11 @@ static void ipa_deconfig(struct ipa *ipa)
 
 static int ipa_firmware_load(struct device *dev)
 {
+	struct qcom_pas_context ctx;
 	const struct firmware *fw;
 	struct resource res;
 	phys_addr_t phys;
 	const char *path;
-	ssize_t size;
-	void *virt;
 	int ret;
 
 	ret = of_reserved_mem_region_to_resource(dev->of_node, 0, &res);
@@ -615,24 +614,18 @@ static int ipa_firmware_load(struct device *dev)
 	}
 
 	phys = res.start;
-	size = (size_t)resource_size(&res);
-	virt = memremap(phys, size, MEMREMAP_WC);
-	if (!virt) {
-		dev_err(dev, "unable to remap firmware memory\n");
-		ret = -ENOMEM;
-		goto out_release_firmware;
-	}
+	qcom_pas_context_init(&ctx, dev, IPA_PAS_ID, phys, resource_size(&res));
 
-	ret = qcom_mdt_load(dev, fw, path, IPA_PAS_ID, virt, phys, size, NULL);
+	ret = qcom_mdt_pas_load(&ctx, fw, path, NULL);
 	if (ret) {
 		dev_err(dev, "error %d loading \"%s\"\n", ret, path);
-	} else {
-		ret = qcom_pas_auth_and_reset(IPA_PAS_ID);
-		if (ret)
-			dev_err(dev, "error %d authenticating \"%s\"\n", ret, path);
+		goto out_release_firmware;
 	}
 
-	memunmap(virt);
+	ret = qcom_pas_prepare_and_auth_reset(&ctx);
+	if (ret)
+		dev_err(dev, "error %d authenticating \"%s\"\n", ret, path);
+
 out_release_firmware:
 	release_firmware(fw);
 
Signature · 1 line
2.55.0
#16 lore raw

Re: [PATCH v3 6/9] net: ipa: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] net: ipa: leak of PAS metadata buffer in ipa_firmware_load() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] net: ipa: leak of PAS metadata buffer in ipa_firmware_load() --

commit e29d8ec88fcd553654746e0eff27a3767cbabafc
Author: Mukesh Ojha <[email protected]>

net: ipa: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

This commit replaces the open-coded memremap(), qcom_mdt_load(), and qcom_pas_auth_and_reset() sequence in ipa_firmware_load() with qcom_pas_context_init(), qcom_mdt_pas_load(), and qcom_pas_prepare_and_auth_reset().

Quoted 39 lines
drivers/net/ipa/ipa_main.c +3 -14
index 14ac2d2faf7c2..0a37741c22858 100644
@@ -585,12 +585,11 @@ static void ipa_deconfig(struct ipa *ipa)
 
 static int ipa_firmware_load(struct device *dev)
 {
+	struct qcom_pas_context ctx;
 	const struct firmware *fw;
 	struct resource res;
 	phys_addr_t phys;
 	const char *path;
-	ssize_t size;
-	void *virt;
 	int ret;
 
 	ret = of_reserved_mem_region_to_resource(dev->of_node, 0, &res);
@@ -615,24 +614,18 @@ static int ipa_firmware_load(struct device *dev)
 	}
 
 	phys = res.start;
-	size = (size_t)resource_size(&res);
-	virt = memremap(phys, size, MEMREMAP_WC);
-	if (!virt) {
-		dev_err(dev, "unable to remap firmware memory\n");
-		ret = -ENOMEM;
-		goto out_release_firmware;
-	}
+	qcom_pas_context_init(&ctx, dev, IPA_PAS_ID, phys, resource_size(&res));
 
-	ret = qcom_mdt_load(dev, fw, path, IPA_PAS_ID, virt, phys, size, NULL);
+	ret = qcom_mdt_pas_load(&ctx, fw, path, NULL);
 	if (ret) {
 		dev_err(dev, "error %d loading \"%s\"\n", ret, path);
-	} else {
-		ret = qcom_pas_auth_and_reset(IPA_PAS_ID);
-		if (ret)
-			dev_err(dev, "error %d authenticating \"%s\"\n", ret, path);

drivers/net/ipa/ipa_main.c static int ipa_firmware_load(struct device *dev) · line 630

Quoted 10 lines
drivers/net/ipa/ipa_main.c +5 -1
+		goto out_release_firmware;
 	}
 
-	memunmap(virt);
+	ret = qcom_pas_prepare_and_auth_reset(&ctx);
+	if (ret)
+		dev_err(dev, "error %d authenticating \"%s\"\n", ret, path);
+
 out_release_firmware:
 	release_firmware(fw);

[Severity: Medium]
Does this leak the metadata buffer stored in ctx.ptr when ipa_firmware_load() returns?

Previously, qcom_mdt_load() passed a NULL context to __qcom_mdt_pas_init(), so qcom_pas_init_image() freed the metadata buffer immediately. qcom_mdt_pas_load() instead passes &ctx to __qcom_mdt_pas_init() -> qcom_pas_init_image(), which retains the coherent DMA or TEE shared-memory allocation in ctx.ptr until qcom_pas_metadata_release(&ctx) is called. Because ipa_firmware_load() never calls qcom_pas_metadata_release(&ctx), the metadata buffer is leaked on both the normal path and if qcom_mdt_pas_load() fails after qcom_pas_init_image() succeeds.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#17 lore raw

Re: [PATCH v3 6/9] net: ipa: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

Quoted 69 lines · On Tue, Nov 17, 2026 at 04:24:12PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] net: ipa: leak of PAS metadata buffer in ipa_firmware_load() --

commit e29d8ec88fcd553654746e0eff27a3767cbabafc
Author: Mukesh Ojha <[email protected]>

net: ipa: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

This commit replaces the open-coded memremap(), qcom_mdt_load(), and qcom_pas_auth_and_reset() sequence in ipa_firmware_load() with qcom_pas_context_init(), qcom_mdt_pas_load(), and qcom_pas_prepare_and_auth_reset().

Quoted 39 lines
drivers/net/ipa/ipa_main.c +3 -14
index 14ac2d2faf7c2..0a37741c22858 100644
@@ -585,12 +585,11 @@ static void ipa_deconfig(struct ipa *ipa)
 
 static int ipa_firmware_load(struct device *dev)
 {
+	struct qcom_pas_context ctx;
 	const struct firmware *fw;
 	struct resource res;
 	phys_addr_t phys;
 	const char *path;
-	ssize_t size;
-	void *virt;
 	int ret;
 
 	ret = of_reserved_mem_region_to_resource(dev->of_node, 0, &res);
@@ -615,24 +614,18 @@ static int ipa_firmware_load(struct device *dev)
 	}
 
 	phys = res.start;
-	size = (size_t)resource_size(&res);
-	virt = memremap(phys, size, MEMREMAP_WC);
-	if (!virt) {
-		dev_err(dev, "unable to remap firmware memory\n");
-		ret = -ENOMEM;
-		goto out_release_firmware;
-	}
+	qcom_pas_context_init(&ctx, dev, IPA_PAS_ID, phys, resource_size(&res));
 
-	ret = qcom_mdt_load(dev, fw, path, IPA_PAS_ID, virt, phys, size, NULL);
+	ret = qcom_mdt_pas_load(&ctx, fw, path, NULL);
 	if (ret) {
 		dev_err(dev, "error %d loading \"%s\"\n", ret, path);
-	} else {
-		ret = qcom_pas_auth_and_reset(IPA_PAS_ID);
-		if (ret)
-			dev_err(dev, "error %d authenticating \"%s\"\n", ret, path);

drivers/net/ipa/ipa_main.c static int ipa_firmware_load(struct device *dev) · line 630

Quoted 10 lines
drivers/net/ipa/ipa_main.c +5 -1
+		goto out_release_firmware;
 	}
 
-	memunmap(virt);
+	ret = qcom_pas_prepare_and_auth_reset(&ctx);
+	if (ret)
+		dev_err(dev, "error %d authenticating \"%s\"\n", ret, path);
+
 out_release_firmware:
 	release_firmware(fw);

[Severity: Medium]
Does this leak the metadata buffer stored in ctx.ptr when ipa_firmware_load() returns?

Quoted 9 lines

Previously, qcom_mdt_load() passed a NULL context to __qcom_mdt_pas_init(), so qcom_pas_init_image() freed the metadata buffer immediately. qcom_mdt_pas_load() instead passes &ctx to __qcom_mdt_pas_init() -> qcom_pas_init_image(), which retains the coherent DMA or TEE shared-memory allocation in ctx.ptr until qcom_pas_metadata_release(&ctx) is called. Because ipa_firmware_load() never calls qcom_pas_metadata_release(&ctx), the metadata buffer is leaked on both the normal path and if qcom_mdt_pas_load() fails after qcom_pas_init_image() succeeds.

Change https://lore.kernel.org/all/[email protected]/
is present in linux-next which takes care of this.

Quoted 3 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Signature · 1 line
-Mukesh Ojha
#18 lore raw

PATCH v3 7/9 wifi: ath12k: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

In reply to Mukesh Ojha

ath12k_ahb_power_up() calls qcom_mdt_load(), which bundles metadata initialization, memory setup, and segment loading into a single API that does not expose a PAS context. Later it calls qcom_pas_auth_and_reset() for TrustZone authentication, which is the intended behaviour. However, a new qcom_mdt_pas_load() has recently been introduced that works with a PAS context and can handle everything qcom_mdt_load() provides, along with additional functionality such as shmbridge creation and retrieving memory from the shmbridge buffer when the same remote processor needs to run while Linux runs at EL2.

Replace qcom_mdt_load() with qcom_pas_context_init() + qcom_mdt_pas_load() and replace qcom_pas_auth_and_reset() with qcom_pas_prepare_and_auth_reset(), using a stack-allocated struct qcom_pas_context in ath12k_ahb_power_up(). The non-scm_auth path retains qcom_mdt_load_no_init() with the caller-provided mem_region.

Apply the same pattern to ath12k_ahb_load_auth_shared_fw() for consistency.

---
 drivers/net/wireless/ath/ath12k/ahb.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
drivers/net/wireless/ath/ath12k/ahb.c +14 -9
index 8a3919675134..75d3141f968f 100644
@@ -418,6 +418,7 @@ static int ath12k_ahb_power_up(struct ath12k_base *ab)
 	struct ath12k_ahb_rproc_info *rproc_info = ab_ahb->rproc_info;
 	char fw_name[ATH12K_USERPD_FW_NAME_LEN];
 	char fw2_name[ATH12K_USERPD_FW_NAME_LEN];
+	struct qcom_pas_context ctx;
 	struct device *dev = ab->dev;
 	const struct firmware *fw, *fw2;
 	unsigned long time_left;
@@ -451,14 +452,15 @@ static int ath12k_ahb_power_up(struct ath12k_base *ab)
 		ATH12K_AHB_UPD_SWID;
 
 	/* Load FW image to a reserved memory location */
-	if (ab_ahb->scm_auth_enabled)
-		ret = qcom_mdt_load(dev, fw, fw_name, pasid, rproc_info->mem_region,
-				    rproc_info->mem_phys, rproc_info->mem_size,
-				    NULL);
-	else
+	if (ab_ahb->scm_auth_enabled) {
+		qcom_pas_context_init(&ctx, dev, pasid,
+				      rproc_info->mem_phys, rproc_info->mem_size);
+		ret = qcom_mdt_pas_load(&ctx, fw, fw_name, NULL);
+	} else {
 		ret = qcom_mdt_load_no_init(dev, fw, fw_name, rproc_info->mem_region,
 					    rproc_info->mem_phys, rproc_info->mem_size,
 					    NULL);
+	}
 	if (ret) {
 		ath12k_err(ab, "Failed to load MDT segments: %d\n", ret);
 		goto err_fw;
@@ -492,7 +494,7 @@ static int ath12k_ahb_power_up(struct ath12k_base *ab)
 
 	if (ab_ahb->scm_auth_enabled) {
 		/* Authenticate FW image using peripheral ID */
-		ret = qcom_pas_auth_and_reset(pasid);
+		ret = qcom_pas_prepare_and_auth_reset(&ctx);
 		if (ret) {
 			ath12k_err(ab, "failed to boot the remote processor %d\n", ret);
 			goto err_fw2;
@@ -1056,6 +1058,7 @@ static int ath12k_ahb_load_auth_shared_fw(struct ath12k_base *ab,
 					  struct ath12k_ahb_rproc_info *rproc_info,
 					  const char *fw_name, u32 pasid)
 {
+	struct qcom_pas_context ctx;
 	int ret;
 
 	const struct firmware *fw __free(firmware) = NULL;
@@ -1074,14 +1077,16 @@ static int ath12k_ahb_load_auth_shared_fw(struct ath12k_base *ab,
 	ath12k_dbg(ab, ATH12K_DBG_AHB, "loading firmware %s, size %zd\n", fw_name,
 		   fw->size);
 
-	ret = qcom_mdt_load(ab->dev, fw, fw_name, pasid, rproc_info->mem_region,
-			    rproc_info->mem_phys, rproc_info->mem_size, NULL);
+	qcom_pas_context_init(&ctx, ab->dev, pasid,
+			      rproc_info->mem_phys, rproc_info->mem_size);
+
+	ret = qcom_mdt_pas_load(&ctx, fw, fw_name, NULL);
 	if (ret) {
 		ath12k_err(ab, "failed to load RO firmware %s: %d\n", fw_name, ret);
 		return ret;
 	}
 
-	ret = qcom_pas_auth_and_reset(pasid);
+	ret = qcom_pas_prepare_and_auth_reset(&ctx);
 	if (ret)
 		ath12k_err(ab, "failed to authenticate and boot shared firmware: %d\n",
 			   ret);
Signature · 1 line
2.55.0
#19 lore raw

Re: [PATCH v3 7/9] wifi: ath12k: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] wifi: ath12k: PAS metadata leak in ath12k_ahb_power_up() and shared FW load --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] wifi: ath12k: PAS metadata leak in ath12k_ahb_power_up() and shared FW load
--

commit dad01e3cbb75f83eee2677fffc757868fad930d0
Author: Mukesh Ojha <[email protected]>

wifi: ath12k: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

This commit replaces qcom_mdt_load() and qcom_pas_auth_and_reset() in ath12k_ahb_power_up() and ath12k_ahb_load_auth_shared_fw() with qcom_pas_context_init(), qcom_mdt_pas_load(), and qcom_pas_prepare_and_auth_reset().

Quoted 60 lines
drivers/net/wireless/ath/ath12k/ahb.c +13 -8
index 2c55aae06dd06..355f1f5509975 100644
@@ -411,6 +411,7 @@ static int ath12k_ahb_power_up(struct ath12k_base *ab)
 	struct ath12k_ahb_rproc_info *rproc_info = ab_ahb->rproc_info;
 	char fw_name[ATH12K_USERPD_FW_NAME_LEN];
 	char fw2_name[ATH12K_USERPD_FW_NAME_LEN];
+	struct qcom_pas_context ctx;
 	struct device *dev = ab->dev;
 	const struct firmware *fw, *fw2;
 	unsigned long time_left;
@@ -444,14 +445,15 @@ static int ath12k_ahb_power_up(struct ath12k_base *ab)
 		ATH12K_AHB_UPD_SWID;
 
 	/* Load FW image to a reserved memory location */
-	if (ab_ahb->scm_auth_enabled)
-		ret = qcom_mdt_load(dev, fw, fw_name, pasid, rproc_info->mem_region,
-				    rproc_info->mem_phys, rproc_info->mem_size,
-				    NULL);
-	else
+	if (ab_ahb->scm_auth_enabled) {
+		qcom_pas_context_init(&ctx, dev, pasid,
+				      rproc_info->mem_phys, rproc_info->mem_size);
+		ret = qcom_mdt_pas_load(&ctx, fw, fw_name, NULL);
+	} else {
 		ret = qcom_mdt_load_no_init(dev, fw, fw_name, rproc_info->mem_region,
 					    rproc_info->mem_phys, rproc_info->mem_size,
 					    NULL);
+	}
 	if (ret) {
 		ath12k_err(ab, "Failed to load MDT segments: %d\n", ret);
 		goto err_fw;
@@ -485,7 +487,7 @@ static int ath12k_ahb_power_up(struct ath12k_base *ab)
 
 	if (ab_ahb->scm_auth_enabled) {
 		/* Authenticate FW image using peripheral ID */
-		ret = qcom_pas_auth_and_reset(pasid);
+		ret = qcom_pas_prepare_and_auth_reset(&ctx);
 		if (ret) {
 			ath12k_err(ab, "failed to boot the remote processor %d\n", ret);
 			goto err_fw2;
@@ -1023,6 +1025,7 @@ static int ath12k_ahb_load_auth_shared_fw(struct ath12k_base *ab,
 					  struct ath12k_ahb_rproc_info *rproc_info,
 					  const char *fw_name, u32 pasid)
 {
+	struct qcom_pas_context ctx;
 	int ret;
 
 	const struct firmware *fw __free(firmware) = NULL;
@@ -1041,14 +1044,16 @@ static int ath12k_ahb_load_auth_shared_fw(struct ath12k_base *ab,
 	ath12k_dbg(ab, ATH12K_DBG_AHB, "loading firmware %s, size %zd\n", fw_name,
 		   fw->size);
 
-	ret = qcom_mdt_load(ab->dev, fw, fw_name, pasid, rproc_info->mem_region,
-			    rproc_info->mem_phys, rproc_info->mem_size, NULL);
+	qcom_pas_context_init(&ctx, ab->dev, pasid,
+			      rproc_info->mem_phys, rproc_info->mem_size);
+
+	ret = qcom_mdt_pas_load(&ctx, fw, fw_name, NULL);

drivers/net/wireless/ath/ath12k/ahb.c static int ath12k_ahb_load_auth_shared_fw(struct ath12k_base *ab, · line 1059

Quoted 10 lines
drivers/net/wireless/ath/ath12k/ahb.c +1 -1
 	if (ret) {
 		ath12k_err(ab, "failed to load RO firmware %s: %d\n", fw_name, ret);
 		return ret;
 	}
 
-	ret = qcom_pas_auth_and_reset(pasid);
+	ret = qcom_pas_prepare_and_auth_reset(&ctx);
 	if (ret)
 		ath12k_err(ab, "failed to authenticate and boot shared firmware: %d\n",
 			   ret);

[Severity: High]
Does this leak the PAS metadata buffer stored in ctx.ptr in both ath12k_ahb_power_up() and ath12k_ahb_load_auth_shared_fw()?

Previously, qcom_mdt_load() passed a NULL context pointer to __qcom_mdt_pas_init() -> qcom_pas_init_image(), which caused the backend to free the metadata buffer immediately. qcom_mdt_pas_load() instead passes &ctx to qcom_pas_init_image(), which retains the allocated metadata buffer in ctx.ptr:

drivers/firmware/qcom/qcom_scm.c:qcom_scm_pas_init_image() {
    ...
	if (ret < 0 || !ctx) {
		dma_free_coherent(dev, size, mdata_buf, mdata_phys);
	} else if (ctx) {
		ctx->ptr = mdata_buf;
		ctx->phys = mdata_phys;
		ctx->size = size;
	}
    ...
}

Neither qcom_mdt_pas_load() nor qcom_pas_prepare_and_auth_reset() frees this allocation, so ctx goes out of scope and leaks the metadata memory on every firmware load and error unwind path unless qcom_pas_metadata_release(&ctx) is called.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#20 lore raw

Re: [PATCH v3 7/9] wifi: ath12k: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

Quoted 109 lines · On Tue, Nov 17, 2026 at 04:24:12PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] wifi: ath12k: PAS metadata leak in ath12k_ahb_power_up() and shared FW load
--

commit dad01e3cbb75f83eee2677fffc757868fad930d0
Author: Mukesh Ojha <[email protected]>

wifi: ath12k: migrate firmware loading to qcom_mdt_pas_load() and qcom_pas_prepare_and_auth_reset()

This commit replaces qcom_mdt_load() and qcom_pas_auth_and_reset() in ath12k_ahb_power_up() and ath12k_ahb_load_auth_shared_fw() with qcom_pas_context_init(), qcom_mdt_pas_load(), and qcom_pas_prepare_and_auth_reset().

Quoted 60 lines
drivers/net/wireless/ath/ath12k/ahb.c +13 -8
index 2c55aae06dd06..355f1f5509975 100644
@@ -411,6 +411,7 @@ static int ath12k_ahb_power_up(struct ath12k_base *ab)
 	struct ath12k_ahb_rproc_info *rproc_info = ab_ahb->rproc_info;
 	char fw_name[ATH12K_USERPD_FW_NAME_LEN];
 	char fw2_name[ATH12K_USERPD_FW_NAME_LEN];
+	struct qcom_pas_context ctx;
 	struct device *dev = ab->dev;
 	const struct firmware *fw, *fw2;
 	unsigned long time_left;
@@ -444,14 +445,15 @@ static int ath12k_ahb_power_up(struct ath12k_base *ab)
 		ATH12K_AHB_UPD_SWID;
 
 	/* Load FW image to a reserved memory location */
-	if (ab_ahb->scm_auth_enabled)
-		ret = qcom_mdt_load(dev, fw, fw_name, pasid, rproc_info->mem_region,
-				    rproc_info->mem_phys, rproc_info->mem_size,
-				    NULL);
-	else
+	if (ab_ahb->scm_auth_enabled) {
+		qcom_pas_context_init(&ctx, dev, pasid,
+				      rproc_info->mem_phys, rproc_info->mem_size);
+		ret = qcom_mdt_pas_load(&ctx, fw, fw_name, NULL);
+	} else {
 		ret = qcom_mdt_load_no_init(dev, fw, fw_name, rproc_info->mem_region,
 					    rproc_info->mem_phys, rproc_info->mem_size,
 					    NULL);
+	}
 	if (ret) {
 		ath12k_err(ab, "Failed to load MDT segments: %d\n", ret);
 		goto err_fw;
@@ -485,7 +487,7 @@ static int ath12k_ahb_power_up(struct ath12k_base *ab)
 
 	if (ab_ahb->scm_auth_enabled) {
 		/* Authenticate FW image using peripheral ID */
-		ret = qcom_pas_auth_and_reset(pasid);
+		ret = qcom_pas_prepare_and_auth_reset(&ctx);
 		if (ret) {
 			ath12k_err(ab, "failed to boot the remote processor %d\n", ret);
 			goto err_fw2;
@@ -1023,6 +1025,7 @@ static int ath12k_ahb_load_auth_shared_fw(struct ath12k_base *ab,
 					  struct ath12k_ahb_rproc_info *rproc_info,
 					  const char *fw_name, u32 pasid)
 {
+	struct qcom_pas_context ctx;
 	int ret;
 
 	const struct firmware *fw __free(firmware) = NULL;
@@ -1041,14 +1044,16 @@ static int ath12k_ahb_load_auth_shared_fw(struct ath12k_base *ab,
 	ath12k_dbg(ab, ATH12K_DBG_AHB, "loading firmware %s, size %zd\n", fw_name,
 		   fw->size);
 
-	ret = qcom_mdt_load(ab->dev, fw, fw_name, pasid, rproc_info->mem_region,
-			    rproc_info->mem_phys, rproc_info->mem_size, NULL);
+	qcom_pas_context_init(&ctx, ab->dev, pasid,
+			      rproc_info->mem_phys, rproc_info->mem_size);
+
+	ret = qcom_mdt_pas_load(&ctx, fw, fw_name, NULL);

drivers/net/wireless/ath/ath12k/ahb.c static int ath12k_ahb_load_auth_shared_fw(struct ath12k_base *ab, · line 1059

Quoted 10 lines
drivers/net/wireless/ath/ath12k/ahb.c +1 -1
 	if (ret) {
 		ath12k_err(ab, "failed to load RO firmware %s: %d\n", fw_name, ret);
 		return ret;
 	}
 
-	ret = qcom_pas_auth_and_reset(pasid);
+	ret = qcom_pas_prepare_and_auth_reset(&ctx);
 	if (ret)
 		ath12k_err(ab, "failed to authenticate and boot shared firmware: %d\n",
 			   ret);

[Severity: High]
Does this leak the PAS metadata buffer stored in ctx.ptr in both ath12k_ahb_power_up() and ath12k_ahb_load_auth_shared_fw()?

Previously, qcom_mdt_load() passed a NULL context pointer to __qcom_mdt_pas_init() -> qcom_pas_init_image(), which caused the backend to free the metadata buffer immediately. qcom_mdt_pas_load() instead passes &ctx to qcom_pas_init_image(), which retains the allocated metadata buffer in ctx.ptr:

drivers/firmware/qcom/qcom_scm.c:qcom_scm_pas_init_image() {
    ...
	if (ret < 0 || !ctx) {
		dma_free_coherent(dev, size, mdata_buf, mdata_phys);
	} else if (ctx) {
		ctx->ptr = mdata_buf;
		ctx->phys = mdata_phys;
		ctx->size = size;
	}
    ...
}
Quoted 4 lines

Neither qcom_mdt_pas_load() nor qcom_pas_prepare_and_auth_reset() frees this allocation, so ctx goes out of scope and leaks the metadata memory on every firmware load and error unwind path unless qcom_pas_metadata_release(&ctx) is called.

change https://lore.kernel.org/all/[email protected]/
takes care of this is present in linux-next.
Please check the commit before trying to raise this point.

Quoted 3 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Signature · 1 line
-Mukesh Ojha
#21 lore raw

PATCH v3 8/9 soc: qcom: mdt_loader: remove qcom_mdt_load()

In reply to Mukesh Ojha

All callers have been migrated to qcom_mdt_pas_load(), which takes an explicit qcom_pas_context instead of a raw pas_id and delegates memory setup to the context. Remove the now-unused qcom_mdt_load() wrapper along with its EXPORT_SYMBOL_GPL and header declaration.

---
 drivers/soc/qcom/mdt_loader.c       | 59 ++++++++++-------------------
 include/linux/soc/qcom/mdt_loader.h | 12 ------
 2 files changed, 19 insertions(+), 52 deletions(-)
drivers/soc/qcom/mdt_loader.c +19 -40
index c6ec67e1da09..fd638833b17e 100644
@@ -329,7 +329,8 @@ static bool qcom_mdt_bins_are_split(const struct firmware *fw)
  * @dev:	device handle to associate resources with
  * @fw:		firmware object for the mdt file
  * @fw_name:	name of the firmware, for construction of segment file names
- * @mem_region:	allocated memory region to load firmware into
+ * @mem_region:	virtual address of the memory region to load firmware into,
+ *		or NULL to have the region mapped internally with ioremap_wc()
  * @mem_phys:	physical address of allocated memory region
  * @mem_size:	size of the allocated memory region
  * @reloc_base:	adjusted physical address after relocation
@@ -344,6 +345,7 @@ int qcom_mdt_load_no_init(struct device *dev, const struct firmware *fw,
 	const struct elf32_phdr *phdrs;
 	const struct elf32_phdr *phdr;
 	const struct elf32_hdr *ehdr;
+	void __iomem *mapped = NULL;
 	phys_addr_t mem_reloc;
 	phys_addr_t min_addr = PHYS_ADDR_MAX;
 	ssize_t offset;
@@ -353,12 +355,22 @@ int qcom_mdt_load_no_init(struct device *dev, const struct firmware *fw,
 	int ret = 0;
 	int i;
 
-	if (!fw || !mem_region || !mem_phys || !mem_size)
+	if (!fw || !mem_phys || !mem_size)
 		return -EINVAL;
 
 	if (!mdt_header_valid(fw))
 		return -EINVAL;
 
+	if (!mem_region) {
+		mapped = ioremap_wc(mem_phys, mem_size);
+		if (!mapped) {
+			dev_err(dev, "unable to map memory region: %pa+%zx\n",
+				&mem_phys, mem_size);
+			return -ENOMEM;
+		}
+		mem_region = (__force void *)mapped;
+	}
+
 	is_split = qcom_mdt_bins_are_split(fw);
 	ehdr = (struct elf32_hdr *)fw->data;
 	phdrs = (struct elf32_phdr *)(fw->data + ehdr->e_phoff);
@@ -437,39 +449,13 @@ int qcom_mdt_load_no_init(struct device *dev, const struct firmware *fw,
 	if (reloc_base)
 		*reloc_base = mem_reloc;
 
+	if (mapped)
+		iounmap(mapped);
+
 	return ret;
 }
 EXPORT_SYMBOL_GPL(qcom_mdt_load_no_init);
 
-/**
- * qcom_mdt_load() - load the firmware which header is loaded as fw
- * @dev:	device handle to associate resources with
- * @fw:		firmware object for the mdt file
- * @fw_name:	name of the firmware, for construction of segment file names
- * @pas_id:	PAS identifier
- * @mem_region:	allocated memory region to load firmware into
- * @mem_phys:	physical address of allocated memory region
- * @mem_size:	size of the allocated memory region
- * @reloc_base:	adjusted physical address after relocation
- *
- * Returns 0 on success, negative errno otherwise.
- */
-int qcom_mdt_load(struct device *dev, const struct firmware *fw,
-		  const char *fw_name, int pas_id, void *mem_region,
-		  phys_addr_t mem_phys, size_t mem_size,
-		  phys_addr_t *reloc_base)
-{
-	int ret;
-
-	ret = __qcom_mdt_pas_init(dev, fw, fw_name, pas_id, mem_phys, NULL);
-	if (ret)
-		return ret;
-
-	return qcom_mdt_load_no_init(dev, fw, fw_name, mem_region, mem_phys,
-				     mem_size, reloc_base);
-}
-EXPORT_SYMBOL_GPL(qcom_mdt_load);
-
 /**
  * qcom_mdt_pas_load - Loads and authenticates the metadata of the firmware
  * (typically contained in the .mdt file), followed by loading the actual
@@ -489,21 +475,14 @@ EXPORT_SYMBOL_GPL(qcom_mdt_load);
 int qcom_mdt_pas_load(struct qcom_pas_context *ctx, const struct firmware *fw,
 		      const char *firmware, phys_addr_t *reloc_base)
 {
-	void __iomem *mem_region;
 	int ret;
 
 	ret = __qcom_mdt_pas_init(ctx->dev, fw, firmware, ctx->pas_id, ctx->mem_phys, ctx);
 	if (ret)
 		return ret;
 
-	mem_region = qcom_pas_ctx_map(ctx);
-	if (!mem_region)
-		return -ENOMEM;
-
-	ret = qcom_mdt_load_no_init(ctx->dev, fw, firmware, (__force void *)mem_region,
-				    ctx->mem_phys, ctx->mem_size, reloc_base);
-	iounmap(mem_region);
-	return ret;
+	return qcom_mdt_load_no_init(ctx->dev, fw, firmware, NULL,
+				     ctx->mem_phys, ctx->mem_size, reloc_base);
 }
 EXPORT_SYMBOL_GPL(qcom_mdt_pas_load);
 
include/linux/soc/qcom/mdt_loader.h +0 -12
index 74886f772d46..f49de7b388bc 100644
@@ -15,10 +15,6 @@ struct qcom_pas_context;
 #if IS_ENABLED(CONFIG_QCOM_MDT_LOADER)
 
 ssize_t qcom_mdt_get_size(const struct firmware *fw);
-int qcom_mdt_load(struct device *dev, const struct firmware *fw,
-		  const char *fw_name, int pas_id, void *mem_region,
-		  phys_addr_t mem_phys, size_t mem_size,
-		  phys_addr_t *reloc_base);
 
 int qcom_mdt_pas_load(struct qcom_pas_context *ctx, const struct firmware *fw,
 		      const char *firmware, phys_addr_t *reloc_base);
@@ -37,14 +33,6 @@ static inline ssize_t qcom_mdt_get_size(const struct firmware *fw)
 	return -ENODEV;
 }
 
-static inline int qcom_mdt_load(struct device *dev, const struct firmware *fw,
-				const char *fw_name, int pas_id,
-				void *mem_region, phys_addr_t mem_phys,
-				size_t mem_size, phys_addr_t *reloc_base)
-{
-	return -ENODEV;
-}
-
 static inline int qcom_mdt_pas_load(struct qcom_pas_context *ctx,
 				    const struct firmware *fw, const char *firmware,
 				    phys_addr_t *reloc_base)
Signature · 1 line
2.55.0
#22 lore raw

Re: [PATCH v3 8/9] soc: qcom: mdt_loader: remove qcom_mdt_load()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] soc: qcom: mdt_loader: undocumented mapping change in qcom_mdt_load_no_init() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] soc: qcom: mdt_loader: undocumented mapping change in qcom_mdt_load_no_init()
--

commit eab28e4077f6ee900ea4fd6648711acdb8ef064c
Author: Mukesh Ojha <[email protected]>

soc: qcom: mdt_loader: remove qcom_mdt_load()

This commit removes the unused qcom_mdt_load() wrapper and moves memory mapping from qcom_mdt_pas_load() into qcom_mdt_load_no_init() when mem_region is NULL.

Quoted 56 lines
drivers/soc/qcom/mdt_loader.c +17 -2
index aafa83466b92b..436d8bc4847a3 100644
@@ -326,7 +326,8 @@ static bool qcom_mdt_bins_are_split(const struct firmware *fw)
  * @dev:	device handle to associate resources with
  * @fw:		firmware object for the mdt file
  * @fw_name:	name of the firmware, for construction of segment file names
- * @mem_region:	allocated memory region to load firmware into
+ * @mem_region:	virtual address of the memory region to load firmware into,
+ *		or NULL to have the region mapped internally with ioremap_wc()
  * @mem_phys:	physical address of allocated memory region
  * @mem_size:	size of the allocated memory region
  * @reloc_base:	adjusted physical address after relocation
@@ -341,6 +342,7 @@ int qcom_mdt_load_no_init(struct device *dev, const struct firmware *fw,
 	const struct elf32_phdr *phdrs;
 	const struct elf32_phdr *phdr;
 	const struct elf32_hdr *ehdr;
+	void __iomem *mapped = NULL;
 	phys_addr_t mem_reloc;
 	phys_addr_t min_addr = PHYS_ADDR_MAX;
 	ssize_t offset;
@@ -350,12 +352,22 @@ int qcom_mdt_load_no_init(struct device *dev, const struct firmware *fw,
 	int ret = 0;
 	int i;
 
-	if (!fw || !mem_region || !mem_phys || !mem_size)
+	if (!fw || !mem_phys || !mem_size)
 		return -EINVAL;
 
 	if (!mdt_header_valid(fw))
 		return -EINVAL;
 
+	if (!mem_region) {
+		mapped = ioremap_wc(mem_phys, mem_size);
+		if (!mapped) {
+			dev_err(dev, "unable to map memory region: %pa+%zx\n",
+				&mem_phys, mem_size);
+			return -ENOMEM;
+		}
+		mem_region = (__force void *)mapped;
+	}
+
 	is_split = qcom_mdt_bins_are_split(fw);
 	ehdr = (struct elf32_hdr *)fw->data;
 	phdrs = (struct elf32_phdr *)(fw->data + ehdr->e_phoff);
@@ -434,39 +446,13 @@ int qcom_mdt_load_no_init(struct device *dev, const struct firmware *fw,
 	if (reloc_base)
 		*reloc_base = mem_reloc;
 
+	if (mapped)
+		iounmap(mapped);
+
 	return ret;
 }
 EXPORT_SYMBOL_GPL(qcom_mdt_load_no_init);

[ ... ]

Quoted 12 lines
drivers/soc/qcom/mdt_loader.c +0 -2
@@ -486,21 +472,14 @@ EXPORT_SYMBOL_GPL(qcom_mdt_load);
 int qcom_mdt_pas_load(struct qcom_pas_context *ctx, const struct firmware *fw,
 		      const char *firmware, phys_addr_t *reloc_base)
 {
-	void __iomem *mem_region;
 	int ret;
 
 	ret = __qcom_mdt_pas_init(ctx->dev, fw, firmware, ctx->pas_id, ctx->mem_phys, ctx);
 	if (ret)
 		return ret;
 
-	mem_region = qcom_pas_ctx_map(ctx);

drivers/soc/qcom/mdt_loader.c EXPORT_SYMBOL_GPL(qcom_mdt_load); · line 483

Quoted 10 lines
drivers/soc/qcom/mdt_loader.c +2 -7
-	if (!mem_region)
-		return -ENOMEM;
-
-	ret = qcom_mdt_load_no_init(ctx->dev, fw, firmware, (__force void *)mem_region,
-				    ctx->mem_phys, ctx->mem_size, reloc_base);
-	iounmap(mem_region);
-	return ret;
+	return qcom_mdt_load_no_init(ctx->dev, fw, firmware, NULL,
+				     ctx->mem_phys, ctx->mem_size, reloc_base);
 }

[Severity: Medium]
Why does this patch move the memory mapping from qcom_mdt_pas_load() into qcom_mdt_load_no_init()?

The changelog only describes removing qcom_mdt_load(), which was already a simple wrapper around __qcom_mdt_pas_init() and qcom_mdt_load_no_init(). Moving the mapping into qcom_mdt_load_no_init() duplicates the ioremap_wc() and error logging already provided by qcom_pas_ctx_map():

include/linux/firmware/qcom/qcom_pas.h:qcom_pas_ctx_map() {
	void __iomem *ptr = ioremap_wc(ctx->mem_phys, ctx->mem_size);

	if (!ptr)
		dev_err(ctx->dev, "unable to map memory region: %pa+%zx\n",
			&ctx->mem_phys, ctx->mem_size);
	return ptr;
}

All direct callers of qcom_mdt_load_no_init() manage their own mem_region mapping. Dropping the !mem_region check means a non-PAS caller that inadvertently passes a NULL mem_region after a mapping failure will silently trigger an internal ioremap_wc() instead of failing with -EINVAL.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#23 lore raw

PATCH v3 9/9 firmware: qcom: pas: remove qcom_pas_auth_and_reset()

In reply to Mukesh Ojha

All callers have been migrated to qcom_pas_prepare_and_auth_reset(), which additionally sets up a shmbridge over firmware memory before TrustZone authentication. Remove the bare pas_id-only wrapper, its EXPORT_SYMBOL_GPL, the header declaration, the auth_and_reset ops slot from struct qcom_pas_ops and both backend implementations.

The internal helper functions (__qcom_scm_pas_auth_and_reset, __qcom_pas_tee_auth_and_reset) remain as they are called directly by their respective prepare_and_auth_reset implementations.

---
 drivers/firmware/qcom/qcom_pas.c       | 16 ----------------
 drivers/firmware/qcom/qcom_pas.h       |  3 ---
 drivers/firmware/qcom/qcom_pas_tee.c   |  6 ------
 drivers/firmware/qcom/qcom_scm.c       |  7 +++----
 include/linux/firmware/qcom/qcom_pas.h |  1 -
 5 files changed, 3 insertions(+), 30 deletions(-)
drivers/firmware/qcom/qcom_pas.c +0 -16
index fdf62003fbf1..08bf3a8d3bf6 100644
@@ -184,22 +184,6 @@ struct resource_table *qcom_pas_get_rsc_table(struct qcom_pas_context *ctx,
 }
 EXPORT_SYMBOL_GPL(qcom_pas_get_rsc_table);
 
-/**
- * qcom_pas_auth_and_reset() - Authenticate the given peripheral firmware
- *			       and reset the remote processor
- * @pas_id:	peripheral authentication service id
- *
- * Return: 0 on success.
- */
-int qcom_pas_auth_and_reset(u32 pas_id)
-{
-	if (!ops_ptr)
-		return -ENODEV;
-
-	return ops_ptr->auth_and_reset(ops_ptr->dev, pas_id);
-}
-EXPORT_SYMBOL_GPL(qcom_pas_auth_and_reset);
-
 /**
  * qcom_pas_prepare_and_auth_reset() - Prepare, authenticate, and reset the
  *				       remote processor
drivers/firmware/qcom/qcom_pas.h +0 -3
index 8643e2760602..a296be2be8c0 100644
@@ -18,8 +18,6 @@ struct device;
  * @get_rsc_table:		Peripheral get resource table callback.
  * @prepare_and_auth_reset:	Peripheral prepare firmware authentication and
  *				reset callback.
- * @auth_and_reset:		Peripheral firmware authentication and reset
- *				callback.
  * @set_remote_state:		Peripheral set remote state callback.
  * @shutdown:			Peripheral shutdown callback.
  * @metadata_release:		Image metadata release callback.
@@ -37,7 +35,6 @@ struct qcom_pas_ops {
 			       size_t *output_rt_size);
 	int (*prepare_and_auth_reset)(struct device *dev,
 				      struct qcom_pas_context *ctx);
-	int (*auth_and_reset)(struct device *dev, u32 pas_id);
 	int (*set_remote_state)(struct device *dev, u32 state, u32 pas_id);
 	int (*shutdown)(struct device *dev, u32 pas_id);
 	void (*metadata_release)(struct device *dev,
drivers/firmware/qcom/qcom_pas_tee.c +0 -6
index 1fca2712d791..525b8a02ae82 100644
@@ -331,11 +331,6 @@ static int __qcom_pas_tee_auth_and_reset(struct device *dev, u32 pas_id,
 	return ret;
 }
 
-static int qcom_pas_tee_auth_and_reset(struct device *dev, u32 pas_id)
-{
-	return __qcom_pas_tee_auth_and_reset(dev, pas_id, 0, 0);
-}
-
 static int qcom_pas_tee_prepare_and_auth_reset(struct device *dev,
 					       struct qcom_pas_context *ctx)
 {
@@ -412,7 +407,6 @@ static struct qcom_pas_ops qcom_pas_ops_tee = {
 	.init_image		= qcom_pas_tee_init_image,
 	.mem_setup		= qcom_pas_tee_mem_setup,
 	.get_rsc_table		= qcom_pas_tee_get_rsc_table,
-	.auth_and_reset		= qcom_pas_tee_auth_and_reset,
 	.prepare_and_auth_reset	= qcom_pas_tee_prepare_and_auth_reset,
 	.set_remote_state	= qcom_pas_tee_set_remote_state,
 	.shutdown		= qcom_pas_tee_shutdown,
drivers/firmware/qcom/qcom_scm.c +3 -4
index 030e4ba90812..b0f96e751167 100644
@@ -855,7 +855,7 @@ static void *qcom_scm_pas_get_rsc_table(struct device *dev,
 	return ret ? ERR_PTR(ret) : tbl_ptr;
 }
 
-static int qcom_scm_pas_auth_and_reset(struct device *dev, u32 pas_id)
+static int __qcom_scm_pas_auth_and_reset(struct device *dev, u32 pas_id)
 {
 	int ret;
 	struct qcom_scm_desc desc = {
@@ -896,7 +896,7 @@ static int qcom_scm_pas_prepare_and_auth_reset(struct device *dev,
 	 * memory region and then invokes a call to TrustZone to authenticate.
 	 */
 	if (!ctx->use_tzmem)
-		return qcom_scm_pas_auth_and_reset(dev, ctx->pas_id);
+		return __qcom_scm_pas_auth_and_reset(dev, ctx->pas_id);
 
 	/*
 	 * When Linux runs @ EL2 Linux must create the shmbridge itself and then
@@ -906,7 +906,7 @@ static int qcom_scm_pas_prepare_and_auth_reset(struct device *dev,
 	if (ret)
 		return ret;
 
-	ret = qcom_scm_pas_auth_and_reset(dev, ctx->pas_id);
+	ret = __qcom_scm_pas_auth_and_reset(dev, ctx->pas_id);
 	qcom_tzmem_shm_bridge_delete(handle);
 
 	return ret;
@@ -987,7 +987,6 @@ static struct qcom_pas_ops qcom_pas_ops_scm = {
 	.init_image		= qcom_scm_pas_init_image,
 	.mem_setup		= qcom_scm_pas_mem_setup,
 	.get_rsc_table		= qcom_scm_pas_get_rsc_table,
-	.auth_and_reset		= qcom_scm_pas_auth_and_reset,
 	.prepare_and_auth_reset	= qcom_scm_pas_prepare_and_auth_reset,
 	.set_remote_state	= qcom_scm_pas_set_remote_state,
 	.shutdown		= qcom_scm_pas_shutdown,
include/linux/firmware/qcom/qcom_pas.h +0 -1
index 458075868bf5..53b8d9631f22 100644
@@ -48,7 +48,6 @@ struct resource_table *qcom_pas_get_rsc_table(struct qcom_pas_context *ctx,
 					      void *input_rt, size_t input_rt_size,
 					      size_t *output_rt_size);
 int qcom_pas_mem_setup(u32 pas_id, phys_addr_t addr, phys_addr_t size);
-int qcom_pas_auth_and_reset(u32 pas_id);
 int qcom_pas_prepare_and_auth_reset(struct qcom_pas_context *ctx);
 int qcom_pas_set_remote_state(u32 state, u32 pas_id);
 int qcom_pas_shutdown(u32 pas_id);
Signature · 1 line
2.55.0