From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796286502; b=xoDGvzkgk4CkypdVRgawjcJBfg6cvlxt74P7ywE5nr7Q0Uqm687b/ZJqApcXEPl1uuVX Ykwsu2DsSW/f28lsQQhsENv9Gz/kNh9tj9aeehW9NDS1h8nsvtk1/4LVULlgURCoyLO7i pnJGHsgZkJirVuN8DPuElNL6uGwS0ij9ooB0fefAXJXsvfhmzbkEPRTzd+/6Lw+nsLUeB f511sn/qY0bIl0zfNuUn+Ex1zg19C3BIUOOgn55a4v428wT8m/R/cRRmQskTpueTLcfV5 eECvw+tkCCKJiVpvn8bP51C6LkK+7Cpd51za+ZJa63UKpwDkta8S6eBUkPIFY9YMewg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796286502; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=8ttNh4C6buiRL6Cui8v0ulsIIiW/3WXNmKmcwHlH1Qk=; b=xlc7qtUmlUIDeDUUE56RESX+XG/J7qqzXF4wv62DQyQHhNc/2tapMBUTrKVv1pt4+Aj/ y3Gb1YY0dU7HVYHjPxYCbshycHaZScAw85DFRSpw4eLmdvt8lakgfALvNrMWRf4Fx4z9n eBuZOY4gxvczSvlXuZksfP8ScrQHGC3QRoVVDadeET5sQcKbG+leh4tqeCX8TIPEe2dsI 7H2wMN5eFNY9d3mGBr1kDwVjgh3Lth4XiECT7UrYMYC6vkbHNfPa9Eib6fBJr+qCvO/k1 O5tHd7vV7pOzE+oOmr5IyvLaZUrFVf+iotfrXAdmP2Kfo98LsMg50j6yo+4nn2opMzQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kJpOAz3Y; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4C7B9606A339; Thu, 3 Dec 2026 08:28:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kJpOAz3Y Received: by smtp.kernel.org (Postfix) with ESMTPSA id 37ADF1F0089F; Thu, 3 Dec 2026 08:28:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796286501; bh=8ttNh4C6buiRL6Cui8v0ulsIIiW/3WXNmKmcwHlH1Qk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=kJpOAz3YAUHNhMCV5guuY4T0vmnTiSIhTkYDNxdKlLW5KEAabvtmPq93Hv6Dd+4i6 8sW96p1TEX9A9Zf/H1bhhbqGEQhD2YaD3eTn27/DRCtd69GAzGsMH7LKDgbCGPhMUV K1OmGmwk6SeNDYYp5GdvCT4F4UYGmpx9ft2QxyDCCtl8UTtwYCWI5lCsck31HLM5AS 9T485hD3vj5EdT21qUfiIKfytVsfibZ+U2t4qNOtgsxpNpgg9jiODqc8baP0yrD7B9 h1n/s2ocgh71HtY9RwmS92U3H6RYb8Qt2FdjLEL7vx3mNAVVNOI8fSzp8yQUqs76J4 d++knR7EG+nWw== From: Lee Jones To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org, Albert Ou , Alexandre Ghiti , Anup Patel , Jassi Brar , linux-riscv@lists.infradead.org, Palmer Dabbelt , Paul Walmsley , Rahul Pathak Subject: [PATCH 03/10] mailbox: riscv-sbi-mpxy: Protect per-CPU shmem access with irqsave Date: Thu, 3 Dec 2026 08:28:03 +0000 Message-ID: X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- Cc: Albert Ou Cc: Alexandre Ghiti Cc: Anup Patel Cc: Jassi Brar Cc: linux-riscv@lists.infradead.org Cc: Palmer Dabbelt Cc: Paul Walmsley Cc: Rahul Pathak 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 +#include #include #include #include #include +#include #include #include #include @@ -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