Skip to contentSkip to conversation

mailbox/pcc.c move shmem unmap to teardown

PATCH In review Adam Young 4 participants 7 messages May 15 – May 22

Subsystems (4)
lore
Share & tools
lore

Adam Young

author
#0 lore raw

PATCH 1/1 mailbox/pcc.c move shmem unmap to teardown

If the mailbox IRQ and shmems are not cleaned up atomically there is a race condition. If the shmem is torn down while the IRQ is active, a late interrupt can trigger a write to un-mapped memory.
If the shmem is torn down after the IRQ, and another thread requests the channel again, we can end up with a channel that has had its shmem unmapped.

By moving the unmap to the teardown process, we can let the mailbox mechanism prevent re-entrance into the startup/teardown functions.

Assisted-by Codex:gpt-5.4
Fixes fa362ffafa51 ("mailbox: pcc: Always map the shared memory communication address")
---
 drivers/mailbox/pcc.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)
drivers/mailbox/pcc.c +8 -11
index 636879ae1db7..7fa2f6b5c492 100644
@@ -401,18 +401,8 @@ EXPORT_SYMBOL_GPL(pcc_mbox_request_channel);
 void pcc_mbox_free_channel(struct pcc_mbox_chan *pchan)
 {
 	struct mbox_chan *chan = pchan->mchan;
-	struct pcc_chan_info *pchan_info;
-	struct pcc_mbox_chan *pcc_mbox_chan;
-
 	if (!chan || !chan->cl)
 		return;
-	pchan_info = chan->con_priv;
-	pcc_mbox_chan = &pchan_info->chan;
-	if (pcc_mbox_chan->shmem) {
-		iounmap(pcc_mbox_chan->shmem);
-		pcc_mbox_chan->shmem = NULL;
-	}
-
 	mbox_free_channel(chan);
 }
 EXPORT_SYMBOL_GPL(pcc_mbox_free_channel);
