Skip to content

Instantly share code, notes, and snippets.

@kerneltoast
Created February 4, 2021 22:00
Show Gist options
  • Save kerneltoast/286ff0993a316a97b5c2506c16a1f6bd to your computer and use it in GitHub Desktop.
Save kerneltoast/286ff0993a316a97b5c2506c16a1f6bd to your computer and use it in GitHub Desktop.
From 6033075d73fb69e54653e97a8b0efff7af400c4a Mon Sep 17 00:00:00 2001
From: Sultan Alsawaf <sultan@openresty.com>
Date: Thu, 4 Feb 2021 14:00:24 -0800
Subject: [PATCH] runtime: make probe locks rt-safe
---
runtime/linux/common_session_state.h | 2 +-
runtime/linux/probe_lock.h | 10 +++++-----
runtime/stp_helper_lock.h | 13 +++++++++++++
translate.cxx | 2 +-
4 files changed, 20 insertions(+), 7 deletions(-)
diff --git a/runtime/linux/common_session_state.h b/runtime/linux/common_session_state.h
index 444349080..83e5f9a59 100644
--- a/runtime/linux/common_session_state.h
+++ b/runtime/linux/common_session_state.h
@@ -50,7 +50,7 @@ static struct stp_globals stp_global;
#define global(name) (stp_global.name)
#define global_set(name, val) (global(name) = (val))
#define global_lock(name) (&global(name ## _lock))
-#define global_lock_init(name) rwlock_init(global_lock(name))
+#define global_lock_init(name) stp_rwlock_init(global_lock(name))
#ifdef STP_TIMING
#define global_skipped(name) (&global(name ## _lock_skip_count))
#define global_contended(name) (&global(name ## _lock_contention_count))
diff --git a/runtime/linux/probe_lock.h b/runtime/linux/probe_lock.h
index 847f0d457..de53e65ee 100644
--- a/runtime/linux/probe_lock.h
+++ b/runtime/linux/probe_lock.h
@@ -22,7 +22,7 @@ struct stp_probe_lock {
atomic_t *skipped;
atomic_t *contention;
#endif
- rwlock_t *lock;
+ stp_rwlock_t *lock;
unsigned write_p;
};
@@ -34,9 +34,9 @@ stp_unlock_probe(const struct stp_probe_lock *locks, unsigned num_locks)
if (num_locks == 0) return; /* defeat a gcc9 warning */
for (i = num_locks; i-- > 0;) {
if (locks[i].write_p)
- write_unlock(locks[i].lock);
+ stp_write_unlock(locks[i].lock);
else
- read_unlock(locks[i].lock);
+ stp_read_unlock(locks[i].lock);
}
}
@@ -47,7 +47,7 @@ stp_lock_probe(const struct stp_probe_lock *locks, unsigned num_locks)
unsigned i, retries = 0;
for (i = 0; i < num_locks; ++i) {
if (locks[i].write_p)
- while (!write_trylock(locks[i].lock)) {
+ while (!stp_write_trylock(locks[i].lock)) {
#if !defined(STAP_SUPPRESS_TIME_LIMITS_ENABLE)
if (++retries > MAXTRYLOCK)
goto skip;
@@ -58,7 +58,7 @@ stp_lock_probe(const struct stp_probe_lock *locks, unsigned num_locks)
udelay (TRYLOCKDELAY);
}
else
- while (!read_trylock(locks[i].lock)) {
+ while (!stp_read_trylock(locks[i].lock)) {
#if !defined(STAP_SUPPRESS_TIME_LIMITS_ENABLE)
if (++retries > MAXTRYLOCK)
goto skip;
diff --git a/runtime/stp_helper_lock.h b/runtime/stp_helper_lock.h
index 81e6a1644..d2961b74c 100644
--- a/runtime/stp_helper_lock.h
+++ b/runtime/stp_helper_lock.h
@@ -31,14 +31,20 @@ static inline void stp_spin_unlock(raw_spinlock_t *lock) { raw_spin_unlock(lock)
#define stp_spin_lock_irqsave(lock, flags) raw_spin_lock_irqsave(lock, flags)
#define stp_spin_unlock_irqrestore(lock, flags) raw_spin_unlock_irqrestore(lock, flags)
+#define stp_rwlock_t raw_spinlock_t
#define STP_DEFINE_RWLOCK(lock) DEFINE_RAW_SPINLOCK(lock)
+static inline void stp_rwlock_init(raw_spinlock_t *lock) { raw_spin_lock_init(lock); }
+
static inline void stp_read_lock(raw_spinlock_t *lock) { raw_spin_lock(lock); }
static inline void stp_read_unlock(raw_spinlock_t *lock) { raw_spin_unlock(lock); }
static inline void stp_write_lock(raw_spinlock_t *lock) { raw_spin_lock(lock); }
static inline void stp_write_unlock(raw_spinlock_t *lock) { raw_spin_unlock(lock); }
+static inline int stp_read_trylock(raw_spinlock_t *lock) { return raw_spin_trylock(lock); }
+static inline int stp_write_trylock(raw_spinlock_t *lock) { return raw_spin_trylock(lock); }
+
#define stp_read_lock_irqsave(lock, flags) raw_spin_lock_irqsave(lock, flags)
#define stp_read_unlock_irqrestore(lock, flags) raw_spin_unlock_irqrestore(lock, flags)
#define stp_write_lock_irqsave(lock, flags) raw_spin_lock_irqsave(lock, flags)
@@ -58,13 +64,20 @@ static inline void stp_spin_unlock(spinlock_t *lock) { spin_unlock(lock); }
#define stp_spin_lock_irqsave(lock, flags) spin_lock_irqsave(lock, flags)
#define stp_spin_unlock_irqrestore(lock, flags) spin_unlock_irqrestore(lock, flags)
+#define stp_rwlock_t rwlock_t
+
#define STP_DEFINE_RWLOCK(lock) DEFINE_RWLOCK(lock)
+static inline void stp_rwlock_init(rwlock_t *lock) { rwlock_init(lock); }
+
static inline void stp_read_lock(rwlock_t *lock) { read_lock(lock); }
static inline void stp_read_unlock(rwlock_t *lock) { read_unlock(lock); }
static inline void stp_write_lock(rwlock_t *lock) { write_lock(lock); }
static inline void stp_write_unlock(rwlock_t *lock) { write_unlock(lock); }
+static inline int stp_read_trylock(rwlock_t *lock) { return read_trylock(lock); }
+static inline int stp_write_trylock(rwlock_t *lock) { return write_trylock(lock); }
+
#define stp_read_lock_irqsave(lock, flags) read_lock_irqsave(lock, flags)
#define stp_read_unlock_irqrestore(lock, flags) read_unlock_irqrestore(lock, flags)
#define stp_write_lock_irqsave(lock, flags) write_lock_irqsave(lock, flags)
diff --git a/translate.cxx b/translate.cxx
index d7fe1e3b2..e7d4e9f99 100644
--- a/translate.cxx
+++ b/translate.cxx
@@ -1710,7 +1710,7 @@ c_unparser::emit_global (vardecl *v)
else
o->newline() << type << " " << vn << ";";
- o->newline() << "rwlock_t " << vn << "_lock;";
+ o->newline() << "stp_rwlock_t " << vn << "_lock;";
o->newline() << "#ifdef STP_TIMING";
o->newline() << "atomic_t " << vn << "_lock_skip_count;";
o->newline() << "atomic_t " << vn << "_lock_contention_count;";
--
2.30.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment