[PATCH RFC v6 05/21] dept: Apply Dept to rwlock

From: Byungchul Park
Date: Wed May 04 2022 - 04:52:27 EST


Makes Dept able to track dependencies by rwlock.

Signed-off-by: Byungchul Park <byungchul.park@xxxxxxx>
---
include/linux/lockdep.h | 25 ++++++++++++++++++++-----
include/linux/rwlock.h | 42 ++++++++++++++++++++++++++++++++++++++++++
include/linux/rwlock_api_smp.h | 8 ++++----
include/linux/rwlock_types.h | 1 +
4 files changed, 67 insertions(+), 9 deletions(-)

diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
index 99569acb..b59d8f3 100644
--- a/include/linux/lockdep.h
+++ b/include/linux/lockdep.h
@@ -588,16 +588,31 @@ static inline void print_irqtrace_events(struct task_struct *curr)
dept_spin_unlock(&(l)->dmap, i); \
} while (0)

-#define rwlock_acquire(l, s, t, i) lock_acquire_exclusive(l, s, t, NULL, i)
+#define rwlock_acquire(l, s, t, i) \
+do { \
+ lock_acquire_exclusive(l, s, t, NULL, i); \
+ dept_rwlock_wlock(&(l)->dmap, s, t, NULL, "write_unlock", i); \
+} while (0)
#define rwlock_acquire_read(l, s, t, i) \
do { \
- if (read_lock_is_recursive()) \
+ if (read_lock_is_recursive()) { \
lock_acquire_shared_recursive(l, s, t, NULL, i); \
- else \
+ dept_rwlock_rlock(&(l)->dmap, s, t, NULL, "read_unlock", i, 0);\
+ } else { \
lock_acquire_shared(l, s, t, NULL, i); \
+ dept_rwlock_rlock(&(l)->dmap, s, t, NULL, "read_unlock", i, 1);\
+ } \
+} while (0)
+#define rwlock_release(l, i) \
+do { \
+ lock_release(l, i); \
+ dept_rwlock_wunlock(&(l)->dmap, i); \
+} while (0)
+#define rwlock_release_read(l, i) \
+do { \
+ lock_release(l, i); \
+ dept_rwlock_runlock(&(l)->dmap, i); \
} while (0)
-
-#define rwlock_release(l, i) lock_release(l, i)

#define seqcount_acquire(l, s, t, i) lock_acquire_exclusive(l, s, t, NULL, i)
#define seqcount_acquire_read(l, s, t, i) lock_acquire_shared_recursive(l, s, t, NULL, i)
diff --git a/include/linux/rwlock.h b/include/linux/rwlock.h
index 8f416c5..bbab144 100644
--- a/include/linux/rwlock.h
+++ b/include/linux/rwlock.h
@@ -28,6 +28,48 @@
do { *(lock) = __RW_LOCK_UNLOCKED(lock); } while (0)
#endif

+#ifdef CONFIG_DEPT
+#define DEPT_EVT_RWLOCK_R 1UL
+#define DEPT_EVT_RWLOCK_W (1UL << 1)
+#define DEPT_EVT_RWLOCK_RW (DEPT_EVT_RWLOCK_R | DEPT_EVT_RWLOCK_W)
+
+#define dept_rwlock_wlock(m, ne, t, n, e_fn, ip) \
+do { \
+ if (t) { \
+ dept_ecxt_enter(m, DEPT_EVT_RWLOCK_W, ip, __func__, e_fn, ne);\
+ } else if (n) { \
+ dept_ecxt_enter_nokeep(m); \
+ } else { \
+ dept_wait(m, DEPT_EVT_RWLOCK_RW, ip, __func__, ne); \
+ dept_ecxt_enter(m, DEPT_EVT_RWLOCK_W, ip, __func__, e_fn, ne);\
+ } \
+} while (0)
+#define dept_rwlock_rlock(m, ne, t, n, e_fn, ip, q) \
+do { \
+ if (t) { \
+ dept_ecxt_enter(m, DEPT_EVT_RWLOCK_R, ip, __func__, e_fn, ne);\
+ } else if (n) { \
+ dept_ecxt_enter_nokeep(m); \
+ } else { \
+ dept_wait(m, (q) ? DEPT_EVT_RWLOCK_RW : DEPT_EVT_RWLOCK_W, ip, __func__, ne);\
+ dept_ecxt_enter(m, DEPT_EVT_RWLOCK_R, ip, __func__, e_fn, ne);\
+ } \
+} while (0)
+#define dept_rwlock_wunlock(m, ip) \
+do { \
+ dept_ecxt_exit(m, DEPT_EVT_RWLOCK_W, ip); \
+} while (0)
+#define dept_rwlock_runlock(m, ip) \
+do { \
+ dept_ecxt_exit(m, DEPT_EVT_RWLOCK_R, ip); \
+} while (0)
+#else
+#define dept_rwlock_wlock(m, ne, t, n, e_fn, ip) do { } while (0)
+#define dept_rwlock_rlock(m, ne, t, n, e_fn, ip, q) do { } while (0)
+#define dept_rwlock_wunlock(m, ip) do { } while (0)
+#define dept_rwlock_runlock(m, ip) do { } while (0)
+#endif
+
#ifdef CONFIG_DEBUG_SPINLOCK
extern void do_raw_read_lock(rwlock_t *lock) __acquires(lock);
extern int do_raw_read_trylock(rwlock_t *lock);
diff --git a/include/linux/rwlock_api_smp.h b/include/linux/rwlock_api_smp.h
index dceb0a5..a222cf1 100644
--- a/include/linux/rwlock_api_smp.h
+++ b/include/linux/rwlock_api_smp.h
@@ -228,7 +228,7 @@ static inline void __raw_write_unlock(rwlock_t *lock)

static inline void __raw_read_unlock(rwlock_t *lock)
{
- rwlock_release(&lock->dep_map, _RET_IP_);
+ rwlock_release_read(&lock->dep_map, _RET_IP_);
do_raw_read_unlock(lock);
preempt_enable();
}
@@ -236,7 +236,7 @@ static inline void __raw_read_unlock(rwlock_t *lock)
static inline void
__raw_read_unlock_irqrestore(rwlock_t *lock, unsigned long flags)
{
- rwlock_release(&lock->dep_map, _RET_IP_);
+ rwlock_release_read(&lock->dep_map, _RET_IP_);
do_raw_read_unlock(lock);
local_irq_restore(flags);
preempt_enable();
@@ -244,7 +244,7 @@ static inline void __raw_read_unlock(rwlock_t *lock)

static inline void __raw_read_unlock_irq(rwlock_t *lock)
{
- rwlock_release(&lock->dep_map, _RET_IP_);
+ rwlock_release_read(&lock->dep_map, _RET_IP_);
do_raw_read_unlock(lock);
local_irq_enable();
preempt_enable();
@@ -252,7 +252,7 @@ static inline void __raw_read_unlock_irq(rwlock_t *lock)

static inline void __raw_read_unlock_bh(rwlock_t *lock)
{
- rwlock_release(&lock->dep_map, _RET_IP_);
+ rwlock_release_read(&lock->dep_map, _RET_IP_);
do_raw_read_unlock(lock);
__local_bh_enable_ip(_RET_IP_, SOFTIRQ_LOCK_OFFSET);
}
diff --git a/include/linux/rwlock_types.h b/include/linux/rwlock_types.h
index 1948442..27b4b78 100644
--- a/include/linux/rwlock_types.h
+++ b/include/linux/rwlock_types.h
@@ -10,6 +10,7 @@
.dep_map = { \
.name = #lockname, \
.wait_type_inner = LD_WAIT_CONFIG, \
+ .dmap = DEPT_MAP_INITIALIZER(lockname) \
}
#else
# define RW_DEP_MAP_INIT(lockname)
--
1.9.1