mailbox/pcc.c shmem map/unmap in callbacks
Subsystems (4)
PATCH v2 1/1 mailbox/pcc.c shmem map/unmap in callbacks
The mailbox IRQ and shmems are not cleaned up atomically, so 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 while the IRQ is active, and another thread requests the channel again, we can end up with a channel that has had its shmem unmapped.
By moving the map to start up and the unmap to teardown, we can let the mailbox mechanism prevent re-entrance into the startup/teardown functions.
Avoid doubly unmapping the region by removing the unmap in the direct error handler for the request.
---
Previous Version: https://lore.kernel.org/linux-hwmon/[email protected]/
Changes in this Version:
- Move the initial mapping into the startup callback
- No Double unmap on error during setup
--- drivers/mailbox/pcc.c | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-)
drivers/mailbox/pcc.c +18 -24
@@ -360,7 +360,6 @@ static irqreturn_t pcc_mbox_irq(int irq, void *p)struct pcc_mbox_chan *pcc_mbox_request_channel (struct mbox_client *cl,int subspace_id) { -struct pcc_mbox_chan *pcc_mchan;struct pcc_chan_info *pchan;struct mbox_chan *chan;int rc;
@@ -375,20 +374,10 @@ pcc_mbox_request_channel(struct mbox_client *cl, int 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); -if (!pcc_mchan->shmem) -return ERR_PTR (-ENXIO ); - rc =mbox_bind_client (chan, cl); -if (rc) { -iounmap (pcc_mchan->shmem); - pcc_mchan->shmem =NULL ; +if (rc)return ERR_PTR (rc); - } - -return pcc_mchan; +return &pchan->chan; }EXPORT_SYMBOL_GPL (pcc_mbox_request_channel);
@@ -401,18 +390,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);
@@ -462,9 +441,15 @@ static bool pcc_last_tx_done(struct mbox_chan *chan)static int pcc_startup (struct mbox_chan *chan) {struct pcc_chan_info *pchan = chan->con_priv; +struct pcc_mbox_chan *pcc_mchan;unsigned long irqflags;int rc; + pcc_mchan = &pchan->chan; + pcc_mchan->shmem =acpi_os_ioremap (pcc_mchan->shmem_base_addr, + pcc_mchan->shmem_size); +if (pcc_mchan->shmem ==NULL ) +return -ENOMEM ;/* * Clear and acknowledge any pending interrupts on responder channel * before enabling the interrupt
@@ -479,6 +464,8 @@ static int pcc_startup(struct mbox_chan *chan)if (unlikely (rc)) {dev_err (chan->mbox->dev,"failed to register PCC interrupt %d\n" , pchan->plat_irq); +iounmap (pcc_mchan->shmem); + pcc_mchan->shmem =NULL ;return rc; } }
@@ -488,15 +475,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
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Unlocked read-modify-write on potentially shared PCC registers (e.g., doorbell or command update). --
commit b67c701fcb8eb6ced91271505440c781fa1718bc
Author: Adam Young <[email protected]>
Subject: mailbox/pcc.c shmem map/unmap in callbacks
This commit moves the shared memory mapping and unmapping out of the request and free channel functions and into the startup and shutdown callbacks. This aims to prevent race conditions where a late interrupt might write to unmapped memory or a channel is requested after its shmem was torn down.
Quoted 4 lines
drivers/mailbox/pcc.c +0
-0
[ ... ]
Quoted 16 lines
drivers/mailbox/pcc.c +6
-0@@ -462,9 +441,15 @@ static bool pcc_last_tx_done(struct mbox_chan *chan)static int pcc_startup (struct mbox_chan *chan) {struct pcc_chan_info *pchan = chan->con_priv; +struct pcc_mbox_chan *pcc_mchan;unsigned long irqflags;int rc; + pcc_mchan = &pchan->chan; + pcc_mchan->shmem =acpi_os_ioremap (pcc_mchan->shmem_base_addr, + pcc_mchan->shmem_size); +if (pcc_mchan->shmem ==NULL ) +return -ENOMEM ;/* * Clear and acknowledge any pending interrupts on responder channel * before enabling the interrupt
drivers/mailbox/pcc.c
Quoted 2 lines
drivers/mailbox/pcc.c +0
-0*/ pcc_chan_acknowledge (pchan);
[Severity: High]
This is a pre-existing issue, but I noticed a potential race condition when calling pcc_chan_acknowledge() here.
The ACPI PCCT specification allows multiple subspaces to share the same doorbell or command update register. When pcc_chan_acknowledge() calls pcc_chan_reg_read_modify_write(), it performs a non-atomic read, modify, and write sequence on these mapped registers:
drivers/mailbox/pcc.c:pcc_chan_acknowledge() {
...
pcc_chan_reg_read_modify_write(&pchan->cmd_update);
...
if (ioread32(&pcc_hdr->flags) & PCC_CMD_COMPLETION_NOTIFY)
pcc_chan_reg_read_modify_write(&pchan->db);
}Since the mailbox framework only serializes operations on a per-channel basis using the channel lock, could concurrent calls to pcc_send_data() or pcc_chan_acknowledge() for different channels that share the same register race with each other?
If this occurs, could it clobber the register bits belonging to other channels, resulting in lost doorbell rings or lost ACKs?
Quoted 2 lines
if (
pchan->plat_irq> 0) {
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Note that this patch is now a pre-req for the MCTP over PCC driver getting accepted. I was trying to keep the two patches separate, but this fixes a race condition in that, and any other Type4 based response mechanism.
Quoted 132 lines · Adam Young
The mailbox IRQ and shmems are not cleaned up atomically, so 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 while the IRQ is active, and another thread requests the channel again, we can end up with a channel that has had its shmem unmapped.By moving the map to start up and the unmap to teardown, we can let the mailbox mechanism prevent re-entrance into the startup/teardown functions.
Avoid doubly unmapping the region by removing the unmap in the direct error handler for the request.
Assisted-by Codex:gpt-5.4Fixes fa362ffafa51 ("mailbox: pcc: Always map the shared memory communication address")Signed-off-by Adam Young <[email protected]>---
Previous Version: https://lore.kernel.org/linux-hwmon/[email protected]/Changes in this Version:
- Move the initial mapping into the startup callback
- No Double unmap on error during setup--- drivers/mailbox/pcc.c | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-)drivers/mailbox/pcc.c +18
-24@@ -360,7 +360,6 @@ static irqreturn_t pcc_mbox_irq(int irq, void *p)struct pcc_mbox_chan *pcc_mbox_request_channel (struct mbox_client *cl,int subspace_id) { -struct pcc_mbox_chan *pcc_mchan;struct pcc_chan_info *pchan;struct mbox_chan *chan;int rc;@@ -375,20 +374,10 @@ pcc_mbox_request_channel(struct mbox_client *cl, int 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); -if (!pcc_mchan->shmem) -return ERR_PTR (-ENXIO ); - rc =mbox_bind_client (chan, cl); -if (rc) { -iounmap (pcc_mchan->shmem); - pcc_mchan->shmem =NULL ; +if (rc)return ERR_PTR (rc); - } - -return pcc_mchan; +return &pchan->chan; }EXPORT_SYMBOL_GPL (pcc_mbox_request_channel);@@ -401,18 +390,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);@@ -462,9 +441,15 @@ static bool pcc_last_tx_done(struct mbox_chan *chan)static int pcc_startup (struct mbox_chan *chan) {struct pcc_chan_info *pchan = chan->con_priv; +struct pcc_mbox_chan *pcc_mchan;unsigned long irqflags;int rc; + pcc_mchan = &pchan->chan; + pcc_mchan->shmem =acpi_os_ioremap (pcc_mchan->shmem_base_addr, + pcc_mchan->shmem_size); +if (pcc_mchan->shmem ==NULL ) +return -ENOMEM ;/* * Clear and acknowledge any pending interrupts on responder channel * before enabling the interrupt @@ -479,6 +464,8 @@ static int pcc_startup(struct mbox_chan *chan)if (unlikely (rc)) {dev_err (chan->mbox->dev,"failed to register PCC interrupt %d\n" , pchan->plat_irq); +iounmap (pcc_mchan->shmem); + pcc_mchan->shmem =NULL ;return rc; } }@@ -488,15 +475,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 = {