Skip to content

Instantly share code, notes, and snippets.

@tuxoko
Created December 1, 2016 19:09
Show Gist options
  • Save tuxoko/0e508573c60a12f12bdf4b8215bac4fb to your computer and use it in GitHub Desktop.
Save tuxoko/0e508573c60a12f12bdf4b8215bac4fb to your computer and use it in GitHub Desktop.
diff --git a/include/sys/mutex.h b/include/sys/mutex.h
index 3192352..bccc61b 100644
--- a/include/sys/mutex.h
+++ b/include/sys/mutex.h
@@ -47,8 +47,24 @@ typedef struct {
#ifdef CONFIG_LOCKDEP
kmutex_type_t m_type;
#endif /* CONFIG_LOCKDEP */
+#ifdef CONFIG_DEBUG_KMEMLEAK
+ void *leak;
+#endif
} kmutex_t;
+#ifdef CONFIG_DEBUG_KMEMLEAK
+void kfree(const void *);
+void spl_mutex_leak_init(kmutex_t *, const char *);
+static inline void spl_mutex_leak_fini(kmutex_t *mp)
+{
+ kfree(mp->leak);
+ mp->leak = NULL;
+}
+#else
+#define spl_mutex_leak_init(mp, name) do { } while (0)
+#define spl_mutex_leak_fini(mp) do { } while (0)
+#endif
+
#define MUTEX(mp) (&((mp)->m_mutex))
static inline void
@@ -120,12 +136,14 @@ spl_mutex_lockdep_on_maybe(kmutex_t *mp) \
spin_lock_init(&(mp)->m_lock); \
spl_mutex_clear_owner(mp); \
spl_mutex_set_type(mp, type); \
+ spl_mutex_leak_init(mp, #mp); \
}
#undef mutex_destroy
#define mutex_destroy(mp) \
{ \
VERIFY3P(mutex_owner(mp), ==, NULL); \
+ spl_mutex_leak_fini(mp); \
}
#define mutex_tryenter(mp) \
diff --git a/module/spl/spl-mutex.c b/module/spl/spl-mutex.c
index a29d488..eb7efd1 100644
--- a/module/spl/spl-mutex.c
+++ b/module/spl/spl-mutex.c
@@ -24,6 +24,7 @@
* Solaris Porting Layer (SPL) Mutex Implementation.
\*****************************************************************************/
+#include <linux/slab.h>
#include <sys/mutex.h>
#ifdef DEBUG_SUBSYSTEM
@@ -32,5 +33,15 @@
#define DEBUG_SUBSYSTEM S_MUTEX
+#ifdef CONFIG_DEBUG_KMEMLEAK
+void spl_mutex_leak_init(kmutex_t *mp, const char *name)
+{
+ mp->leak = kmalloc(16, GFP_KERNEL);
+ /* No, we don't care about the '\0'. */
+ strncpy(mp->leak, name, 16);
+}
+EXPORT_SYMBOL(spl_mutex_leak_init);
+#endif
+
int spl_mutex_init(void) { return 0; }
void spl_mutex_fini(void) { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment