Skip to contentSkip to conversation

media: iris: several fixes

PATCH In review Dmitry Baryshkov 2 participants 10 messages Jul 31

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

v2 cover media: iris: several fixes

Fix several problems reported by Sashiko.

---
Changes in v2:
- Dropped duplicate SoB (Konrad)
- Simplified iris_add_session() (Konrad)
- Link to v1: https://patch.msgid.link/[email protected]

To: Vikash Garodia <[email protected]>
To: Dikshita Agarwal <[email protected]>
To: Abhinav Kumar <[email protected]>
To: Bryan O'Donoghue <[email protected]>
To: Mauro Carvalho Chehab <[email protected]>
To: Stefan Schmidt <[email protected]>
To: Hans Verkuil <[email protected]>
To: Vedang Nagar <[email protected]>
To: Dmitry Baryshkov <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
Dmitry Baryshkov (5):
media: iris: fail firmware boot on invalid uc_region
media: iris: take core lock when scanning the instance list
media: iris: guard against a NULL hfi_sys_ops in the interrupt handler
media: iris: reject open() when the session limit is reached
media: iris: reference count video instances

 drivers/media/platform/qcom/iris/iris_hfi_common.c |  3 +-
 .../platform/qcom/iris/iris_hfi_gen1_response.c    |  2 ++
 .../platform/qcom/iris/iris_hfi_gen2_response.c    |  1 +
 drivers/media/platform/qcom/iris/iris_instance.h   |  4 +++
 drivers/media/platform/qcom/iris/iris_utils.c      |  6 ++++
 drivers/media/platform/qcom/iris/iris_utils.h      |  1 +
 drivers/media/platform/qcom/iris/iris_vb2.c        |  6 +++-
 drivers/media/platform/qcom/iris/iris_vidc.c       | 42 ++++++++++++++++------
 drivers/media/platform/qcom/iris/iris_vpu_common.c |  2 +-
 9 files changed, 53 insertions(+), 14 deletions(-)

---
base-commit: 56a985509842cdef159cea98d85d9213ee047cd7
change-id: 20260730-iris-fixes-846d7d353034

Best regards,

Signature · 2 lines
With best wishes
Dmitry
#1 lore raw

PATCH v2 1/5 media: iris: fail firmware boot on invalid uc_region

iris_vpu_boot_firmware() polls CTRL_STATUS while the firmware boots. When the hardware reports an invalid uc_region setting, the poll loop breaks early with count still below max_tries. The following timeout check (count >= max_tries) is therefore false, so the function skips the error return and falls through to return 0, reporting a successful boot. The caller then enables host interrupts and proceeds to use firmware that never came up, which can lead to unhandled timeouts or hardware hangs.

Return an error directly from the uc_region error path instead of breaking out of the loop and reporting success.

Fixes abf5bac63f68 ("media: iris: implement the boot sequence of the firmware")
Assisted-by Claude:claude-opus-4-8
---
 drivers/media/platform/qcom/iris/iris_vpu_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/qcom/iris/iris_vpu_common.c +1 -1
index e4847c107709..819db7569524 100644
@@ -84,7 +84,7 @@ int iris_vpu_boot_firmware(struct iris_core *core)
 		ctrl_status = readl(core->reg_base + CTRL_STATUS);
 		if ((ctrl_status & CTRL_ERROR_STATUS__M) == 0x4) {
 			dev_err(core->dev, "invalid setting for uc_region\n");
-			break;
+			return -EINVAL;
 		}
 
 		usleep_range(50, 100);
Signature · 1 line
2.47.3
Reviewed-by on v2 1/5
#2 lore raw

Re: [PATCH v2 1/5] media: iris: fail firmware boot on invalid uc_region

Quoted 8 lines · Dmitry Baryshkov

iris_vpu_boot_firmware() polls CTRL_STATUS while the firmware boots. When the hardware reports an invalid uc_region setting, the poll loop breaks early with count still below max_tries. The following timeout check (count >= max_tries) is therefore false, so the function skips the error return and falls through to return 0, reporting a successful boot. The caller then enables host interrupts and proceeds to use firmware that never came up, which can lead to unhandled timeouts or hardware hangs.

Quoted 7 lines

Return an error directly from the uc_region error path instead of breaking out of the loop and reporting success.

Fixes abf5bac63f68 ("media: iris: implement the boot sequence of the firmware")
Assisted-by Claude:claude-opus-4-8

---

Konrad

#3 lore raw

PATCH v2 2/5 media: iris: take core lock when scanning the instance list

In reply to Dmitry Baryshkov

iris_check_session_supported() walks core->instances to confirm the current instance is registered, but does so without holding core->lock. A concurrent iris_close() takes core->lock and removes a (possibly different) instance from the list via list_del_init() before freeing it, so the lockless traversal can follow a freed pointer and dereference it, resulting in a use-after-free.

Hold core->lock across the list traversal, matching the other iterators over core->instances such as iris_check_core_mbpf(). The lock is dropped before iris_check_core_mbpf() is called so the nesting is unchanged.

Fixes bdbe1cac0c10 ("media: iris: add check whether the video session is supported or not")
Assisted-by Claude:claude-opus-4-8
---
 drivers/media/platform/qcom/iris/iris_vb2.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/media/platform/qcom/iris/iris_vb2.c +5 -1
index a2ea2d67f60d..8faf709c26c1 100644
@@ -56,10 +56,14 @@ static int iris_check_session_supported(struct iris_inst *inst)
 	bool found = false;
 	int ret;
 
+	mutex_lock(&core->lock);
 	list_for_each_entry(instance, &core->instances, list) {
-		if (instance == inst)
+		if (instance == inst) {
 			found = true;
+			break;
+		}
 	}
+	mutex_unlock(&core->lock);
 
 	if (!found) {
 		ret = -EINVAL;
Signature · 1 line
2.47.3
Reviewed-by on v2 2/5
#4 lore raw

Re: [PATCH v2 2/5] media: iris: take core lock when scanning the instance list

Quoted 7 lines · Dmitry Baryshkov

iris_check_session_supported() walks core->instances to confirm the current instance is registered, but does so without holding core->lock. A concurrent iris_close() takes core->lock and removes a (possibly different) instance from the list via list_del_init() before freeing it, so the lockless traversal can follow a freed pointer and dereference it, resulting in a use-after-free.

Quoted 8 lines

Hold core->lock across the list traversal, matching the other iterators over core->instances such as iris_check_core_mbpf(). The lock is dropped before iris_check_core_mbpf() is called so the nesting is unchanged.

Fixes bdbe1cac0c10 ("media: iris: add check whether the video session is supported or not")
Assisted-by Claude:claude-opus-4-8

---

we also have:

iris_check_core_load()
iris_get_required_freq()
iris_vpu36_get_required_freq()
iris_vpu36_check_core_load()

that traverse the lsit and actually end up being called with the lock, but only implicitly - perhaps a lockdep hint could be useful there?

Konrad

#5 lore raw

PATCH v2 3/5 media: iris: guard against a NULL hfi_sys_ops in the interrupt handler

In reply to Dmitry Baryshkov

core->hfi_sys_ops is populated only during core bring-up, once the firmware generation has been detected. iris_vpu_power_on() enables interrupts earlier than that, so an interrupt (for instance a spurious one) that fires in this window makes iris_hfi_isr_handler() unconditionally dereference a NULL core->hfi_sys_ops and panic.

Skip the response handler when hfi_sys_ops has not been set up yet; the interrupt is still acknowledged and the line re-enabled.

Fixes d8a6a63372b8 ("media: qcom: iris: merge hfi_response_ops and hfi_command_ops")
Assisted-by Claude:claude-opus-4-8
---
 drivers/media/platform/qcom/iris/iris_hfi_common.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/media/platform/qcom/iris/iris_hfi_common.c +2 -1
index 8769ec61f117..42fc854d4c05 100644
@@ -109,7 +109,8 @@ irqreturn_t iris_hfi_isr_handler(int irq, void *data)
 	iris_vpu_clear_interrupt(core);
 	mutex_unlock(&core->lock);
 
-	core->hfi_sys_ops->sys_hfi_response_handler(core);
+	if (core->hfi_sys_ops)
+		core->hfi_sys_ops->sys_hfi_response_handler(core);
 
 	if (!iris_vpu_watchdog(core, core->intr_status))
 		enable_irq(irq);
Signature · 1 line
2.47.3
#6 lore raw

PATCH v2 4/5 media: iris: reject open() when the session limit is reached

In reply to Dmitry Baryshkov

iris_add_session() silently skips adding the instance to core->instances once max_session_count is reached, but returns void, so iris_open() continues as if it succeeded and hands a file descriptor back to userspace. As the instance is not on core->instances, firmware responses for it are dropped and every subsequent ioctl times out.

Make iris_add_session() return an error when the limit is reached and fail iris_open() accordingly, freeing the partially initialised instance.

Fixes 38fc8beaba55 ("media: iris: implement reqbuf ioctl with vb2_queue_setup")
Assisted-by Claude:claude-opus-4-8
---
 drivers/media/platform/qcom/iris/iris_vidc.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
drivers/media/platform/qcom/iris/iris_vidc.c +13 -6
index fcbc60016bee..4ca9185b3d2b 100644
@@ -40,21 +40,23 @@ static void iris_v4l2_fh_deinit(struct iris_inst *inst, struct file *filp)
 	v4l2_fh_exit(&inst->fh);
 }
 
-static void iris_add_session(struct iris_inst *inst)
+static int iris_add_session(struct iris_inst *inst)
 {
 	struct iris_core *core = inst->core;
 	struct iris_inst *iter;
 	u32 count = 0;
 
-	mutex_lock(&core->lock);
+	guard(mutex)(&core->lock);
 
 	list_for_each_entry(iter, &core->instances, list)
 		count++;
 
-	if (count < core->iris_platform_data->max_session_count)
-		list_add_tail(&inst->list, &core->instances);
+	if (count >= core->iris_platform_data->max_session_count)
+		return -EBUSY;
 
-	mutex_unlock(&core->lock);
+	list_add_tail(&inst->list, &core->instances);
+
+	return 0;
 }
 
 static void iris_remove_session(struct iris_inst *inst)
@@ -206,12 +208,17 @@ int iris_open(struct file *filp)
 	if (ret)
 		goto fail_m2m_ctx_release;
 
-	iris_add_session(inst);
+	ret = iris_add_session(inst);
+	if (ret)
+		goto fail_inst_deinit;
 
 	inst->fh.m2m_ctx = inst->m2m_ctx;
 
 	return 0;
 
+fail_inst_deinit:
+	kfree(inst->fmt_src);
+	kfree(inst->fmt_dst);
 fail_m2m_ctx_release:
 	v4l2_m2m_ctx_release(inst->m2m_ctx);
 fail_m2m_release:
Signature · 1 line
2.47.3
Reviewed-by on v2 4/5
#7 lore raw

Re: [PATCH v2 4/5] media: iris: reject open() when the session limit is reached

Quoted 6 lines · Dmitry Baryshkov

iris_add_session() silently skips adding the instance to core->instances once max_session_count is reached, but returns void, so iris_open() continues as if it succeeded and hands a file descriptor back to userspace. As the instance is not on core->instances, firmware responses for it are dropped and every subsequent ioctl times out.

Quoted 7 lines

Make iris_add_session() return an error when the limit is reached and fail iris_open() accordingly, freeing the partially initialised instance.

Fixes 38fc8beaba55 ("media: iris: implement reqbuf ioctl with vb2_queue_setup")
Assisted-by Claude:claude-opus-4-8

---

Konrad

#8 lore raw

PATCH v2 5/5 media: iris: reference count video instances

In reply to Dmitry Baryshkov

iris_get_instance() looks up an instance on core->instances and returns it after dropping core->lock, without taking any reference. The threaded interrupt handler uses this to find the instance a firmware response belongs to and then takes inst->lock. Meanwhile userspace may close the same file descriptor: iris_close() removes the instance from the list, destroys inst->lock and frees the instance. The interrupt handler then operates on freed memory and a destroyed mutex, a use-after-free.

Add a kref to struct iris_inst. iris_get_instance() takes a reference under core->lock, so an instance it returns cannot be freed until the caller drops that reference with iris_inst_put(). The instance is released (mutexes destroyed, memory freed) only when the last reference goes away, whether that is held by the closing thread or the interrupt handler.

Fixes 38fc8beaba55 ("media: iris: implement reqbuf ioctl with vb2_queue_setup")
Assisted-by Claude:claude-opus-4-8
---
 .../platform/qcom/iris/iris_hfi_gen1_response.c    |  2 ++
 .../platform/qcom/iris/iris_hfi_gen2_response.c    |  1 +
 drivers/media/platform/qcom/iris/iris_instance.h   |  4 ++++
 drivers/media/platform/qcom/iris/iris_utils.c      |  6 ++++++
 drivers/media/platform/qcom/iris/iris_utils.h      |  1 +
 drivers/media/platform/qcom/iris/iris_vidc.c       | 23 +++++++++++++++++-----
 6 files changed, 32 insertions(+), 5 deletions(-)
drivers/media/platform/qcom/iris/iris_hfi_gen1_response.c +2 -0
index bfd7495bf44f..e2b95c22f4d1 100644
@@ -632,6 +632,7 @@ static void iris_hfi_gen1_handle_response(struct iris_core *core, void *response
 			mutex_lock(&inst->lock);
 			iris_hfi_gen1_session_event_notify(inst, hdr);
 			mutex_unlock(&inst->lock);
+			iris_inst_put(inst);
 		} else {
 			iris_hfi_gen1_sys_event_notify(core, hdr);
 		}
@@ -667,6 +668,7 @@ static void iris_hfi_gen1_handle_response(struct iris_core *core, void *response
 			}
 		}
 		mutex_unlock(&inst->lock);
+		iris_inst_put(inst);
 
 		break;
 	}
drivers/media/platform/qcom/iris/iris_hfi_gen2_response.c +1 -0
index 8c2644c7f6e8..f0782c4b1e6e 100644
@@ -969,6 +969,7 @@ static int iris_hfi_gen2_handle_session_response(struct iris_core *core,
 		iris_hfi_gen2_handle_dequeue_buffers(inst);
 
 	mutex_unlock(&inst->lock);
+	iris_inst_put(inst);
 
 	return ret;
 }
drivers/media/platform/qcom/iris/iris_instance.h +4 -0
index a770331d1675..dde5d0b8a83b 100644
@@ -6,6 +6,8 @@
 #ifndef __IRIS_INSTANCE_H__
 #define __IRIS_INSTANCE_H__
 
+#include <linux/kref.h>
+
 #include <media/v4l2-ctrls.h>
 
 #include "iris_buffer.h"
