linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Namhyung Kim <namhyung@kernel.org>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@kernel.org>, Will Deacon <will@kernel.org>,
	Waiman Long <longman@redhat.com>,
	Boqun Feng <boqun.feng@gmail.com>
Cc: LKML <linux-kernel@vger.kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Steven Rostedt <rostedt@goodmis.org>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Byungchul Park <byungchul.park@lge.com>,
	"Paul E. McKenney" <paulmck@kernel.org>,
	Arnd Bergmann <arnd@arndb.de>,
	linux-arch@vger.kernel.org, bpf@vger.kernel.org,
	Radoslaw Burny <rburny@google.com>
Subject: [PATCH 1/4] locking: Add lock contention tracepoints
Date: Mon, 28 Feb 2022 17:04:09 -0800	[thread overview]
Message-ID: <20220301010412.431299-2-namhyung@kernel.org> (raw)
In-Reply-To: <20220301010412.431299-1-namhyung@kernel.org>

This adds two new lock contention tracepoints like below:

 * lock:contention_begin
 * lock:contention_end

The lock:contention_begin takes a flags argument to classify locks.  I
found it useful to pass a task state it goes to and it can tell if the
given lock is busy-waiting (spinlock) or sleeping (mutex or semaphore).
Also it has info whether it's a reader-writer lock, real-time, and
per-cpu.

Move tracepoint definitions into a separate file so that we can use
some of them without lockdep.  Also lock_trace.h header was added to
provide access to the tracepoints in the header file (like spinlock.h)
which cannot include the tracepoint definition directly.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 include/linux/lock_trace.h  | 31 +++++++++++++++++++++++++++
 include/trace/events/lock.h | 42 ++++++++++++++++++++++++++++++++++++-
 kernel/locking/Makefile     |  2 +-
 kernel/locking/lockdep.c    |  1 -
 kernel/locking/tracepoint.c | 21 +++++++++++++++++++
 5 files changed, 94 insertions(+), 3 deletions(-)
 create mode 100644 include/linux/lock_trace.h
 create mode 100644 kernel/locking/tracepoint.c

diff --git a/include/linux/lock_trace.h b/include/linux/lock_trace.h
new file mode 100644
index 000000000000..d84bcc9570a4
--- /dev/null
+++ b/include/linux/lock_trace.h
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef __LINUX_LOCK_TRACE_H
+#define __LINUX_LOCK_TRACE_H
+
+#include <linux/tracepoint-defs.h>
+
+DECLARE_TRACEPOINT(contention_begin);
+DECLARE_TRACEPOINT(contention_end);
+
+#define LCB_F_READ	(1U << 31)
+#define LCB_F_WRITE	(1U << 30)
+#define LCB_F_RT	(1U << 29)
+#define LCB_F_PERCPU	(1U << 28)
+
+extern void lock_contention_begin(void *lock, unsigned long ip,
+				  unsigned int flags);
+extern void lock_contention_end(void *lock);
+
+#define LOCK_CONTENTION_BEGIN(_lock, _flags)				\
+	do {								\
+		if (tracepoint_enabled(contention_begin))		\
+			lock_contention_begin(_lock, _RET_IP_, _flags);	\
+	} while (0)
+
+#define LOCK_CONTENTION_END(_lock)					\
+	do {								\
+		if (tracepoint_enabled(contention_end))			\
+			lock_contention_end(_lock);			\
+	} while (0)
+
+#endif /* __LINUX_LOCK_TRACE_H */
diff --git a/include/trace/events/lock.h b/include/trace/events/lock.h
index d7512129a324..7bca0a537dbd 100644
--- a/include/trace/events/lock.h
+++ b/include/trace/events/lock.h
@@ -5,11 +5,12 @@
 #if !defined(_TRACE_LOCK_H) || defined(TRACE_HEADER_MULTI_READ)
 #define _TRACE_LOCK_H
 
-#include <linux/lockdep.h>
 #include <linux/tracepoint.h>
 
 #ifdef CONFIG_LOCKDEP
 
+#include <linux/lockdep.h>
+
 TRACE_EVENT(lock_acquire,
 
 	TP_PROTO(struct lockdep_map *lock, unsigned int subclass,
@@ -81,6 +82,45 @@ DEFINE_EVENT(lock, lock_acquired,
 #endif
 #endif
 
+TRACE_EVENT(contention_begin,
+
+	TP_PROTO(void *lock, unsigned long ip, unsigned int flags),
+
+	TP_ARGS(lock, ip, flags),
+
+	TP_STRUCT__entry(
+		__field(void *, lock_addr)
+		__field(unsigned long, ip)
+		__field(unsigned int, flags)
+	),
+
+	TP_fast_assign(
+		__entry->lock_addr = lock;
+		__entry->ip = ip;
+		__entry->flags = flags;
+	),
+
+	TP_printk("%p %pS (%x)", __entry->lock_addr,  (void *) __entry->ip,
+		  __entry->flags)
+);
+
+TRACE_EVENT(contention_end,
+
+	TP_PROTO(void *lock),
+
+	TP_ARGS(lock),
+
+	TP_STRUCT__entry(
+		__field(void *, lock_addr)
+	),
+
+	TP_fast_assign(
+		__entry->lock_addr = lock;
+	),
+
+	TP_printk("%p", __entry->lock_addr)
+);
+
 #endif /* _TRACE_LOCK_H */
 
 /* This part must be outside protection */
diff --git a/kernel/locking/Makefile b/kernel/locking/Makefile
index d51cabf28f38..d212401adcdc 100644
--- a/kernel/locking/Makefile
+++ b/kernel/locking/Makefile
@@ -3,7 +3,7 @@
 # and is generally not a function of system call inputs.
 KCOV_INSTRUMENT		:= n
 
-obj-y += mutex.o semaphore.o rwsem.o percpu-rwsem.o
+obj-y += mutex.o semaphore.o rwsem.o percpu-rwsem.o tracepoint.o
 
 # Avoid recursion lockdep -> KCSAN -> ... -> lockdep.
 KCSAN_SANITIZE_lockdep.o := n
diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
index 50036c10b518..08f8fb6a2d1e 100644
--- a/kernel/locking/lockdep.c
+++ b/kernel/locking/lockdep.c
@@ -60,7 +60,6 @@
 
 #include "lockdep_internals.h"
 
-#define CREATE_TRACE_POINTS
 #include <trace/events/lock.h>
 
 #ifdef CONFIG_PROVE_LOCKING
diff --git a/kernel/locking/tracepoint.c b/kernel/locking/tracepoint.c
new file mode 100644
index 000000000000..d6f5c6c1d7bd
--- /dev/null
+++ b/kernel/locking/tracepoint.c
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#include <linux/lock_trace.h>
+
+#define CREATE_TRACE_POINTS
+#include <trace/events/lock.h>
+
+/* these are exported via LOCK_CONTENTION_{BEGIN,END} macro */
+EXPORT_TRACEPOINT_SYMBOL_GPL(contention_begin);
+EXPORT_TRACEPOINT_SYMBOL_GPL(contention_end);
+
+void lock_contention_begin(void *lock, unsigned long ip, unsigned int flags)
+{
+	trace_contention_begin(lock, ip, flags);
+}
+EXPORT_SYMBOL_GPL(lock_contention_begin);
+
+void lock_contention_end(void *lock)
+{
+	trace_contention_end(lock);
+}
+EXPORT_SYMBOL_GPL(lock_contention_end);
-- 
2.35.1.574.g5d30c73bfb-goog


  reply	other threads:[~2022-03-01  1:04 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-01  1:04 [RFC 0/4] locking: Add new lock contention tracepoints (v2) Namhyung Kim
2022-03-01  1:04 ` Namhyung Kim [this message]
2022-03-01  1:04 ` [PATCH 2/4] locking: Apply contention tracepoints in the slow path Namhyung Kim
2022-03-01  8:43   ` Peter Zijlstra
2022-03-01 18:03     ` Namhyung Kim
2022-03-01  9:03   ` Peter Zijlstra
2022-03-01 14:45     ` Steven Rostedt
2022-03-01 18:14       ` Namhyung Kim
2022-03-01 18:11     ` Namhyung Kim
2022-03-14 21:44     ` Namhyung Kim
2022-03-01  1:04 ` [PATCH 3/4] locking/mutex: Pass proper call-site ip Namhyung Kim
2022-03-01  9:05   ` Peter Zijlstra
2022-03-01 14:53     ` Steven Rostedt
2022-03-01 19:47       ` Peter Zijlstra
2022-03-04  7:28         ` Namhyung Kim
2022-03-01 18:25     ` Namhyung Kim
2022-03-01  1:04 ` [PATCH 4/4] locking/rwsem: " Namhyung Kim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220301010412.431299-2-namhyung@kernel.org \
    --to=namhyung@kernel.org \
    --cc=arnd@arndb.de \
    --cc=boqun.feng@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=byungchul.park@lge.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mingo@kernel.org \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rburny@google.com \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).