Skip to contentSkip to conversation

media: s5p-mfc: Fix potential deadlock on condlock

PATCH In review Chengfeng Ye 2 participants 2 messages Sep 26, 2023 – Sep 29, 2023

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

PATCH 1/1 media: s5p-mfc: Fix potential deadlock on condlock

As &dev->condlock is acquired under irq context along the following call chain from s5p_mfc_irq(), other acquisition of the same lock inside process context or softirq context should disable irq avoid double lock. enc_post_frame_start() seems to be one such function that execute under process context or softirq context.

<deadlock #1>

enc_post_frame_start()
--> clear_work_bit()
--> spin_loc(&dev->condlock)
<interrupt>
--> s5p_mfc_irq()
--> s5p_mfc_handle_frame()
--> clear_work_bit()
--> spin_lock(&dev->condlock)

This flaw was found by an experimental static analysis tool I am developing for irq-related deadlock.

To prevent the potential deadlock, the patch change clear_work_bit() inside enc_post_frame_start() to clear_work_bit_irqsave().

---
 drivers/media/platform/samsung/s5p-mfc/s5p_mfc_enc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/samsung/s5p-mfc/s5p_mfc_enc.c +1 -1
index f62703cebb77..4b4c129c09e7 100644
@@ -1297,7 +1297,7 @@ static int enc_post_frame_start(struct s5p_mfc_ctx *ctx)
 	if (ctx->state == MFCINST_FINISHING && ctx->ref_queue_cnt == 0)
 		src_ready = false;
 	if (!src_ready || ctx->dst_queue_cnt == 0)
-		clear_work_bit(ctx);
+		clear_work_bit_irqsave(ctx);
 
 	return 0;
 }
Signature · 1 line
2.17.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 17 lines · Chengfeng Ye

As &dev->condlock is acquired under irq context along the following call chain from s5p_mfc_irq(), other acquisition of the same lock inside process context or softirq context should disable irq avoid double lock. enc_post_frame_start() seems to be one such function that execute under process context or softirq context.

<deadlock #1>

enc_post_frame_start()
--> clear_work_bit()
--> spin_loc(&dev->condlock)
<interrupt>

    --> s5p_mfc_irq()
    --> s5p_mfc_handle_frame()
    --> clear_work_bit()
    --> spin_lock(&dev->condlock)
Quoted 7 lines

This flaw was found by an experimental static analysis tool I am developing for irq-related deadlock.

To prevent the potential deadlock, the patch change clear_work_bit() inside enc_post_frame_start() to clear_work_bit_irqsave().

Quoted 9 lines
---
  drivers/media/platform/samsung/s5p-mfc/s5p_mfc_enc.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/samsung/s5p-mfc/s5p_mfc_enc.c +0 -0
index f62703cebb77..4b4c129c09e7 100644

drivers/media/platform/samsung/s5p-mfc/s5p_mfc_enc.c static int enc_post_frame_start(struct s5p_mfc_ctx *ctx) · line 1303

Quoted 8 lines
drivers/media/platform/samsung/s5p-mfc/s5p_mfc_enc.c +1 -1
@@ -1297,7 +1297,7 @@ static int enc_post_frame_start(struct s5p_mfc_ctx *ctx)
  	if (ctx->state == MFCINST_FINISHING && ctx->ref_queue_cnt == 0)
  		src_ready = false;
  	if (!src_ready || ctx->dst_queue_cnt == 0)
-		clear_work_bit(ctx);
+		clear_work_bit_irqsave(ctx);
 
  	return 0;
  }

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland