[PATCH v2] lib: sbi_ipi: Protect the IPI device list with a read-write lock
Nickolai Zeldovich
nickolai at csail.mit.edu
Mon Nov 16 05:56:22 PST 2026
The cold boot hart wakes the other harts before it registers the IPI
devices, and each woken hart walks ipi_dev_node_list in
sbi_ipi_raw_clear(true) while sbi_ipi_add_device() may be inserting
into it. Nothing orders the walk against the insertion, so a hart can
follow a new node before its contents are visible and call through a
NULL entry->dev.
Take the write lock around the insertion and the read lock around the
all-devices walks in sbi_ipi_raw_clear() and sbi_ipi_raw_send().
Fixes: ee92afa63892 ("lib: sbi: Introduce IPI device rating")
Signed-off-by: Nickolai Zeldovich <nickolai at csail.mit.edu>
---
Notes:
Changes in v2:
- Protect the list with a read-write lock instead of smp_wmb()/smp_rmb()
and an open-coded list insertion (Anup).
- New subject (v1: "lib: sbi: Order the publication of IPI device
nodes"); added the Fixes tag.
Depends on "lib: sbi: Add read-write locks", sent separately.
v1: https://lore.kernel.org/r/20261006193630.735611-1-nickolai@csail.mit.edu
Tested on QEMU virt with 8 harts, with ACLINT and with AIA (IMSIC)
IPIs: Linux brings up all harts.
This patch was written with the assistance of an AI agent.
lib/sbi/sbi_ipi.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/lib/sbi/sbi_ipi.c b/lib/sbi/sbi_ipi.c
index b04a5877..756baab0 100644
--- a/lib/sbi/sbi_ipi.c
+++ b/lib/sbi/sbi_ipi.c
@@ -11,6 +11,7 @@
#include <sbi/riscv_asm.h>
#include <sbi/riscv_atomic.h>
#include <sbi/riscv_barrier.h>
+#include <sbi/riscv_locks.h>
#include <sbi/sbi_bitops.h>
#include <sbi/sbi_domain.h>
#include <sbi/sbi_error.h>
@@ -41,7 +42,9 @@ struct sbi_ipi_device_node {
static unsigned long ipi_data_off;
static const struct sbi_ipi_device *ipi_dev = NULL;
-static SBI_LIST_HEAD(ipi_dev_node_list);
+static rwlock_t ipi_dev_node_lock = RW_LOCK_INITIALIZER;
+static struct sbi_dlist ipi_dev_node_list GUARDED_BY(&ipi_dev_node_lock) =
+ SBI_LIST_HEAD_INIT(ipi_dev_node_list);
static const struct sbi_ipi_event_ops *ipi_ops_array[SBI_IPI_EVENT_MAX];
static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartindex,
@@ -297,10 +300,12 @@ int sbi_ipi_raw_send(u32 hartindex, bool all_devices)
wmb();
if (all_devices) {
+ read_lock(&ipi_dev_node_lock);
sbi_list_for_each_entry(entry, &ipi_dev_node_list, head) {
if (entry->dev->ipi_send)
entry->dev->ipi_send(hartindex);
}
+ read_unlock(&ipi_dev_node_lock);
} else {
ipi_dev->ipi_send(hartindex);
}
@@ -313,10 +318,12 @@ void sbi_ipi_raw_clear(bool all_devices)
struct sbi_ipi_device_node *entry;
if (all_devices) {
+ read_lock(&ipi_dev_node_lock);
sbi_list_for_each_entry(entry, &ipi_dev_node_list, head) {
if (entry->dev->ipi_clear)
entry->dev->ipi_clear();
}
+ read_unlock(&ipi_dev_node_lock);
} else {
if (ipi_dev && ipi_dev->ipi_clear)
ipi_dev->ipi_clear();
@@ -351,7 +358,9 @@ void sbi_ipi_add_device(const struct sbi_ipi_device *dev)
return;
SBI_INIT_LIST_HEAD(&entry->head);
entry->dev = dev;
+ write_lock(&ipi_dev_node_lock);
sbi_list_add_tail(&entry->head, &ipi_dev_node_list);
+ write_unlock(&ipi_dev_node_lock);
if (!ipi_dev || ipi_dev->rating < dev->rating)
ipi_dev = dev;
base-commit: ed70256dd22c09962d146e433834239825ef0616
prerequisite-patch-id: 3f219b25b93a055133df65f70c1671f3df0514b7
--
2.53.0
More information about the opensbi
mailing list