mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org
Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org,
	Albert Ou <aou@eecs.berkeley.edu>,
	Alexandre Ghiti <alex@ghiti.fr>,
	Anup Patel <apatel@ventanamicro.com>,
	Jassi Brar <jassisinghbrar@gmail.com>,
	linux-riscv@lists.infradead.org,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Paul Walmsley <pjw@kernel.org>,
	Rahul Pathak <rahul@summations.net>
Subject: [PATCH 03/10] mailbox: riscv-sbi-mpxy: Protect per-CPU shmem access with irqsave
Date: Thu,  3 Dec 2026 08:28:03 +0000	[thread overview]
Message-ID: <bea0760c0ba24a64096797f8dd377738ce21ec11.1796286447.git.lee@kernel.org> (raw)
In-Reply-To: <cover.1796286447.git.lee@kernel.org>

The SBI MPXY helpers evaluate this_cpu_ptr(&mpxy_local) before calling
get_cpu(), allowing CPU migration between computing the per-CPU pointer
and disabling preemption.  They also leave local interrupts enabled
while using the per-CPU shared memory buffer, allowing a hardirq caller
(such as mbox_send_message() or mpxy_mbox_msi_write()) to clobber the
shared memory buffer mid-operation.

Use guard(irqsave)() across all per-CPU shared memory helpers and fetch
this_cpu_ptr(&mpxy_local) inside the IRQ-disabled section.

Fixes: bf3022a4eb11 ("mailbox: Add RISC-V SBI message proxy (MPXY) based mailbox driver")
Closes: https://sashiko.dev/#/bug/linux-d2c1eefe-4777-4ad6-94c5-504e347f3add
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---

Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexandre Ghiti <alex@ghiti.fr>
Cc: Anup Patel <apatel@ventanamicro.com>
Cc: Jassi Brar <jassisinghbrar@gmail.com>
Cc: linux-riscv@lists.infradead.org
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Paul Walmsley <pjw@kernel.org>
Cc: Rahul Pathak <rahul@summations.net>

 drivers/mailbox/riscv-sbi-mpxy-mbox.c | 114 +++++++++++++-------------
 1 file changed, 59 insertions(+), 55 deletions(-)

diff --git a/drivers/mailbox/riscv-sbi-mpxy-mbox.c b/drivers/mailbox/riscv-sbi-mpxy-mbox.c
index 4ebce35a8b6c..90b1972a42ea 100644
--- a/drivers/mailbox/riscv-sbi-mpxy-mbox.c
+++ b/drivers/mailbox/riscv-sbi-mpxy-mbox.c
@@ -6,10 +6,12 @@
  */
 
 #include <linux/acpi.h>
+#include <linux/cleanup.h>
 #include <linux/cpu.h>
 #include <linux/errno.h>
 #include <linux/init.h>
 #include <linux/irqchip/riscv-imsic.h>
+#include <linux/irqflags.h>
 #include <linux/mailbox_controller.h>
 #include <linux/mailbox/riscv-rpmi-message.h>
 #include <linux/minmax.h>
@@ -127,52 +129,57 @@ static bool mpxy_shmem_init_done;
 
 static int mpxy_get_channel_count(u32 *channel_count)
 {
-	struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local);
-	struct sbi_mpxy_channel_ids_data *sdata = mpxy->shmem;
+	struct sbi_mpxy_channel_ids_data *sdata;
+	struct mpxy_local *mpxy;
 	u32 remaining, returned;
 	struct sbiret sret;
 
-	if (!mpxy->shmem_active)
-		return -ENODEV;
 	if (!channel_count)
 		return -EINVAL;
 
-	get_cpu();
+	guard(irqsave)();
+
+	mpxy = this_cpu_ptr(&mpxy_local);
+	if (!mpxy->shmem_active)
+		return -ENODEV;
 
 	/* Get the remaining and returned fields to calculate total */
 	sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_GET_CHANNEL_IDS,
 			 0, 0, 0, 0, 0, 0);
 	if (sret.error)
-		goto err_put_cpu;
+		return sbi_err_map_linux_errno(sret.error);
 
+	sdata = mpxy->shmem;
 	remaining = le32_to_cpu(sdata->remaining);
 	returned = le32_to_cpu(sdata->returned);
 	*channel_count = remaining + returned;
 
-err_put_cpu:
-	put_cpu();
-	return sbi_err_map_linux_errno(sret.error);
+	return 0;
 }
 
 static int mpxy_get_channel_ids(u32 channel_count, u32 *channel_ids)
 {
-	struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local);
-	struct sbi_mpxy_channel_ids_data *sdata = mpxy->shmem;
 	u32 remaining, returned, count, start_index = 0;
+	struct sbi_mpxy_channel_ids_data *sdata;
+	struct mpxy_local *mpxy;
 	struct sbiret sret;
 