@@ -488,15 +478,22 @@ static int pcc_startup(struct mbox_chan *chan)
 
 /**
  * pcc_shutdown - Called from Mailbox Controller code. Used here
- *		to free the interrupt.
+ *		to free the interrupt and unmap the shared memory.
  * @chan: Pointer to Mailbox channel to shutdown.
  */
 static void pcc_shutdown(struct mbox_chan *chan)
 {
 	struct pcc_chan_info *pchan = chan->con_priv;
+	struct pcc_mbox_chan *pcc_mbox_chan;
 
 	if (pchan->plat_irq > 0)
 		devm_free_irq(chan->mbox->dev, pchan->plat_irq, chan);
+
+	pcc_mbox_chan = &pchan->chan;
+	if (pcc_mbox_chan->shmem) {
+		iounmap(pcc_mbox_chan->shmem);
+		pcc_mbox_chan->shmem = NULL;
+	}
 }
 
 static const struct mbox_chan_ops pcc_chan_ops = {
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Double Unmap / iounmap(NULL) in pcc_mbox_request_channel error path. - [High] TOCTOU Race…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Double Unmap / iounmap(NULL) in pcc_mbox_request_channel error path.
- [High] TOCTOU Race Condition in pcc_mbox_request_channel leading to panic. --

commit b19a3a11311850c88f208492b1f19cd0503eefd7
Author: Adam Young <[email protected]>

mailbox/pcc.c move shmem unmap to teardown

This commit moves the shared memory unmapping from pcc_mbox_free_channel() to the pcc_shutdown() callback. This is intended to fix a race condition where late interrupts could write to unmapped memory if the channel was torn down while the IRQ was active.

Quoted 4 lines
drivers/mailbox/pcc.c +0 -0
index 636879ae1db76..7fa2f6b5c492a 100644

[ ... ]

Quoted 12 lines
drivers/mailbox/pcc.c +2 -1
@@ -488,15 +478,22 @@ static int pcc_startup(struct mbox_chan *chan)
 
 /**
  * pcc_shutdown - Called from Mailbox Controller code. Used here
- *		to free the interrupt.
+ *		to free the interrupt and unmap the shared memory.
  * @chan: Pointer to Mailbox channel to shutdown.
  */
 static void pcc_shutdown(struct mbox_chan *chan)
 {
 	struct pcc_chan_info *pchan = chan->con_priv;
+	struct pcc_mbox_chan *pcc_mbox_chan;

drivers/mailbox/pcc.c static int pcc_startup(struct mbox_chan *chan) · line 497

Quoted 10 lines
drivers/mailbox/pcc.c +6 -0
 
 	if (pchan->plat_irq > 0)
 		devm_free_irq(chan->mbox->dev, pchan->plat_irq, chan);
+
+	pcc_mbox_chan = &pchan->chan;
+	if (pcc_mbox_chan->shmem) {
+		iounmap(pcc_mbox_chan->shmem);
+		pcc_mbox_chan->shmem = NULL;
+	}
 }

Will this cause a regression with a double unmap and an iounmap(NULL) call during the error path of pcc_mbox_request_channel()?

If mbox_bind_client() fails during its startup phase, the mailbox core automatically unwinds by calling mbox_free_channel(), which invokes this updated pcc_shutdown() callback.

When control returns to pcc_mbox_request_channel(), it sees the failure and executes its own error path:

drivers/mailbox/pcc.c:pcc_mbox_request_channel() {
...

	rc = mbox_bind_client(chan, cl);
	if (rc) {
		iounmap(pcc_mchan->shmem);
		pcc_mchan->shmem = NULL;
		return ERR_PTR(rc);
	}

...
}

Since the pointer was already set to NULL in pcc_shutdown(), won't this unconditionally execute iounmap(NULL)? This violates kernel API contracts and can trigger a stack trace on architectures like x86, or potentially cause a kernel oops. Should an explicit check for pcc_mchan->shmem be added to the error path?

Additionally, there appears to be a pre-existing regression in pcc_mbox_request_channel() that wasn't introduced by this patch.

If two threads request the same subspace simultaneously, can both observe chan->cl == NULL without locks and proceed to map the shared memory?

drivers/mailbox/pcc.c:pcc_mbox_request_channel() {
...

	if (IS_ERR(chan) || chan->cl) {
		pr_err("Channel not found for idx: %d\n", subspace_id);
		return ERR_PTR(-EBUSY);
	}

	pcc_mchan = &pchan->chan;
	pcc_mchan->shmem = acpi_os_ioremap(pcc_mchan->shmem_base_addr,
					   pcc_mchan->shmem_size);

...

Both threads blindly overwrite the shared pcc_mchan->shmem pointer. When both threads call mbox_bind_client(), one thread will successfully bind and begin using the channel, while the second thread will fail with -EBUSY.

The second thread will then enter its error path:

	rc = mbox_bind_client(chan, cl);
	if (rc) {
		iounmap(pcc_mchan->shmem);
		pcc_mchan->shmem = NULL;
		return ERR_PTR(rc);
	}

Doesn't this unmap the active memory mapping for the winning thread? The next time the winning thread interacts with the hardware, won't it dereference the NULL pointer or access unmapped memory, resulting in a kernel panic?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 49 lines · On 5/15/26 12:49, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Double Unmap / iounmap(NULL) in pcc_mbox_request_channel error path.
- [High] TOCTOU Race Condition in pcc_mbox_request_channel leading to panic. --

commit b19a3a11311850c88f208492b1f19cd0503eefd7
Author: Adam Young <[email protected]>

mailbox/pcc.c move shmem unmap to teardown

This commit moves the shared memory unmapping from pcc_mbox_free_channel() to the pcc_shutdown() callback. This is intended to fix a race condition where late interrupts could write to unmapped memory if the channel was torn down while the IRQ was active.

Quoted 4 lines
drivers/mailbox/pcc.c +0 -0
index 636879ae1db76..7fa2f6b5c492a 100644

[ ... ]

Quoted 12 lines
drivers/mailbox/pcc.c +2 -1
@@ -488,15 +478,22 @@ static int pcc_startup(struct mbox_chan *chan)
 
  /**
   * pcc_shutdown - Called from Mailbox Controller code. Used here
- *		to free the interrupt.
+ *		to free the interrupt and unmap the shared memory.
   * @chan: Pointer to Mailbox channel to shutdown.
   */
  static void pcc_shutdown(struct mbox_chan *chan)
  {
  	struct pcc_chan_info *pchan = chan->con_priv;
+	struct pcc_mbox_chan *pcc_mbox_chan;

drivers/mailbox/pcc.c static int pcc_startup(struct mbox_chan *chan) · line 497

Quoted 10 lines
drivers/mailbox/pcc.c +6 -0
 
  	if (pchan->plat_irq > 0)
  		devm_free_irq(chan->mbox->dev, pchan->plat_irq, chan);
+
+	pcc_mbox_chan = &pchan->chan;
+	if (pcc_mbox_chan->shmem) {
+		iounmap(pcc_mbox_chan->shmem);
+		pcc_mbox_chan->shmem = NULL;
+	}
  }

Will this cause a regression with a double unmap and an iounmap(NULL) call during the error path of pcc_mbox_request_channel()?

If mbox_bind_client() fails during its startup phase, the mailbox core automatically unwinds by calling mbox_free_channel(), which invokes this updated pcc_shutdown() callback.

Quoted 8 lines

When control returns to pcc_mbox_request_channel(), it sees the failure and executes its own error path:

drivers/mailbox/pcc.c:pcc_mbox_request_channel() {
...

	rc = mbox_bind_client(chan, cl);
	if (rc) {
		iounmap(pcc_mchan->shmem);

Yes this call needs to be removed.

Quoted 12 lines
		pcc_mchan->shmem = NULL;
		return ERR_PTR(rc);
	}

...
}

Since the pointer was already set to NULL in pcc_shutdown(), won't this unconditionally execute iounmap(NULL)? This violates kernel API contracts and can trigger a stack trace on architectures like x86, or potentially cause a kernel oops. Should an explicit check for pcc_mchan->shmem be added to the error path?

Quoted 5 lines

Additionally, there appears to be a pre-existing regression in pcc_mbox_request_channel() that wasn't introduced by this patch.

If two threads request the same subspace simultaneously, can both observe chan->cl == NULL without locks and proceed to map the shared memory?

No they can't.  Locking needs to be introduced but is beyond the scope of this patch.

Quoted 32 lines

drivers/mailbox/pcc.c:pcc_mbox_request_channel() {
...

	if (IS_ERR(chan) || chan->cl) {
		pr_err("Channel not found for idx: %d\n", subspace_id);
		return ERR_PTR(-EBUSY);
	}

	pcc_mchan = &pchan->chan;
	pcc_mchan->shmem = acpi_os_ioremap(pcc_mchan->shmem_base_addr,
					   pcc_mchan->shmem_size);

...

Both threads blindly overwrite the shared pcc_mchan->shmem pointer. When both threads call mbox_bind_client(), one thread will successfully bind and begin using the channel, while the second thread will fail with -EBUSY.

The second thread will then enter its error path:

	rc = mbox_bind_client(chan, cl);
	if (rc) {
		iounmap(pcc_mchan->shmem);
		pcc_mchan->shmem = NULL;
		return ERR_PTR(rc);
	}

Doesn't this unmap the active memory mapping for the winning thread? The next time the winning thread interacts with the hardware, won't it dereference the NULL pointer or access unmapped memory, resulting in a kernel panic?

maintainer
#3 lore raw
In reply to Adam Young
Quoted 10 lines · Adam Young

If the mailbox IRQ and shmems are not cleaned up atomically there is a race condition. If the shmem is torn down while the IRQ is active, a late interrupt can trigger a write to un-mapped memory.
If the shmem is torn down after the IRQ, and another thread requests the channel again, we can end up with a channel that has had its shmem unmapped.

By moving the unmap to the teardown process, we can let the mailbox mechanism prevent re-entrance into the startup/teardown functions.

The startup-failure path now pass NULL to iounmap() in pcc_mbox_request_channel().

__mbox_bind_client() calls mbox_free_channel() before returning an error when ->startup() fails:

With this change, that mbox_free_channel() reaches pcc_shutdown() and clears pcc_mchan->shmem. Control then returns to pcc_mbox_request_channel(), which still unconditionally calls iounmap(pcc_mchan->shmem) in the error path.

The startup and shutdown calls are symmetrical and similarly the mbox_request and mbox_free. This change is removing that balance.

NACK from me.

Signature · 2 lines
Regards,
Sudeep
#4 lore raw
Quoted 17 lines · Sudeep Holla
Quoted 10 lines · Adam Young

If the mailbox IRQ and shmems are not cleaned up atomically there is a race condition. If the shmem is torn down while the IRQ is active, a late interrupt can trigger a write to un-mapped memory.
If the shmem is torn down after the IRQ, and another thread requests the channel again, we can end up with a channel that has had its shmem unmapped.

By moving the unmap to the teardown process, we can let the mailbox mechanism prevent re-entrance into the startup/teardown functions.

The startup-failure path now pass NULL to iounmap() in pcc_mbox_request_channel().

__mbox_bind_client() calls mbox_free_channel() before returning an error when ->startup() fails:

Quoted 9 lines

With this change, that mbox_free_channel() reaches pcc_shutdown() and clears pcc_mchan->shmem. Control then returns to pcc_mbox_request_channel(), which still unconditionally calls iounmap(pcc_mchan->shmem) in the error path.

The startup and shutdown calls are symmetrical and similarly the mbox_request and mbox_free. This change is removing that balance.

NACK from me.

Yeah, I saw that (since posting) as well.

I have an updated version that moves the map to the startup and that removes the unmmap call from pcc_mbox_request_channel which seems to resolve the balancing issue. I will post shortly.

I was trying to minimize the change by not moving the map portion, but I realize that both need to be under control of the mbox code to get to a multithread safe design.

maintainer
#5 lore raw
Quoted 30 lines · Adam Young
Quoted 17 lines · Sudeep Holla
Quoted 10 lines · Adam Young

If the mailbox IRQ and shmems are not cleaned up atomically there is a race condition. If the shmem is torn down while the IRQ is active, a late interrupt can trigger a write to un-mapped memory.
If the shmem is torn down after the IRQ, and another thread requests the channel again, we can end up with a channel that has had its shmem unmapped.

By moving the unmap to the teardown process, we can let the mailbox mechanism prevent re-entrance into the startup/teardown functions.

The startup-failure path now pass NULL to iounmap() in pcc_mbox_request_channel().

__mbox_bind_client() calls mbox_free_channel() before returning an error when ->startup() fails:

Quoted 9 lines

With this change, that mbox_free_channel() reaches pcc_shutdown() and clears pcc_mchan->shmem. Control then returns to pcc_mbox_request_channel(), which still unconditionally calls iounmap(pcc_mchan->shmem) in the error path.

The startup and shutdown calls are symmetrical and similarly the mbox_request and mbox_free. This change is removing that balance.

NACK from me.

Yeah, I saw that (since posting) as well.

Quoted 8 lines

I have an updated version that moves the map to the startup and that removes the unmmap call from pcc_mbox_request_channel which seems to resolve the balancing issue. I will post shortly.

I was trying to minimize the change by not moving the map portion, but I realize that both need to be under control of the mbox code to get to a multithread safe design.

But you fail to explain why after shutdown, a late interrupt arrives. I can't follow that.

Signature · 2 lines
Regards,
Sudeep
#6 lore raw
Quoted 34 lines · Sudeep Holla
Quoted 33 lines · Adam Young
Quoted 17 lines · Sudeep Holla
Quoted 10 lines · Adam Young

If the mailbox IRQ and shmems are not cleaned up atomically there is a race condition. If the shmem is torn down while the IRQ is active, a late interrupt can trigger a write to un-mapped memory.
If the shmem is torn down after the IRQ, and another thread requests the channel again, we can end up with a channel that has had its shmem unmapped.

By moving the unmap to the teardown process, we can let the mailbox mechanism prevent re-entrance into the startup/teardown functions.

The startup-failure path now pass NULL to iounmap() in pcc_mbox_request_channel().

__mbox_bind_client() calls mbox_free_channel() before returning an error when ->startup() fails:

Quoted 9 lines

With this change, that mbox_free_channel() reaches pcc_shutdown() and clears pcc_mchan->shmem. Control then returns to pcc_mbox_request_channel(), which still unconditionally calls iounmap(pcc_mchan->shmem) in the error path.

The startup and shutdown calls are symmetrical and similarly the mbox_request and mbox_free. This change is removing that balance.

NACK from me.

Yeah, I saw that (since posting) as well.

I have an updated version that moves the map to the startup and that removes the unmmap call from pcc_mbox_request_channel which seems to resolve the balancing issue. I will post shortly.

Quoted 6 lines
Quoted 4 lines

I was trying to minimize the change by not moving the map portion, but I realize that both need to be under control of the mbox code to get to a multithread safe design.

But you fail to explain why after shutdown, a late interrupt arrives. I can't follow that.

We can't really control the platform.  We could be in a case where the platform is taking a really long time to process a task, and the user gets impatient and attempts to restart the link as a debugging step, and the platform completes and sends a response interrupt, or sends a packet on a type 4 interface as a response.