From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, Elie Morisse <syniurge@gmail.com>,
Shyam Sundar S K <shyam-sundar.s-k@amd.com>,
Andi Shyti <andi.shyti@kernel.org>,
Bakulchandra Shah <Nehal-bakulchandra.shah@amd.com>,
Wolfram Sang <wsa@kernel.org>,
linux-i2c@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH 08/10] i2c: amd-mp2: Synchronise IRQ completion with command timeout
Date: Thu, 3 Dec 2026 18:31:12 +0000 [thread overview]
Message-ID: <20261203183214.1939736-9-lee@kernel.org> (raw)
In-Reply-To: <20261203183214.1939736-1-lee@kernel.org>
On transfer timeout, amd_mp2_rw_timeout() resets i2c_common->reqcmd to
i2c_none without synchronising against amd_mp2_irq_isr() or clearing
i2c_common->msg. A delayed interrupt then invokes
i2c_amd_cmd_completion(), which dereferences the freed
i2c_common->msg->buf in dev_dbg() and unconditionally calls complete(),
prematurely waking a subsequent transfer.
Protect command state transitions and IRQ completion dispatch with a
spinlock, ignore interrupts when reqcmd is i2c_none, clear
i2c_common->msg when a command ends, and drop the racy msg->buf
dev_dbg() from i2c_amd_cmd_completion().
Fixes: 529766e0a011 ("i2c: Add drivers for the AMD PCIe MP2 I2C controller")
Closes: https://sashiko.dev/#/bug/linux-e7a691b1-b6dc-40c3-9444-f514df014d30
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
drivers/i2c/busses/i2c-amd-mp2-pci.c | 29 +++++++++++++++++++++------
drivers/i2c/busses/i2c-amd-mp2-plat.c | 5 -----
drivers/i2c/busses/i2c-amd-mp2.h | 3 +++
3 files changed, 26 insertions(+), 11 deletions(-)
diff --git a/drivers/i2c/busses/i2c-amd-mp2-pci.c b/drivers/i2c/busses/i2c-amd-mp2-pci.c
index cc24a4f648fa..495a131dcfc3 100644
--- a/drivers/i2c/busses/i2c-amd-mp2-pci.c
+++ b/drivers/i2c/busses/i2c-amd-mp2-pci.c
@@ -43,13 +43,15 @@ static int amd_mp2_cmd(struct amd_i2c_common *i2c_common,
union i2c_cmd_base i2c_cmd_base)
{
struct amd_mp2_dev *privdata = i2c_common->mp2_dev;
+ unsigned long flags;
void __iomem *reg;
- i2c_common->reqcmd = i2c_cmd_base.s.i2c_cmd;
-
reg = privdata->mmio + ((i2c_cmd_base.s.bus_id == 1) ?
AMD_C2P_MSG1 : AMD_C2P_MSG0);
+ spin_lock_irqsave(&privdata->lock, flags);
+ i2c_common->reqcmd = i2c_cmd_base.s.i2c_cmd;
writel(i2c_cmd_base.ul, reg);
+ spin_unlock_irqrestore(&privdata->lock, flags);
return 0;
}
@@ -184,6 +186,7 @@ void amd_mp2_process_event(struct amd_i2c_common *i2c_common)
{
struct amd_mp2_dev *privdata = i2c_common->mp2_dev;
struct pci_dev *pdev = privdata->pci_dev;
+ unsigned long flags;
if (unlikely(i2c_common->reqcmd == i2c_none)) {
pci_warn(pdev, "received msg but no cmd was sent (bus = %d)!\n",
@@ -193,7 +196,10 @@ void amd_mp2_process_event(struct amd_i2c_common *i2c_common)
__amd_mp2_process_event(i2c_common);
+ spin_lock_irqsave(&privdata->lock, flags);
i2c_common->reqcmd = i2c_none;
+ i2c_common->msg = NULL;
+ spin_unlock_irqrestore(&privdata->lock, flags);
amd_mp2_c2p_mutex_unlock(i2c_common);
}
EXPORT_SYMBOL_GPL(amd_mp2_process_event);
@@ -203,6 +209,7 @@ static irqreturn_t amd_mp2_irq_isr(int irq, void *dev)
struct amd_mp2_dev *privdata = dev;
struct pci_dev *pdev = privdata->pci_dev;
struct amd_i2c_common *i2c_common;
+ unsigned long flags;
u32 val;
unsigned int bus_id;
void __iomem *reg;
@@ -215,15 +222,19 @@ static irqreturn_t amd_mp2_irq_isr(int irq, void *dev)
reg = privdata->mmio + ((bus_id == 0) ?
AMD_P2C_MSG1 : AMD_P2C_MSG2);
+ spin_lock_irqsave(&privdata->lock, flags);
val = readl(reg);
if (val != 0) {
writel(0, reg);
writel(0, privdata->mmio + AMD_P2C_MSG_INTEN);
- i2c_common->eventval.ul = val;
- i2c_common->cmd_completion(i2c_common);
+ if (i2c_common->reqcmd != i2c_none) {
+ i2c_common->eventval.ul = val;
+ i2c_common->cmd_completion(i2c_common);
+ }
ret = IRQ_HANDLED;
}
+ spin_unlock_irqrestore(&privdata->lock, flags);
}
if (ret != IRQ_HANDLED) {
@@ -240,7 +251,13 @@ static irqreturn_t amd_mp2_irq_isr(int irq, void *dev)
void amd_mp2_rw_timeout(struct amd_i2c_common *i2c_common)
{
+ struct amd_mp2_dev *privdata = i2c_common->mp2_dev;
+ unsigned long flags;
+
+ spin_lock_irqsave(&privdata->lock, flags);
i2c_common->reqcmd = i2c_none;
+ i2c_common->msg = NULL;
+ spin_unlock_irqrestore(&privdata->lock, flags);
amd_mp2_c2p_mutex_unlock(i2c_common);
}
EXPORT_SYMBOL_GPL(amd_mp2_rw_timeout);
@@ -350,12 +367,12 @@ static int amd_mp2_pci_probe(struct pci_dev *pci_dev,
return -ENOMEM;
privdata->pci_dev = pci_dev;
+ mutex_init(&privdata->c2p_lock);
+ spin_lock_init(&privdata->lock);
rc = amd_mp2_pci_init(privdata, pci_dev);
if (rc)
return rc;
- mutex_init(&privdata->c2p_lock);
-
pm_runtime_set_autosuspend_delay(&pci_dev->dev, 1000);
pm_runtime_use_autosuspend(&pci_dev->dev);
pm_runtime_put_autosuspend(&pci_dev->dev);
diff --git a/drivers/i2c/busses/i2c-amd-mp2-plat.c b/drivers/i2c/busses/i2c-amd-mp2-plat.c
index 6fa369be3981..9d4f8d0cb15b 100644
--- a/drivers/i2c/busses/i2c-amd-mp2-plat.c
+++ b/drivers/i2c/busses/i2c-amd-mp2-plat.c
@@ -85,11 +85,6 @@ static void i2c_amd_start_cmd(struct amd_i2c_dev *i2c_dev)
static void i2c_amd_cmd_completion(struct amd_i2c_common *i2c_common)
{
struct amd_i2c_dev *i2c_dev = amd_i2c_dev_common(i2c_common);
- union i2c_event *event = &i2c_common->eventval;
-
- if (event->r.status == i2c_readcomplete_event)
- dev_dbg(&i2c_dev->pdev->dev, "readdata:%*ph\n", event->r.length,
- i2c_common->msg->buf);
complete(&i2c_dev->cmd_complete);
}
diff --git a/drivers/i2c/busses/i2c-amd-mp2.h b/drivers/i2c/busses/i2c-amd-mp2.h
index 9b7e9494dd12..d883606be3cf 100644
--- a/drivers/i2c/busses/i2c-amd-mp2.h
+++ b/drivers/i2c/busses/i2c-amd-mp2.h
@@ -12,6 +12,7 @@
#include <linux/i2c.h>
#include <linux/pci.h>
#include <linux/pm_runtime.h>
+#include <linux/spinlock.h>
#define PCI_DEVICE_ID_AMD_MP2 0x15E6
@@ -174,6 +175,7 @@ struct amd_i2c_common {
* @mmio: iommapped registers
* @c2p_lock: controls access to the C2P mailbox shared between
* the two adapters
+ * @lock: protects command state against concurrent IRQ handling
* @c2p_lock_busid: id of the adapter which locked c2p_lock
*/
struct amd_mp2_dev {
@@ -181,6 +183,7 @@ struct amd_mp2_dev {
struct amd_i2c_common *busses[2];
void __iomem *mmio;
struct mutex c2p_lock;
+ spinlock_t lock; /* protects command state against IRQ handling */
u8 c2p_lock_busid;
unsigned int probed;
int dev_irq;
--
2.56.1.911.g883d8bcb20-goog
next prev parent reply other threads:[~2026-12-03 18:33 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20261203183214.1939736-1-lee@kernel.org>
2026-12-03 18:31 ` [PATCH 01/10] i2c: imx-lpi2c: Clear rx_cmd_buf pointer after freeing in lpi2c_imx_dma_xfer() Lee Jones
2026-12-03 18:31 ` [PATCH 02/10] i2c: qcom-geni: Clear dma_buf pointer after releasing DMA buffer Lee Jones
2026-12-03 18:31 ` [PATCH 03/10] i2c: sh_mobile: Terminate DMA before freeing bounce buffer in sh_mobile_xfer() Lee Jones
2026-12-03 18:31 ` [PATCH 04/10] i2c: omap: Check buf_len before buffer accesses in data transfer helpers Lee Jones
2026-12-03 18:31 ` [PATCH 05/10] i2c: stm32f7: Prevent duplicate execution of stm32f7_i2c_dma_callback() Lee Jones
2026-12-03 18:31 ` [PATCH 06/10] i2c: stm32f7: Allocate smbus_buf separately for DMA safety Lee Jones
2026-12-03 18:31 ` [PATCH 07/10] i2c: usbio: Validate full reply length in split-read loop Lee Jones
2026-12-03 18:31 ` Lee Jones [this message]
2026-12-03 18:31 ` [PATCH 09/10] i2c: mlxbf: Reject zero-length SMBus reads to prevent stack overflow Lee Jones
2026-12-03 18:31 ` [PATCH 10/10] i2c: octeon: Guard to_pci_dev() with dev_is_pci() in octeon_i2c_set_clock() Lee Jones
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261203183214.1939736-9-lee@kernel.org \
--to=lee@kernel.org \
--cc=Nehal-bakulchandra.shah@amd.com \
--cc=andi.shyti@kernel.org \
--cc=linux-i2c@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=shyam-sundar.s-k@amd.com \
--cc=stable@vger.kernel.org \
--cc=syniurge@gmail.com \
--cc=wsa@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox