Skip to contentSkip to conversation

bus: mhi: host: Fix up null pointer access in mhi_irq_handler

PATCH Applied Qiang Yu 3 participants 9 messages Jul 22, 2022 – Aug 30, 2022

Subsystems (3)
lore
Share & tools
lore

Qiang Yu

author
#0 lore raw

PATCH v3 1/1 bus: mhi: host: Fix up null pointer access in mhi_irq_handler

The irq handler for a shared IRQ ought to be prepared for running even now it's being freed. So let's check the pointer used by mhi_irq_handler to avoid null pointer access since it is probably released before freeing IRQ.

---
v2->v3: add comments
v1->v2: change dev_err to dev_dbg

 drivers/bus/mhi/host/main.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
drivers/bus/mhi/host/main.c +16 -3
index f3aef77a..df0fbfe 100644
@@ -430,12 +430,25 @@ irqreturn_t mhi_irq_handler(int irq_number, void *dev)
 {
 	struct mhi_event *mhi_event = dev;
 	struct mhi_controller *mhi_cntrl = mhi_event->mhi_cntrl;
-	struct mhi_event_ctxt *er_ctxt =
-		&mhi_cntrl->mhi_ctxt->er_ctxt[mhi_event->er_index];
+	struct mhi_event_ctxt *er_ctxt;
 	struct mhi_ring *ev_ring = &mhi_event->ring;
-	dma_addr_t ptr = le64_to_cpu(er_ctxt->rp);
+	dma_addr_t ptr;
 	void *dev_rp;
 
+	/*
+	 * If CONFIG_DEBUG_SHIRQ is set, the IRQ handler will get invoked during __free_irq()
+	 * and by that time mhi_ctxt() would've freed. So check for the existence of mhi_ctxt
+	 * before handling the IRQs.
+	 */
+	if (!mhi_cntrl->mhi_ctxt) {
+		dev_dbg(&mhi_cntrl->mhi_dev->dev,
+			"mhi_ctxt has been freed\n");
+		return IRQ_HANDLED;
+	}
+
+	er_ctxt = &mhi_cntrl->mhi_ctxt->er_ctxt[mhi_event->er_index];
+	ptr = le64_to_cpu(er_ctxt->rp);
+
 	if (!is_valid_ring_ptr(ev_ring, ptr)) {
 		dev_err(&mhi_cntrl->mhi_dev->dev,
 			"Event ring rp points outside of the event ring\n");
Signature · 1 line
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.
maintainer Reviewed-by on v3 1/1
#1 lore raw

+ath11k, Kalle

Quoted 6 lines · Qiang Yu

The irq handler for a shared IRQ ought to be prepared for running even now it's being freed. So let's check the pointer used by mhi_irq_handler to avoid null pointer access since it is probably released before freeing IRQ.

Thanks,
Mani

Quoted 43 lines

---
v2->v3: add comments
v1->v2: change dev_err to dev_dbg

 drivers/bus/mhi/host/main.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
drivers/bus/mhi/host/main.c +16 -3
index f3aef77a..df0fbfe 100644
@@ -430,12 +430,25 @@ irqreturn_t mhi_irq_handler(int irq_number, void *dev)
 {
 	struct mhi_event *mhi_event = dev;
 	struct mhi_controller *mhi_cntrl = mhi_event->mhi_cntrl;
-	struct mhi_event_ctxt *er_ctxt =
-		&mhi_cntrl->mhi_ctxt->er_ctxt[mhi_event->er_index];
+	struct mhi_event_ctxt *er_ctxt;
 	struct mhi_ring *ev_ring = &mhi_event->ring;
-	dma_addr_t ptr = le64_to_cpu(er_ctxt->rp);
+	dma_addr_t ptr;
 	void *dev_rp;
 
+	/*
+	 * If CONFIG_DEBUG_SHIRQ is set, the IRQ handler will get invoked during __free_irq()
+	 * and by that time mhi_ctxt() would've freed. So check for the existence of mhi_ctxt
+	 * before handling the IRQs.
+	 */
+	if (!mhi_cntrl->mhi_ctxt) {
+		dev_dbg(&mhi_cntrl->mhi_dev->dev,
+			"mhi_ctxt has been freed\n");
+		return IRQ_HANDLED;
+	}
+
+	er_ctxt = &mhi_cntrl->mhi_ctxt->er_ctxt[mhi_event->er_index];
+	ptr = le64_to_cpu(er_ctxt->rp);
+
 	if (!is_valid_ring_ptr(ev_ring, ptr)) {
 		dev_err(&mhi_cntrl->mhi_dev->dev,
 			"Event ring rp points outside of the event ring\n");
Signature · 1 line
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.
maintainer Reviewed-by on v3 1/1
#2 lore raw
In reply to Qiang Yu

+ath11k, Kalle

Quoted 6 lines · Qiang Yu

The irq handler for a shared IRQ ought to be prepared for running even now it's being freed. So let's check the pointer used by mhi_irq_handler to avoid null pointer access since it is probably released before freeing IRQ.

Thanks,
Mani

Quoted 43 lines

---
v2->v3: add comments
v1->v2: change dev_err to dev_dbg

 drivers/bus/mhi/host/main.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
drivers/bus/mhi/host/main.c +16 -3
index f3aef77a..df0fbfe 100644
@@ -430,12 +430,25 @@ irqreturn_t mhi_irq_handler(int irq_number, void *dev)
 {
 	struct mhi_event *mhi_event = dev;
 	struct mhi_controller *mhi_cntrl = mhi_event->mhi_cntrl;
-	struct mhi_event_ctxt *er_ctxt =
-		&mhi_cntrl->mhi_ctxt->er_ctxt[mhi_event->er_index];
+	struct mhi_event_ctxt *er_ctxt;
 	struct mhi_ring *ev_ring = &mhi_event->ring;
-	dma_addr_t ptr = le64_to_cpu(er_ctxt->rp);
+	dma_addr_t ptr;
 	void *dev_rp;
 
+	/*
+	 * If CONFIG_DEBUG_SHIRQ is set, the IRQ handler will get invoked during __free_irq()
+	 * and by that time mhi_ctxt() would've freed. So check for the existence of mhi_ctxt
+	 * before handling the IRQs.
+	 */
+	if (!mhi_cntrl->mhi_ctxt) {
+		dev_dbg(&mhi_cntrl->mhi_dev->dev,
+			"mhi_ctxt has been freed\n");
+		return IRQ_HANDLED;
+	}
+
+	er_ctxt = &mhi_cntrl->mhi_ctxt->er_ctxt[mhi_event->er_index];
+	ptr = le64_to_cpu(er_ctxt->rp);
+
 	if (!is_valid_ring_ptr(ev_ring, ptr)) {
 		dev_err(&mhi_cntrl->mhi_dev->dev,
 			"Event ring rp points outside of the event ring\n");
Signature · 1 line
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.
Tested-by
#3 lore raw
Quoted 11 lines · Manivannan Sadhasivam

+ath11k, Kalle

Quoted 6 lines · Qiang Yu

The irq handler for a shared IRQ ought to be prepared for running even now it's being freed. So let's check the pointer used by mhi_irq_handler to avoid null pointer access since it is probably released before freeing IRQ.

This fixes the crash and my regression tests pass now, thanks. But please see my question below.

Quoted 10 lines

drivers/bus/mhi/host/main.c irqreturn_t mhi_irq_handler(int irq_number, void *dev) · line 447

Quoted 10 lines
drivers/bus/mhi/host/main.c +10 -0
+	/*
+	 * If CONFIG_DEBUG_SHIRQ is set, the IRQ handler will get invoked during __free_irq()
+	 * and by that time mhi_ctxt() would've freed. So check for the existence of mhi_ctxt
+	 * before handling the IRQs.
+	 */
+	if (!mhi_cntrl->mhi_ctxt) {
+		dev_dbg(&mhi_cntrl->mhi_dev->dev,
+			"mhi_ctxt has been freed\n");
+		return IRQ_HANDLED;
+	}

I don't see any protection accessing mhi_cntrl->mhi_ctxt, is this really free of race conditions?

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
#4 lore raw
Quoted 10 lines · Kalle Valo
Quoted 8 lines · Manivannan Sadhasivam

+ath11k, Kalle

Quoted 5 lines · Qiang Yu

The irq handler for a shared IRQ ought to be prepared for running even now it's being freed. So let's check the pointer used by mhi_irq_handler to avoid null pointer access since it is probably released before freeing IRQ.

Quoted 9 lines
Quoted 3 lines
Quoted 1 line

This fixes the crash and my regression tests pass now, thanks. But please see my question below.

Thanks Kalle!

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/bus/mhi/host/main.c +5 -0
+	/*
+	 * If CONFIG_DEBUG_SHIRQ is set, the IRQ handler will get invoked during __free_irq()
+	 * and by that time mhi_ctxt() would've freed. So check for the existence of mhi_ctxt
+	 * before handling the IRQs.
+	 */
Quoted 9 lines
Quoted 5 lines

drivers/bus/mhi/host/main.c irqreturn_t mhi_irq_handler(int irq_number, void *dev) · line 447

Quoted 5 lines
drivers/bus/mhi/host/main.c +5 -0
+	if (!mhi_cntrl->mhi_ctxt) {
+		dev_dbg(&mhi_cntrl->mhi_dev->dev,
+			"mhi_ctxt has been freed\n");
+		return IRQ_HANDLED;
+	}

I don't see any protection accessing mhi_cntrl->mhi_ctxt, is this really free of race conditions?

As Qiang said, it is safe to access mhi_ctxt here.

Thanks,
Mani

Quoted 4 lines
Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
Signature · 1 line
மணிவண்ணன் சதாசிவம்
#5 lore raw
Quoted 15 lines · Manivannan Sadhasivam
Quoted 14 lines · Kalle Valo
Quoted 11 lines · Manivannan Sadhasivam

+ath11k, Kalle

Quoted 6 lines · Qiang Yu

The irq handler for a shared IRQ ought to be prepared for running even now it's being freed. So let's check the pointer used by mhi_irq_handler to avoid null pointer access since it is probably released before freeing IRQ.

Quoted 7 lines
Quoted 5 lines

This fixes the crash and my regression tests pass now, thanks. But please see my question below.

Thanks Kalle!

I just tested v6.0-rc3 and it's still crashing. What's the plan to get this fix to Linus?

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
#6 lore raw
Quoted 20 lines · Kalle Valo
Quoted 18 lines · Manivannan Sadhasivam
Quoted 17 lines · Kalle Valo
Quoted 11 lines · Manivannan Sadhasivam

+ath11k, Kalle

Quoted 6 lines · Qiang Yu

The irq handler for a shared IRQ ought to be prepared for running even now it's being freed. So let's check the pointer used by mhi_irq_handler to avoid null pointer access since it is probably released before freeing IRQ.

This fixes the crash and my regression tests pass now, thanks. But please see my question below.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Thanks Kalle!

I just tested v6.0-rc3 and it's still crashing. What's the plan to get this fix to Linus?

Sorry for the delay. Just sent the PR to Greg for including it as a fix for v6.0.

Thanks,
Mani

Quoted 5 lines
Signature · 4 lines
-- 
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
Signature · 1 line
மணிவண்ணன் சதாசிவம்
#7 lore raw
Quoted 24 lines · Manivannan Sadhasivam
Quoted 23 lines · Kalle Valo
Quoted 21 lines · Manivannan Sadhasivam
Quoted 19 lines · Kalle Valo
Quoted 11 lines · Manivannan Sadhasivam

+ath11k, Kalle

Quoted 6 lines · Qiang Yu

The irq handler for a shared IRQ ought to be prepared for running even now it's being freed. So let's check the pointer used by mhi_irq_handler to avoid null pointer access since it is probably released before freeing IRQ.

This fixes the crash and my regression tests pass now, thanks. But please see my question below.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Thanks Kalle!

I just tested v6.0-rc3 and it's still crashing. What's the plan to get this fix to Linus?

Sorry for the delay. Just sent the PR to Greg for including it as a fix for v6.0.

Excellent, thank you.

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
maintainer Applied
#8 lore raw
In reply to Qiang Yu
Quoted 6 lines · Qiang Yu

The irq handler for a shared IRQ ought to be prepared for running even now it's being freed. So let's check the pointer used by mhi_irq_handler to avoid null pointer access since it is probably released before freeing IRQ.

Applied to mhi-next! This patch is targeted for v5.20-rcS.

Thanks,
Mani

Quoted 43 lines

---
v2->v3: add comments
v1->v2: change dev_err to dev_dbg

 drivers/bus/mhi/host/main.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
drivers/bus/mhi/host/main.c +16 -3
index f3aef77a..df0fbfe 100644
@@ -430,12 +430,25 @@ irqreturn_t mhi_irq_handler(int irq_number, void *dev)
 {
 	struct mhi_event *mhi_event = dev;
 	struct mhi_controller *mhi_cntrl = mhi_event->mhi_cntrl;
-	struct mhi_event_ctxt *er_ctxt =
-		&mhi_cntrl->mhi_ctxt->er_ctxt[mhi_event->er_index];
+	struct mhi_event_ctxt *er_ctxt;
 	struct mhi_ring *ev_ring = &mhi_event->ring;
-	dma_addr_t ptr = le64_to_cpu(er_ctxt->rp);
+	dma_addr_t ptr;
 	void *dev_rp;
 
+	/*
+	 * If CONFIG_DEBUG_SHIRQ is set, the IRQ handler will get invoked during __free_irq()
+	 * and by that time mhi_ctxt() would've freed. So check for the existence of mhi_ctxt
+	 * before handling the IRQs.
+	 */
+	if (!mhi_cntrl->mhi_ctxt) {
+		dev_dbg(&mhi_cntrl->mhi_dev->dev,
+			"mhi_ctxt has been freed\n");
+		return IRQ_HANDLED;
+	}
+
+	er_ctxt = &mhi_cntrl->mhi_ctxt->er_ctxt[mhi_event->er_index];
+	ptr = le64_to_cpu(er_ctxt->rp);
+
 	if (!is_valid_ring_ptr(ev_ring, ptr)) {
 		dev_err(&mhi_cntrl->mhi_dev->dev,
 			"Event ring rp points outside of the event ring\n");
Signature · 1 line
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.
Signature · 1 line
மணிவண்ணன் சதாசிவம்