linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Namhyung Kim <namhyung@kernel.org>
To: Arnaldo Carvalho de Melo <acme@kernel.org>, Jiri Olsa <jolsa@kernel.org>
Cc: Ingo Molnar <mingo@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	linux-perf-users@vger.kernel.org, Will Deacon <will@kernel.org>,
	Waiman Long <longman@redhat.com>,
	Boqun Feng <boqun.feng@gmail.com>,
	Davidlohr Bueso <dave@stgolabs.net>, Song Liu <song@kernel.org>,
	Hao Luo <haoluo@google.com>,
	bpf@vger.kernel.org
Subject: [PATCH 3/3] perf lock contention: Support old rw_semaphore type
Date: Mon,  6 Feb 2023 16:24:03 -0800	[thread overview]
Message-ID: <20230207002403.63590-4-namhyung@kernel.org> (raw)
In-Reply-To: <20230207002403.63590-1-namhyung@kernel.org>

The old kernel has a different type of the owner field in rwsem.
We can check it using bpf_core_type_matches() builtin in clang but
it also needs its own version check since it's available on recent
versions.

Cc: Hao Luo <haoluo@google.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 .../perf/util/bpf_skel/lock_contention.bpf.c  | 57 ++++++++++++++-----
 1 file changed, 44 insertions(+), 13 deletions(-)

diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/util/bpf_skel/lock_contention.bpf.c
index c5556606134e..e6007eaeda1a 100644
--- a/tools/perf/util/bpf_skel/lock_contention.bpf.c
+++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c
@@ -84,6 +84,14 @@ struct {
 	__uint(max_entries, 1);
 } addr_filter SEC(".maps");
 
+struct rw_semaphore___old {
+	struct task_struct *owner;
+} __attribute__((preserve_access_index));
+
+struct rw_semaphore___new {
+	atomic_long_t owner;
+} __attribute__((preserve_access_index));
+
 /* control flags */
 int enabled;
 int has_cpu;
@@ -161,6 +169,41 @@ static inline int update_task_data(struct task_struct *task)
 	return 0;
 }
 
+#ifndef __has_builtin
+# define __has_builtin(x) 0
+#endif
+
+static inline struct task_struct *get_lock_owner(__u64 lock, __u32 flags)
+{
+	struct task_struct *task;
+	__u64 owner = 0;
+
+	if (flags & LCB_F_MUTEX) {
+		struct mutex *mutex = (void *)lock;
+		owner = BPF_CORE_READ(mutex, owner.counter);
+	} else if (flags == LCB_F_READ || flags == LCB_F_WRITE) {
+#if __has_builtin(bpf_core_type_matches)
+		if (bpf_core_type_matches(struct rw_semaphore___old)) {
+			struct rw_semaphore___old *rwsem = (void *)lock;
+			owner = (unsigned long)BPF_CORE_READ(rwsem, owner);
+		} else if (bpf_core_type_matches(struct rw_semaphore___new)) {
+			struct rw_semaphore___new *rwsem = (void *)lock;
+			owner = BPF_CORE_READ(rwsem, owner.counter);
+		}
+#else
+		/* assume new struct */
+		struct rw_semaphore *rwsem = (void *)lock;
+		owner = BPF_CORE_READ(rwsem, owner.counter);
+#endif
+	}
+
+	if (!owner)
+		return NULL;
+
+	task = (void *)(owner & ~7UL);
+	return task;
+}
+
 SEC("tp_btf/contention_begin")
 int contention_begin(u64 *ctx)
 {
@@ -199,19 +242,7 @@ int contention_begin(u64 *ctx)
 		struct task_struct *task;
 
 		if (lock_owner) {
-			if (pelem->flags & LCB_F_MUTEX) {
-				struct mutex *lock = (void *)pelem->lock;
-				unsigned long owner = BPF_CORE_READ(lock, owner.counter);
-
-				task = (void *)(owner & ~7UL);
-			} else if (pelem->flags == LCB_F_READ || pelem->flags == LCB_F_WRITE) {
-				struct rw_semaphore *lock = (void *)pelem->lock;
-				unsigned long owner = BPF_CORE_READ(lock, owner.counter);
-
-				task = (void *)(owner & ~7UL);
-			} else {
-				task = NULL;
-			}
+			task = get_lock_owner(pelem->lock, pelem->flags);
 
 			/* The flags is not used anymore.  Pass the owner pid. */
 			if (task)
-- 
2.39.1.519.gcb327c4b5f-goog


  parent reply	other threads:[~2023-02-07  0:24 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-07  0:24 [RFC/PATCH 0/3] perf lock contention: Track lock owner (v2) Namhyung Kim
2023-02-07  0:24 ` [PATCH 1/3] perf lock contention: Fix to save callstack for the default modified Namhyung Kim
2023-02-07 22:22   ` Namhyung Kim
2023-02-07  0:24 ` [PATCH 2/3] perf lock contention: Add -o/--lock-owner option Namhyung Kim
2023-02-07 15:11   ` Arnaldo Carvalho de Melo
2023-02-07 22:20     ` Namhyung Kim
2023-02-07  0:24 ` Namhyung Kim [this message]
2023-02-08 13:37 ` [RFC/PATCH 0/3] perf lock contention: Track lock owner (v2) Arnaldo Carvalho de Melo

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=20230207002403.63590-4-namhyung@kernel.org \
    --to=namhyung@kernel.org \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=boqun.feng@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=dave@stgolabs.net \
    --cc=haoluo@google.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=song@kernel.org \
    --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).