-	if (!mpxy->shmem_active)
-		return -ENODEV;
 	if (!channel_count || !channel_ids)
 		return -EINVAL;
 
-	get_cpu();
+	guard(irqsave)();
+
+	mpxy = this_cpu_ptr(&mpxy_local);
+	if (!mpxy->shmem_active)
+		return -ENODEV;
+
+	sdata = mpxy->shmem;
 
 	do {
 		sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_GET_CHANNEL_IDS,
 				 start_index, 0, 0, 0, 0, 0);
 		if (sret.error)
-			goto err_put_cpu;
+			return sbi_err_map_linux_errno(sret.error);
 
 		remaining = le32_to_cpu(sdata->remaining);
 		returned = le32_to_cpu(sdata->returned);
@@ -183,54 +190,53 @@ static int mpxy_get_channel_ids(u32 channel_count, u32 *channel_ids)
 		start_index += count;
 	} while (remaining && start_index < channel_count);
 
-err_put_cpu:
-	put_cpu();
-	return sbi_err_map_linux_errno(sret.error);
+	return 0;
 }
 
 static int mpxy_read_attrs(u32 channel_id, u32 base_attrid, u32 attr_count,
 			   u32 *attrs_buf)
 {
-	struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local);
+	struct mpxy_local *mpxy;
 	struct sbiret sret;
 
-	if (!mpxy->shmem_active)
-		return -ENODEV;
 	if (!attr_count || !attrs_buf)
 		return -EINVAL;
 
-	get_cpu();
+	guard(irqsave)();
+
+	mpxy = this_cpu_ptr(&mpxy_local);
+	if (!mpxy->shmem_active)
+		return -ENODEV;
 
 	sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_READ_ATTRS,
 			 channel_id, base_attrid, attr_count, 0, 0, 0);
 	if (sret.error)
-		goto err_put_cpu;
+		return sbi_err_map_linux_errno(sret.error);
 
 	memcpy_from_le32(attrs_buf, (__le32 *)mpxy->shmem, attr_count);
 
-err_put_cpu:
-	put_cpu();
-	return sbi_err_map_linux_errno(sret.error);
+	return 0;
 }
 
 static int mpxy_write_attrs(u32 channel_id, u32 base_attrid, u32 attr_count,
 			    u32 *attrs_buf)
 {
-	struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local);
+	struct mpxy_local *mpxy;
 	struct sbiret sret;
 
-	if (!mpxy->shmem_active)
-		return -ENODEV;
 	if (!attr_count || !attrs_buf)
 		return -EINVAL;
 
-	get_cpu();
+	guard(irqsave)();
+
+	mpxy = this_cpu_ptr(&mpxy_local);
+	if (!mpxy->shmem_active)
+		return -ENODEV;
 
 	memcpy_to_le32((__le32 *)mpxy->shmem, attrs_buf, attr_count);
 	sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_WRITE_ATTRS,
 			 channel_id, base_attrid, attr_count, 0, 0, 0);
 
-	put_cpu();
 	return sbi_err_map_linux_errno(sret.error);
 }
 
@@ -239,16 +245,18 @@ static int mpxy_send_message_with_resp(u32 channel_id, u32 msg_id,
 				       void *rx, unsigned long max_rx_len,
 				       unsigned long *rx_len)
 {
-	struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local);
+	struct mpxy_local *mpxy;
 	unsigned long rx_bytes;
 	struct sbiret sret;
 
-	if (!mpxy->shmem_active)
-		return -ENODEV;
 	if (!tx && tx_len)
 		return -EINVAL;
 
-	get_cpu();
+	guard(irqsave)();
+
+	mpxy = this_cpu_ptr(&mpxy_local);
+	if (!mpxy->shmem_active)
+		return -ENODEV;
 
 	/* Message protocols allowed to have no data in messages */
 	if (tx_len)
@@ -258,32 +266,31 @@ static int mpxy_send_message_with_resp(u32 channel_id, u32 msg_id,
 			 channel_id, msg_id, tx_len, 0, 0, 0);
 	if (rx && !sret.error) {
 		rx_bytes = sret.value;
-		if (rx_bytes > max_rx_len) {
-			put_cpu();
+		if (rx_bytes > max_rx_len)
 			return -ENOSPC;
-		}
 
 		memcpy(rx, mpxy->shmem, rx_bytes);
 		if (rx_len)
 			*rx_len = rx_bytes;
 	}
 
-	put_cpu();
 	return sbi_err_map_linux_errno(sret.error);
 }
 
 static int mpxy_send_message_without_resp(u32 channel_id, u32 msg_id,
 					  void *tx, unsigned long tx_len)
 {
-	struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local);
+	struct mpxy_local *mpxy;
 	struct sbiret sret;
 
-	if (!mpxy->shmem_active)
-		return -ENODEV;
 	if (!tx && tx_len)
 		return -EINVAL;
 
-	get_cpu();
+	guard(irqsave)();
+
+	mpxy = this_cpu_ptr(&mpxy_local);
+	if (!mpxy->shmem_active)
+		return -ENODEV;
 
 	/* Message protocols allowed to have no data in messages */
 	if (tx_len)
@@ -292,7 +299,6 @@ static int mpxy_send_message_without_resp(u32 channel_id, u32 msg_id,
 	sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_SEND_MSG_WITHOUT_RESP,
 			 channel_id, msg_id, tx_len, 0, 0, 0);
 
-	put_cpu();
 	return sbi_err_map_linux_errno(sret.error);
 }
 
@@ -300,32 +306,30 @@ static int mpxy_get_notifications(u32 channel_id,
 				  struct sbi_mpxy_notification_data *notif_data,
 				  unsigned long *events_data_len)
 {
-	struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local);
+	struct mpxy_local *mpxy;
 	struct sbiret sret;
 
-	if (!mpxy->shmem_active)
-		return -ENODEV;
 	if (!notif_data || !events_data_len)
 		return -EINVAL;
 
-	get_cpu();
+	guard(irqsave)();
+
+	mpxy = this_cpu_ptr(&mpxy_local);
+	if (!mpxy->shmem_active)
+		return -ENODEV;
 
 	sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_GET_NOTIFICATION_EVENTS,
 			 channel_id, 0, 0, 0, 0, 0);
 	if (sret.error)
-		goto err_put_cpu;
+		return sbi_err_map_linux_errno(sret.error);
 	if (sret.value < 0 || mpxy_shmem_size < sizeof(*notif_data) ||
-	    sret.value > mpxy_shmem_size - sizeof(*notif_data)) {
-		put_cpu();
+	    sret.value > mpxy_shmem_size - sizeof(*notif_data))
 		return -EOVERFLOW;
-	}
 
 	memcpy(notif_data, mpxy->shmem, sret.value + sizeof(*notif_data));
 	*events_data_len = sret.value;
 
-err_put_cpu:
-	put_cpu();
-	return sbi_err_map_linux_errno(sret.error);
+	return 0;
 }
 
 static int mpxy_get_shmem_size(unsigned long *shmem_size)
-- 
2.56.1.911.g883d8bcb20-goog


  parent reply	other threads:[~2026-12-03  8:28 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  8:28 [PATCH 00/10] mailbox: Fix Critical and High issues reported by Sashiko Lee Jones
2026-12-03  8:28 ` [PATCH 01/10] mailbox: qcom-ipcc: Skip of_node_put() on phandle parse failure Lee Jones
2026-12-03 10:38   ` Konrad Dybcio
2026-12-03  8:28 ` [PATCH 02/10] mailbox: ti-msgmgr: Use unsigned IDs in ti_msgmgr_of_xlate() Lee Jones
2026-12-03  8:28 ` Lee Jones [this message]
2026-12-03 14:32   ` [PATCH 03/10] mailbox: riscv-sbi-mpxy: Protect per-CPU shmem access with irqsave Rahul Pathak
2026-12-03 15:05     ` Lee Jones
2026-12-03  8:28 ` [PATCH 04/10] mailbox: riscv-sbi-mpxy: Validate shmem bounds in message send helpers Lee Jones
2026-12-03  8:28 ` [PATCH 05/10] mailbox: bcm-pdc: Unregister mailbox controller before destroying DMA pools Lee Jones
2026-12-03  8:28 ` [PATCH 06/10] mailbox: qcom-ipcc: Remove IRQ domain via devm to avoid ISR use-after-free Lee Jones
2026-12-03 10:39   ` Konrad Dybcio
2026-12-03  8:28 ` [PATCH 07/10] mailbox: sun6i-msgbox: Free IRQ before disabling clock on teardown Lee Jones
2026-12-03 13:43   ` Chen-Yu Tsai
2026-12-03  8:28 ` [PATCH 08/10] mailbox: sun6i-msgbox: Synchronise IRQ during channel shutdown Lee Jones
2026-12-03 13:49   ` Chen-Yu Tsai
2026-12-03  8:28 ` [PATCH 09/10] mailbox: arm_mhu_db: Initialise channel registers before controller registration Lee Jones
2026-12-03  9:40   ` Sudeep Holla
2026-12-03  8:28 ` [PATCH 10/10] mailbox: ti-msgmgr: Return -EIO on queue error in ti_msgmgr_send_data() 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=bea0760c0ba24a64096797f8dd377738ce21ec11.1796286447.git.lee@kernel.org \
    --to=lee@kernel.org \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=apatel@ventanamicro.com \
    --cc=jassisinghbrar@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    --cc=rahul@summations.net \
    --cc=stable@vger.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

all inboxes | Powered by JetHome®