[PATCH] lib: sbi: Add read-write locks
Nickolai Zeldovich
nickolai at csail.mit.edu
Mon Nov 16 04:41:41 PST 2026
Add rwlock_t with read_lock()/read_unlock(), write_lock()/write_unlock()
and trylock variants. Readers share the lock and a writer has it alone.
Writers have priority, so a stream of readers cannot hold one off.
The lock word holds a writer bit and a reader count. A writer sets the
bit, which turns new readers away, and waits for the readers already
in to leave. A reader does not touch the count while the bit is set;
otherwise it adds itself and backs out if a writer set the bit in the
meantime. This needs only fetch-and-add/or/and, which map to AMOs with
A or Zaamo and to LR/SC loops with Zalrsc, as in spin_lock().
Add SBIUnit cases for both modes and for a reader meeting a waiting
writer.
Suggested-by: Anup Patel <anup at brainfault.org>
Signed-off-by: Nickolai Zeldovich <nickolai at csail.mit.edu>
---
Notes:
Tested on QEMU virt: the SBIUnit locks suite passes; an 8-hart stress
run at boot (mixed readers and writers) shows no exclusion violations;
with 7 harts looping on read_trylock(), a writer gets through, where a
reader-preferring version starves it. Built for rv32 and rv64 with
clang and GCC; the lock code also builds for Zaamo-only and
Zalrsc-only ISA strings. The fence and .aqrl ordering was checked with
litmus tests against the RISC-V memory model.
This patch was written with the assistance of an AI agent.
include/sbi/riscv_locks.h | 36 +++++++++
include/sbi/sbi_visibility.h | 4 +
lib/sbi/riscv_locks.c | 134 +++++++++++++++++++++++++++++++
lib/sbi/tests/riscv_locks_test.c | 46 +++++++++++
4 files changed, 220 insertions(+)
diff --git a/include/sbi/riscv_locks.h b/include/sbi/riscv_locks.h
index a0a34c04..bb1fea48 100644
--- a/include/sbi/riscv_locks.h
+++ b/include/sbi/riscv_locks.h
@@ -42,4 +42,40 @@ void spin_lock(spinlock_t *lock) ACQUIRE(*lock) MUST_NOT_HOLD(*lock);
void spin_unlock(spinlock_t *lock) RELEASE(*lock);
+/*
+ * Readers share the lock; a writer has it alone. Writers have priority:
+ * once a writer waits for the lock, new readers wait until it has
+ * released it. So a hart must not take a read lock it already holds.
+ */
+typedef struct {
+ /* RWLOCK_WRITER while a writer holds or waits for it, plus readers */
+ u32 val;
+} CAPABILITY("rwlock") rwlock_t;
+
+#define RWLOCK_WRITER (1u << 31)
+
+#define __RW_LOCK_UNLOCKED \
+ (rwlock_t) { 0 }
+
+#define RW_LOCK_INIT(x) \
+ x = __RW_LOCK_UNLOCKED
+
+#define RW_LOCK_INITIALIZER \
+ __RW_LOCK_UNLOCKED
+
+#define DEFINE_RW_LOCK(x) \
+ rwlock_t RW_LOCK_INIT(x)
+
+bool read_trylock(rwlock_t *lock) TRY_ACQUIRE_SHARED(true, *lock);
+
+void read_lock(rwlock_t *lock) ACQUIRE_SHARED(*lock) MUST_NOT_HOLD(*lock);
+
+void read_unlock(rwlock_t *lock) RELEASE_SHARED(*lock);
+
+bool write_trylock(rwlock_t *lock) TRY_ACQUIRE(true, *lock);
+
+void write_lock(rwlock_t *lock) ACQUIRE(*lock) MUST_NOT_HOLD(*lock);
+
+void write_unlock(rwlock_t *lock) RELEASE(*lock);
+
#endif
diff --git a/include/sbi/sbi_visibility.h b/include/sbi/sbi_visibility.h
index 993561ef..503249f4 100644
--- a/include/sbi/sbi_visibility.h
+++ b/include/sbi/sbi_visibility.h
@@ -27,9 +27,13 @@
#define PT_GUARDED_BY(x) TSA_ATTR(pt_guarded_by(x))
#define ACQUIRE(...) TSA_ATTR(acquire_capability(__VA_ARGS__))
#define RELEASE(...) TSA_ATTR(release_capability(__VA_ARGS__))
+#define ACQUIRE_SHARED(...) TSA_ATTR(acquire_shared_capability(__VA_ARGS__))
+#define RELEASE_SHARED(...) TSA_ATTR(release_shared_capability(__VA_ARGS__))
#define MUST_HOLD(...) TSA_ATTR(requires_capability(__VA_ARGS__))
#define MUST_NOT_HOLD(...) TSA_ATTR(locks_excluded(__VA_ARGS__))
#define TRY_ACQUIRE(b, ...) TSA_ATTR(try_acquire_capability(b, __VA_ARGS__))
+#define TRY_ACQUIRE_SHARED(b, ...) \
+ TSA_ATTR(try_acquire_shared_capability(b, __VA_ARGS__))
#define NO_THREAD_SAFETY_ANALYSIS TSA_ATTR(no_thread_safety_analysis)
#endif
diff --git a/lib/sbi/riscv_locks.c b/lib/sbi/riscv_locks.c
index fa624a94..03040747 100644
--- a/lib/sbi/riscv_locks.c
+++ b/lib/sbi/riscv_locks.c
@@ -88,3 +88,137 @@ void spin_unlock(spinlock_t *lock) NO_THREAD_SAFETY_ANALYSIS
{
__smp_store_release(&lock->owner, lock->owner + 1);
}
+
+#if !defined(__riscv_atomic) && !defined(__riscv_zaamo) && !defined(__riscv_zalrsc)
+#error "need A or Zaamo or Zalrsc"
+#endif
+
+/* Atomic fetch-and-add/or/and on the lock word, returning the old value */
+static u32 rwlock_fetch_add(rwlock_t *lock, u32 v)
+{
+ u32 old;
+#if defined(__riscv_atomic) || defined(__riscv_zaamo)
+ __asm__ __volatile__(" amoadd.w.aqrl %0, %2, %1\n"
+ : "=r"(old), "+A"(lock->val)
+ : "r"(v)
+ : "memory");
+#else
+ u32 tmp;
+
+ __asm__ __volatile__("1: lr.w.aqrl %0, %2\n"
+ " add %1, %0, %3\n"
+ " sc.w.aqrl %1, %1, %2\n"
+ " bnez %1, 1b\n"
+ : "=&r"(old), "=&r"(tmp), "+A"(lock->val)
+ : "r"(v)
+ : "memory");
+#endif
+ return old;
+}
+
+static u32 rwlock_fetch_or(rwlock_t *lock, u32 v)
+{
+ u32 old;
+#if defined(__riscv_atomic) || defined(__riscv_zaamo)
+ __asm__ __volatile__(" amoor.w.aqrl %0, %2, %1\n"
+ : "=r"(old), "+A"(lock->val)
+ : "r"(v)
+ : "memory");
+#else
+ u32 tmp;
+
+ __asm__ __volatile__("1: lr.w.aqrl %0, %2\n"
+ " or %1, %0, %3\n"
+ " sc.w.aqrl %1, %1, %2\n"
+ " bnez %1, 1b\n"
+ : "=&r"(old), "=&r"(tmp), "+A"(lock->val)
+ : "r"(v)
+ : "memory");
+#endif
+ return old;
+}
+
+static u32 rwlock_fetch_and(rwlock_t *lock, u32 v)
+{
+ u32 old;
+#if defined(__riscv_atomic) || defined(__riscv_zaamo)
+ __asm__ __volatile__(" amoand.w.aqrl %0, %2, %1\n"
+ : "=r"(old), "+A"(lock->val)
+ : "r"(v)
+ : "memory");
+#else
+ u32 tmp;
+
+ __asm__ __volatile__("1: lr.w.aqrl %0, %2\n"
+ " and %1, %0, %3\n"
+ " sc.w.aqrl %1, %1, %2\n"
+ " bnez %1, 1b\n"
+ : "=&r"(old), "=&r"(tmp), "+A"(lock->val)
+ : "r"(v)
+ : "memory");
+#endif
+ return old;
+}
+
+static u32 rwlock_read(rwlock_t *lock)
+{
+ return *(volatile u32 *)&lock->val;
+}
+
+bool read_trylock(rwlock_t *lock) NO_THREAD_SAFETY_ANALYSIS
+{
+ /* Leave the count alone while a writer holds or waits for the lock */
+ if (rwlock_read(lock) & RWLOCK_WRITER)
+ return false;
+ if (!(rwlock_fetch_add(lock, 1) & RWLOCK_WRITER))
+ return true;
+
+ /* A writer set its bit after the check above */
+ rwlock_fetch_add(lock, -1);
+ return false;
+}
+
+void read_lock(rwlock_t *lock) NO_THREAD_SAFETY_ANALYSIS
+{
+ while (!read_trylock(lock))
+ ;
+}
+
+void read_unlock(rwlock_t *lock) NO_THREAD_SAFETY_ANALYSIS
+{
+ rwlock_fetch_add(lock, -1);
+}
+
+bool write_trylock(rwlock_t *lock) NO_THREAD_SAFETY_ANALYSIS
+{
+ u32 old = rwlock_fetch_or(lock, RWLOCK_WRITER);
+
+ if (old & RWLOCK_WRITER)
+ return false;
+ if (!old)
+ return true;
+
+ /* Readers hold the lock and this must not wait for them */
+ rwlock_fetch_and(lock, ~RWLOCK_WRITER);
+ return false;
+}
+
+void write_lock(rwlock_t *lock) NO_THREAD_SAFETY_ANALYSIS
+{
+ /* Claim the writer bit, which keeps new readers out */
+ while (rwlock_fetch_or(lock, RWLOCK_WRITER) & RWLOCK_WRITER) {
+ while (rwlock_read(lock) & RWLOCK_WRITER)
+ ;
+ }
+
+ /* Wait for the readers that got in before the bit was set */
+ while (rwlock_read(lock) & ~RWLOCK_WRITER)
+ ;
+ RISCV_FENCE(r, rw);
+}
+
+void write_unlock(rwlock_t *lock) NO_THREAD_SAFETY_ANALYSIS
+{
+ /* Not a plain store: a failing reader may have the count raised */
+ rwlock_fetch_and(lock, ~RWLOCK_WRITER);
+}
diff --git a/lib/sbi/tests/riscv_locks_test.c b/lib/sbi/tests/riscv_locks_test.c
index 2cbba3da..3f8bfde7 100644
--- a/lib/sbi/tests/riscv_locks_test.c
+++ b/lib/sbi/tests/riscv_locks_test.c
@@ -34,10 +34,56 @@ static void spin_trylock_success(struct sbiunit_test_case *test)
spin_unlock(&test_lock);
}
+static rwlock_t test_rwlock = RW_LOCK_INITIALIZER;
+
+static void read_lock_shared(struct sbiunit_test_case *test)
+ NO_THREAD_SAFETY_ANALYSIS
+{
+ read_lock(&test_rwlock);
+ SBIUNIT_EXPECT(test, read_trylock(&test_rwlock));
+ SBIUNIT_EXPECT(test, !write_trylock(&test_rwlock));
+ read_unlock(&test_rwlock);
+ SBIUNIT_EXPECT(test, !write_trylock(&test_rwlock));
+ read_unlock(&test_rwlock);
+
+ SBIUNIT_ASSERT(test, write_trylock(&test_rwlock));
+ write_unlock(&test_rwlock);
+}
+
+static void write_lock_exclusive(struct sbiunit_test_case *test)
+ NO_THREAD_SAFETY_ANALYSIS
+{
+ write_lock(&test_rwlock);
+ SBIUNIT_EXPECT(test, !read_trylock(&test_rwlock));
+ SBIUNIT_EXPECT(test, !write_trylock(&test_rwlock));
+ write_unlock(&test_rwlock);
+
+ SBIUNIT_ASSERT(test, read_trylock(&test_rwlock));
+ read_unlock(&test_rwlock);
+}
+
+static void read_trylock_writer_waiting(struct sbiunit_test_case *test)
+ NO_THREAD_SAFETY_ANALYSIS
+{
+ read_lock(&test_rwlock);
+
+ /* What a writer waiting for this reader leaves in the lock word */
+ test_rwlock.val |= RWLOCK_WRITER;
+ SBIUNIT_EXPECT(test, !read_trylock(&test_rwlock));
+ SBIUNIT_EXPECT_EQ(test, test_rwlock.val, RWLOCK_WRITER | 1);
+ test_rwlock.val &= ~RWLOCK_WRITER;
+
+ read_unlock(&test_rwlock);
+ SBIUNIT_ASSERT_EQ(test, test_rwlock.val, 0);
+}
+
static struct sbiunit_test_case locks_test_cases[] = {
SBIUNIT_TEST_CASE(spin_lock_test),
SBIUNIT_TEST_CASE(spin_trylock_fail),
SBIUNIT_TEST_CASE(spin_trylock_success),
+ SBIUNIT_TEST_CASE(read_lock_shared),
+ SBIUNIT_TEST_CASE(write_lock_exclusive),
+ SBIUNIT_TEST_CASE(read_trylock_writer_waiting),
SBIUNIT_END_CASE,
};
base-commit: ed70256dd22c09962d146e433834239825ef0616
--
2.53.0
More information about the opensbi
mailing list