Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=205.220.180.131 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796382950; b=0luFIUPAg4+knqdJW3g+C7eKKkOZ185wdVH4W/S6c7CVPqAu7k/qsz1RQ6pzGgUuxZKD qid3be5EU0NDZBEqrLpZKnukMvHnD1TlaJecybysyg57222VyrXkC4k9DFGTmGamdJt6F W3w9ZJJRkF5cstav6RxjGPMvWiIm74O8VBz1sV5dLR+EF+EyeuwdJYBeKpC8N5bjHIuZe cd7A/LU5+dFDS3+L043nyopzJ4fXbwcxhywnGiaQS+DT+fZpdukuM68BJFDTQiMX/AS+N KBl901hBe8jlfhW2Fz1ODaSnrLjsGEX8WeoqBtMgWXcpAtBkHAcgMfDL1UJxnIAvmDw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382950; h=DMARC-Filter:Received:DKIM-Signature:Received:Received:Received: Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding: X-Proofpoint-Reinject:X-QCInternal:X-QCInternal: X-Proofpoint-Spam-Details-Enc:X-Proofpoint-ORIG-GUID: X-Proofpoint-GUID:X-Authority-Analysis:X-Proofpoint-Spam-Info: X-Proofpoint-Virus-Version:X-Proofpoint-Spam-Details; bh=KdtlId+IeeClWo/1rSVD0DxctWf4Q/yT435J5pGGLuo=; b=exgZ0oKtIUKcz4sTAPG/8XiT4eARQrQwXV0l7oMlINEnij/DpiDR7nCunh+ssFE0fpX8 tNRDJr1brxDXg0GWNCb/Po0lbNp2V3U0lSKxo22eKTTl8VgyT8kbE/BiPIJH3CcQ8L05E avJKjGEq/T5kzIeDckQM/yTShUuj8yfvRGQAIdO/7S65pbcllmUQDWW3ip1y+Jgh8HqmP 2odGZ4utSMc+BArF0QLz+2w8yGXOTI3g8oVhzQM8aZ7liNeZJ5RClRBgVPP6vIzCPMpCg vG19JerygCY9UkPJBaTM4IUgHH3sUmP7Vr2Rh8ZfNK3pZCu6igGfq5bSgKxzwCh7Irg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=qualcomm.com; dkim=pass header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=L4qVFgIZ; arc=none smtp.remote-ip=205.220.180.131 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A2B30606A333; Fri, 4 Dec 2026 11:15:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A2B30606A333 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=L4qVFgIZ Received: from pps.filterd (m0279869.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6B45Ge462769487; Fri, 4 Dec 2026 11:15:20 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=qcppdkim1; bh=KdtlId+IeeC lWo/1rSVD0DxctWf4Q/yT435J5pGGLuo=; b=L4qVFgIZzlZHXhBP/OfzAPO5oXi Dt56iejn0N0XzHLDRbV6TWYdjIP3oq/TMekpA58Dqpukg4vlZsBDgRABRrUDPLF3 4hZFJ/51sbAidoFcgvU/xbk/HS4e+f90n/+TxMpaPkhk4X6uoCK3sMw8CLMqDtaf lmnvAE6yxu6dXivJX1oIsIwQyH2FxBMQLnSdgBESH3K6WvXL51DMvvzV4868U4+j 5Xx1bHaBw1IZERRZjNFPRuO8h4QJ/ORgay8UkxxWeWQUQdIDUF7BZZFAQwg7ro3Z V1inJhBupTd5hqYyy4b9xs6aMW/bzu2V5NQlT6TA0lwZvxb6mj/RMIjVz8A== Received: from apblrppmta01.qualcomm.com (blr-bdr-fw-01_GlobalNAT_AllZones-Outside.qualcomm.com [103.229.18.19]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4jbq7s12qq-1 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 04 Dec 2026 11:15:19 +0000 (GMT) Received: from pps.filterd (apblrppmta01.qualcomm.com [127.0.0.1]) by APBLRPPMTA01.qualcomm.com (8.18.1.11/8.18.1.11) with ESMTP id 6B4BFGKR1939404; Fri, 4 Dec 2026 11:15:16 GMT Received: from hu-devc-blr-u24-a.qualcomm.com (hu-anuppate-blr.qualcomm.com [10.131.36.165]) by APBLRPPMTA01.qualcomm.com (PPS) with ESMTPS id 6B4BFGHw1939383 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 04 Dec 2026 11:15:16 +0000 (GMT) Received: by hu-devc-blr-u24-a.qualcomm.com (Postfix, from userid 486687) id 470D724964; Fri, 4 Dec 2026 16:45:16 +0530 (+0530) From: Anup Patel To: Palmer Dabbelt , Paul Walmsley , Rahul Pathak , Stephen Boyd , Brian Masney , Jerome Brunet , Thomas Gleixner , Radu Rendec , Jassi Brar Cc: Sunil V L , Himanshu Chauhan , Atish Patra , Anup Patel , Amirreza Zarrabi , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, Anup Patel , Rahul Pathak Subject: [PATCH v3 2/5] RISC-V: Prevent interrupt when accessing MPXY shared memory Date: Fri, 4 Dec 2026 16:45:01 +0530 Message-ID: <20261204111504.3362889-3-anup.patel@oss.qualcomm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261204111504.3362889-1-anup.patel@oss.qualcomm.com> References: <20261204111504.3362889-1-anup.patel@oss.qualcomm.com> X-Mailing-List: linux-clk@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Proofpoint-Reinject: loops=2 maxloops=12 X-QCInternal: smtphost X-QCInternal: smtphost X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMjA0MDA0NCBTYWx0ZWRfXzpoTAvOIarZM /fTTryBUuH/zmnyF7BdrmLaLTSvaFIMOVEw02l6xU7d+d76aHLGGoBTOJyncGhrkKt+u8kmrcJX rTaYlJUrqCLyXsYJuLnXN1fOiR5sY13NWaz+Xm5++JoEXoONHMxTdq74KXhk4F37vFuHHkLNPtK lr69CeeN+52I3D84kouMZ/U2kbDhZrb5TtUVgE1k4QmOtLvyV6VuOuwR8JKu241/jlV8+bcJyCz ZUEOJUVtcGbtegwN4uJG06aKhhkZ26ZqhNT6/OCyDswGBISkkm6UgJg9Q+6vqvGAWitrsd6wJHl OvR+2Gly6lXoURmXt8m1sNilfui2wBWrfllrXQs5vlOp3ios4DmNCW7CWIxJx7LG9kK4PwFBP6U 6ReT7NZaHOXkfm44KQxawjldIf4giRmM1l26rUhPnSG4BiccMki6Kl/wd/XRAUzrT+uPJyqC2jM daGKIiWD6RxS54v2zUA== X-Proofpoint-ORIG-GUID: dSGrLjoZqIt6r_0z4oOzP5zjU05iu4Gg X-Proofpoint-GUID: dSGrLjoZqIt6r_0z4oOzP5zjU05iu4Gg X-Authority-Analysis: v=2.4 cv=AOKPfg2Y c=1 sm=1 tr=0 ts=6b12a0c8 cx=c_pps a=Ou0eQOY4+eZoSc0qltEV5Q==:117 a=Ou0eQOY4+eZoSc0qltEV5Q==:17 a=pUM-KoujPXoA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=_glEPmIy2e8OvE2BGh3C:22 a=pGLkceISAAAA:8 a=EUspDBNiAAAA:8 a=Z9CWrU2r9MEVleDcQ90A:9 X-Proofpoint-Spam-Info: AW1haW4tMjYxMjA0MDA0NCBTYWx0ZWRfX6IbpdbecHwZZ FMGYOtwphwBtLtcyH6zdXB+qD+dz/q/fyi+2v+AucR6hAExweGijX6UJ2LAijU6jGBbtaikOWEz IFEz9sl5HFKnOq+cfJuAWKY2Z9Zj0ug= X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.138,FMLib:17.12.100.49 definitions=2026-12-04_01,2026-12-03_04,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 impostorscore=0 malwarescore=0 phishscore=0 bulkscore=0 suspectscore=0 priorityscore=1501 spamscore=0 adultscore=0 lowpriorityscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2611040001 definitions=main-2612040044 Currently, get_cpu()/put_cpu() is used to prevent getting preempted when accessing shared memory but an external interrupt can still be taken which may invoke RPMI system IRQ driver potentially corrupting MPXY shared memory. To prevent the above, replace get_cpu()/put_cpu() with irq_save/restore which ensures external interrupt can't corrupt MPXY shared memory. Co-developed-by: Jassi Brar Signed-off-by: Jassi Brar Signed-off-by: Anup Patel Reviewed-by: Rahul Pathak --- arch/riscv/kernel/sbi_mpxy.c | 114 +++++++++++++++++------------------ 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/arch/riscv/kernel/sbi_mpxy.c b/arch/riscv/kernel/sbi_mpxy.c index c1c5058d3252..4b25e5dc7fd6 100644 --- a/arch/riscv/kernel/sbi_mpxy.c +++ b/arch/riscv/kernel/sbi_mpxy.c @@ -6,8 +6,10 @@ */ #define pr_fmt(fmt) "riscv: " fmt +#include #include #include +#include #include #include #include @@ -50,113 +52,116 @@ EXPORT_SYMBOL_GPL(sbi_mpxy_shmem_size); int sbi_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(); + /* + * The shared memory is per-CPU. Disable IRQs so that a local IRQ + * cannot re-enter and clobber the buffer while the SBI call uses it. + * This also pins the current CPU for this_cpu_ptr(). + */ + guard(irqsave)(); + mpxy = this_cpu_ptr(&mpxy_local); + if (!mpxy->shmem_active) + return -ENODEV; + sdata = mpxy->shmem; /* 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); 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; } EXPORT_SYMBOL_GPL(sbi_mpxy_get_channel_count); int sbi_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); - count = returned < (channel_count - start_index) ? - returned : (channel_count - start_index); + count = min(returned, channel_count - start_index); memcpy_from_le32(&channel_ids[start_index], sdata->channel_array, count); start_index += count; } while (remaining && start_index < channel_count); -err_put_cpu: - put_cpu(); - return sbi_err_map_linux_errno(sret.error); + return 0; } EXPORT_SYMBOL_GPL(sbi_mpxy_get_channel_ids); int sbi_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; } EXPORT_SYMBOL_GPL(sbi_mpxy_read_attrs); int sbi_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); } EXPORT_SYMBOL_GPL(sbi_mpxy_write_attrs); @@ -166,18 +171,19 @@ int sbi_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; if (mpxy_shmem_size < tx_len) return -ENOSPC; - 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) @@ -187,17 +193,14 @@ int sbi_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); } EXPORT_SYMBOL_GPL(sbi_mpxy_send_message_with_resp); @@ -205,17 +208,18 @@ EXPORT_SYMBOL_GPL(sbi_mpxy_send_message_with_resp); int sbi_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; if (mpxy_shmem_size < tx_len) return -ENOSPC; - 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) @@ -224,7 +228,6 @@ int sbi_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); } EXPORT_SYMBOL_GPL(sbi_mpxy_send_message_without_resp); @@ -233,32 +236,29 @@ int sbi_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; } EXPORT_SYMBOL_GPL(sbi_mpxy_get_notifications); -- 2.43.0