@@ -35,6 +37,7 @@ enum iris_fmt_type_cap {
  * struct iris_inst - holds per video instance parameters
  *
  * @list: used for attach an instance to the core
+ * @kref: reference count, keeps the instance alive while the IRQ thread uses it
  * @core: pointer to core structure
  * @session_id: id of current video session
  * @hfi_session_ops: iris HFI session ops
@@ -82,6 +85,7 @@ enum iris_fmt_type_cap {
 
 struct iris_inst {
 	struct list_head		list;
+	struct kref			kref;
 	struct iris_core		*core;
 	u32				session_id;
 	const struct iris_hfi_session_ops	*hfi_session_ops;
drivers/media/platform/qcom/iris/iris_utils.c +6 -0
index ba5c8dc1280c..1096cc4b01c3 100644
@@ -92,6 +92,12 @@ struct iris_inst *iris_get_instance(struct iris_core *core, u32 session_id)
 	mutex_lock(&core->lock);
 	list_for_each_entry(inst, &core->instances, list) {
 		if (inst->session_id == session_id) {
+			/*
+			 * Take a reference under core->lock, paired with
+			 * iris_inst_put() once the caller is done, so the
+			 * instance cannot be freed by a concurrent close().
+			 */
+			kref_get(&inst->kref);
 			mutex_unlock(&core->lock);
 			return inst;
 		}
drivers/media/platform/qcom/iris/iris_utils.h +1 -0
index 228a5f963812..be23acc0e848 100644
@@ -48,6 +48,7 @@ bool iris_split_mode_enabled(struct iris_inst *inst);
 bool iris_fmt_is_8bit(u32 pixelformat);
 bool iris_fmt_is_10bit(u32 pixelformat);
 struct iris_inst *iris_get_instance(struct iris_core *core, u32 session_id);
+void iris_inst_put(struct iris_inst *inst);
 void iris_helper_buffers_done(struct iris_inst *inst, unsigned int type,
 			      enum vb2_buffer_state state);
 int iris_wait_for_session_response(struct iris_inst *inst, bool is_flush);
drivers/media/platform/qcom/iris/iris_vidc.c +18 -5
index 4ca9185b3d2b..56c6c1b0ac16 100644
@@ -40,6 +40,22 @@ static void iris_v4l2_fh_deinit(struct iris_inst *inst, struct file *filp)
 	v4l2_fh_exit(&inst->fh);
 }
 
+static void iris_inst_release(struct kref *kref)
+{
+	struct iris_inst *inst = container_of(kref, struct iris_inst, kref);
+
+	mutex_destroy(&inst->ctx_q_lock);
+	mutex_destroy(&inst->lock);
+	kfree(inst->fmt_src);
+	kfree(inst->fmt_dst);
+	kfree(inst);
+}
+
+void iris_inst_put(struct iris_inst *inst)
+{
+	kref_put(&inst->kref, iris_inst_release);
+}
+
 static int iris_add_session(struct iris_inst *inst)
 {
 	struct iris_core *core = inst->core;
@@ -167,6 +183,7 @@ int iris_open(struct file *filp)
 	inst->domain = session_type;
 	inst->session_id = hash32_ptr(inst);
 	inst->state = IRIS_INST_DEINIT;
+	kref_init(&inst->kref);
 
 	mutex_init(&inst->lock);
 	mutex_init(&inst->ctx_q_lock);
@@ -308,11 +325,7 @@ int iris_close(struct file *filp)
 	iris_check_num_queued_internal_buffers(inst, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
 	iris_remove_session(inst);
 	mutex_unlock(&inst->lock);
-	mutex_destroy(&inst->ctx_q_lock);
-	mutex_destroy(&inst->lock);
-	kfree(inst->fmt_src);
-	kfree(inst->fmt_dst);
-	kfree(inst);
+	iris_inst_put(inst);
 
 	return 0;
 }
Signature · 1 line
2.47.3
#9 lore raw

Re: [PATCH v2 5/5] media: iris: reference count video instances

Quoted 7 lines · Dmitry Baryshkov

iris_get_instance() looks up an instance on core->instances and returns it after dropping core->lock, without taking any reference. The threaded interrupt handler uses this to find the instance a firmware response belongs to and then takes inst->lock. Meanwhile userspace may close the same file descriptor: iris_close() removes the instance from the list, destroys inst->lock and frees the instance. The interrupt handler then operates on freed memory and a destroyed mutex, a use-after-free.

[...]

drivers/media/platform/qcom/iris/iris_utils.c struct iris_inst *iris_get_instance(struct iris_core *core, u32 session_id) · line 101

Quoted 11 lines
drivers/media/platform/qcom/iris/iris_utils.c +6 -0
@@ -92,6 +92,12 @@ struct iris_inst *iris_get_instance(struct iris_core *core, u32 session_id)
 	mutex_lock(&core->lock);
 	list_for_each_entry(inst, &core->instances, list) {
 		if (inst->session_id == session_id) {
+			/*
+			 * Take a reference under core->lock, paired with
+			 * iris_inst_put() once the caller is done, so the
+			 * instance cannot be freed by a concurrent close().
+			 */
+			kref_get(&inst->kref);
 			mutex_unlock(&core->lock);

But none of the _puts() are under the core lock?

Konrad