[PATCH v2] lib: sbi_ipi: Protect the IPI device list with a read-write lock
Samuel Holland
samuel.holland at sifive.com
Mon Nov 16 07:06:03 PST 2026
On 2026-11-16 7:56 AM, Nickolai Zeldovich wrote:
> 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.
This is a more fundamental issue, not just with the IPI device list. Effectively
none of the cold boot code is written with synchronization in mind, and
functions like sbi_hsm_hart_wait() and sbi_ipi_raw_clear() can call into
arbitrary platform code (think timer or irqchip drivers), so any driver
subsystem could have a race here. I think the only robust solution is to move
wake_coldboot_harts() to the very end of the coldboot process, so secondary
harts never see a partially-initialized state.
The tiny bit of parallelism gained by releasing the secondary harts early is not
worth complicating runtime code paths with locking. And on platforms that leave
secondary harts in reset until the HSM start operation, nothing at all is
gained, because the hart necessarily comes out of reset long after cold boot
completes.
If the slight increase in boot time on platforms that release all harts in
parallel is a concern, we can refactor the code to have the boot hart 1) run the
cold boot process _excluding_ the local warm boot operations, 2) release the
secondary harts, and then 3) call init_warm_startup(). This refactoring
direction is additionally useful because it cleanly separates out code that is
used for cold boot only, and those bytes can be reclaimed as additional
stack/heap memory (cf. Linux __init).
Regards,
Samuel
> 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
More information about the opensbi
mailing list