Skip to contentSkip to conversation

lib: sbi: Add read-write locks

PATCH New Nickolai Zeldovich 1 participant 1 message Nov 16 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: sbi: Add read-write locks

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.

---

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(+)
include/sbi/riscv_locks.h +36 -0
index a0a34c04..bb1fea48 100644
@@ -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
include/sbi/sbi_visibility.h +4 -0
index 993561ef..503249f4 100644
@@ -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
lib/sbi/riscv_locks.c +134 -0

137 lines not on this page · raw message

lib/sbi/tests/riscv_locks_test.c +46 -0

56 lines not on this page · raw message

base-commit: ed70256dd22c09962d146e433834239825ef0616

Signature · 7 lines
2.53.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi