linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 00/18] Mutex wrapper, locking and memory leak fixes
@ 2022-08-26 16:40 Ian Rogers
  2022-08-26 16:40 ` [PATCH v4 01/18] perf mutex: Wrapped usage of mutex and cond Ian Rogers
                   ` (5 more replies)
  0 siblings, 6 replies; 11+ messages in thread
From: Ian Rogers @ 2022-08-26 16:40 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Thomas Gleixner, Darren Hart, Davidlohr Bueso,
	André Almeida, Nathan Chancellor, Nick Desaulniers, Tom Rix,
	Weiguo Li, Athira Rajeev, Thomas Richter, Ravi Bangoria,
	Dario Petrillo, Hewenliang, yaowenbin, Wenyu Liu, Song Liu,
	Andrii Nakryiko, Dave Marchevsky, Leo Yan, Kim Phillips,
	Pavithra Gurushankar, Alexandre Truong, Quentin Monnet,
	William Cohen, Andres Freund, Adrian Hunter, Martin Liška,
	Colin Ian King, James Clark, Fangrui Song, Stephane Eranian,
	Kajol Jain, Alexey Bayduraev, Riccardo Mancini, Andi Kleen,
	Masami Hiramatsu, Zechuan Chen, Jason Wang, Christophe JAILLET,
	Remi Bernon, linux-kernel, linux-perf-users, bpf, llvm
  Cc: Ian Rogers

When fixing a locking race and memory leak in:
https://lore.kernel.org/linux-perf-users/20211118193714.2293728-1-irogers@google.com/

It was requested that debug mutex code be separated out into its own
files. This was, in part, done by Pavithra Gurushankar in:
https://lore.kernel.org/lkml/20220727111954.105118-1-gpavithrasha@gmail.com/

These patches fix issues with the previous patches, add in the
original dso->nsinfo fix and then build on our mutex wrapper with
clang's -Wthread-safety analysis. The analysis found missing unlocks
in builtin-sched.c which are fixed and -Wthread-safety is enabled by
default when building with clang.

v4. Adds a comment for the trylock result, fixes the new line (missed
    in v3) and removes two blank lines as suggested by Adrian Hunter.
v3. Adds a missing new line to the error messages and removes the
    pshared argument to mutex_init by having two functions, mutex_init
    and mutex_init_pshared. These changes were suggested by Adrian Hunter.
v2. Breaks apart changes that s/pthread_mutex/mutex/g and the lock
    annotations as requested by Arnaldo and Namhyung. A boolean is
    added to builtin-sched.c to terminate thread funcs rather than
    leaving them blocked on delted mutexes.

Ian Rogers (17):
  perf bench: Update use of pthread mutex/cond
  perf tests: Avoid pthread.h inclusion
  perf hist: Update use of pthread mutex
  perf bpf: Remove unused pthread.h include
  perf lock: Remove unused pthread.h include
  perf record: Update use of pthread mutex
  perf sched: Update use of pthread mutex
  perf ui: Update use of pthread mutex
  perf mmap: Remove unnecessary pthread.h include
  perf dso: Update use of pthread mutex
  perf annotate: Update use of pthread mutex
  perf top: Update use of pthread mutex
  perf dso: Hold lock when accessing nsinfo
  perf mutex: Add thread safety annotations
  perf sched: Fixes for thread safety analysis
  perf top: Fixes for thread safety analysis
  perf build: Enable -Wthread-safety with clang

Pavithra Gurushankar (1):
  perf mutex: Wrapped usage of mutex and cond

 tools/perf/Makefile.config                 |   5 +
 tools/perf/bench/epoll-ctl.c               |  33 +++---
 tools/perf/bench/epoll-wait.c              |  33 +++---
 tools/perf/bench/futex-hash.c              |  33 +++---
 tools/perf/bench/futex-lock-pi.c           |  33 +++---
 tools/perf/bench/futex-requeue.c           |  33 +++---
 tools/perf/bench/futex-wake-parallel.c     |  33 +++---
 tools/perf/bench/futex-wake.c              |  33 +++---
 tools/perf/bench/numa.c                    |  93 ++++++----------
 tools/perf/builtin-inject.c                |   4 +
 tools/perf/builtin-lock.c                  |   1 -
 tools/perf/builtin-record.c                |  13 ++-
 tools/perf/builtin-sched.c                 | 105 +++++++++---------
 tools/perf/builtin-top.c                   |  45 ++++----
 tools/perf/tests/mmap-basic.c              |   2 -
 tools/perf/tests/openat-syscall-all-cpus.c |   2 +-
 tools/perf/tests/perf-record.c             |   2 -
 tools/perf/ui/browser.c                    |  20 ++--
 tools/perf/ui/browsers/annotate.c          |  12 +--
 tools/perf/ui/setup.c                      |   5 +-
 tools/perf/ui/tui/helpline.c               |   5 +-
 tools/perf/ui/tui/progress.c               |   8 +-
 tools/perf/ui/tui/setup.c                  |   8 +-
 tools/perf/ui/tui/util.c                   |  18 ++--
 tools/perf/ui/ui.h                         |   4 +-
 tools/perf/util/Build                      |   1 +
 tools/perf/util/annotate.c                 |  15 +--
 tools/perf/util/annotate.h                 |   4 +-
 tools/perf/util/bpf-event.h                |   1 -
 tools/perf/util/build-id.c                 |  12 ++-
 tools/perf/util/dso.c                      |  19 ++--
 tools/perf/util/dso.h                      |   4 +-
 tools/perf/util/hist.c                     |   6 +-
 tools/perf/util/hist.h                     |   4 +-
 tools/perf/util/map.c                      |   3 +
 tools/perf/util/mmap.h                     |   1 -
 tools/perf/util/mutex.c                    | 119 +++++++++++++++++++++
 tools/perf/util/mutex.h                    | 108 +++++++++++++++++++
 tools/perf/util/probe-event.c              |   3 +
 tools/perf/util/symbol.c                   |   4 +-
 tools/perf/util/top.h                      |   5 +-
 41 files changed, 569 insertions(+), 323 deletions(-)
 create mode 100644 tools/perf/util/mutex.c
 create mode 100644 tools/perf/util/mutex.h

-- 
2.37.2.672.g94769d06f0-goog


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v4 01/18] perf mutex: Wrapped usage of mutex and cond
  2022-08-26 16:40 [PATCH v4 00/18] Mutex wrapper, locking and memory leak fixes Ian Rogers
@ 2022-08-26 16:40 ` Ian Rogers
  2022-08-26 16:40 ` [PATCH v4 02/18] perf bench: Update use of pthread mutex/cond Ian Rogers
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Ian Rogers @ 2022-08-26 16:40 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Thomas Gleixner, Darren Hart, Davidlohr Bueso,
	André Almeida, Nathan Chancellor, Nick Desaulniers, Tom Rix,
	Weiguo Li, Athira Rajeev, Thomas Richter, Ravi Bangoria,
	Dario Petrillo, Hewenliang, yaowenbin, Wenyu Liu, Song Liu,
	Andrii Nakryiko, Dave Marchevsky, Leo Yan, Kim Phillips,
	Pavithra Gurushankar, Alexandre Truong, Quentin Monnet,
	William Cohen, Andres Freund, Adrian Hunter, Martin Liška,
	Colin Ian King, James Clark, Fangrui Song, Stephane Eranian,
	Kajol Jain, Alexey Bayduraev, Riccardo Mancini, Andi Kleen,
	Masami Hiramatsu, Zechuan Chen, Jason Wang, Christophe JAILLET,
	Remi Bernon, linux-kernel, linux-perf-users, bpf, llvm
  Cc: Ian Rogers

From: Pavithra Gurushankar <gpavithrasha@gmail.com>

Added a new header file mutex.h that wraps the usage of
pthread_mutex_t and pthread_cond_t. By abstracting these it is
possible to introduce error checking.

Signed-off-by: Pavithra Gurushankar <gpavithrasha@gmail.com>
Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/Build   |   1 +
 tools/perf/util/mutex.c | 117 ++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/mutex.h |  48 +++++++++++++++++
 3 files changed, 166 insertions(+)
 create mode 100644 tools/perf/util/mutex.c
 create mode 100644 tools/perf/util/mutex.h

diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 9dfae1bda9cc..8fd6dc8de521 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -143,6 +143,7 @@ perf-y += branch.o
 perf-y += mem2node.o
 perf-y += clockid.o
 perf-y += list_sort.o
+perf-y += mutex.o
 
 perf-$(CONFIG_LIBBPF) += bpf-loader.o
 perf-$(CONFIG_LIBBPF) += bpf_map.o
diff --git a/tools/perf/util/mutex.c b/tools/perf/util/mutex.c
new file mode 100644
index 000000000000..5029237164e5
--- /dev/null
+++ b/tools/perf/util/mutex.c
@@ -0,0 +1,117 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "mutex.h"
+
+#include "debug.h"
+#include <linux/string.h>
+#include <errno.h>
+
+static void check_err(const char *fn, int err)
+{
+	char sbuf[STRERR_BUFSIZE];
+
+	if (err == 0)
+		return;
+
+	pr_err("%s error: '%s'\n", fn, str_error_r(err, sbuf, sizeof(sbuf)));
+}
+
+#define CHECK_ERR(err) check_err(__func__, err)
+
+static void __mutex_init(struct mutex *mtx, bool pshared)
+{
+	pthread_mutexattr_t attr;
+
+	CHECK_ERR(pthread_mutexattr_init(&attr));
+
+#ifndef NDEBUG
+	/* In normal builds enable error checking, such as recursive usage. */
+	CHECK_ERR(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
+#endif
+	if (pshared)
+		CHECK_ERR(pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
+
+	CHECK_ERR(pthread_mutex_init(&mtx->lock, &attr));
+	CHECK_ERR(pthread_mutexattr_destroy(&attr));
+}
+
+void mutex_init(struct mutex *mtx)
+{
+	__mutex_init(mtx, /*pshared=*/false);
+}
+
+void mutex_init_pshared(struct mutex *mtx)
+{
+	__mutex_init(mtx, /*pshared=*/true);
+}
+
+void mutex_destroy(struct mutex *mtx)
+{
+	CHECK_ERR(pthread_mutex_destroy(&mtx->lock));
+}
+
+void mutex_lock(struct mutex *mtx)
+{
+	CHECK_ERR(pthread_mutex_lock(&mtx->lock));
+}
+
+void mutex_unlock(struct mutex *mtx)
+{
+	CHECK_ERR(pthread_mutex_unlock(&mtx->lock));
+}
+
+bool mutex_trylock(struct mutex *mtx)
+{
+	int ret = pthread_mutex_trylock(&mtx->lock);
+
+	if (ret == 0)
+		return true; /* Lock acquired. */
+
+	if (ret == EBUSY)
+		return false; /* Lock busy. */
+
+	/* Print error. */
+	CHECK_ERR(ret);
+	return false;
+}
+
+static void __cond_init(struct cond *cnd, bool pshared)
+{
+	pthread_condattr_t attr;
+
+	CHECK_ERR(pthread_condattr_init(&attr));
+	if (pshared)
+		CHECK_ERR(pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
+
+	CHECK_ERR(pthread_cond_init(&cnd->cond, &attr));
+	CHECK_ERR(pthread_condattr_destroy(&attr));
+}
+
+void cond_init(struct cond *cnd)
+{
+	__cond_init(cnd, /*pshared=*/false);
+}
+
+void cond_init_pshared(struct cond *cnd)
+{
+	__cond_init(cnd, /*pshared=*/true);
+}
+
+void cond_destroy(struct cond *cnd)
+{
+	CHECK_ERR(pthread_cond_destroy(&cnd->cond));
+}
+
+void cond_wait(struct cond *cnd, struct mutex *mtx)
+{
+	CHECK_ERR(pthread_cond_wait(&cnd->cond, &mtx->lock));
+}
+
+void cond_signal(struct cond *cnd)
+{
+	CHECK_ERR(pthread_cond_signal(&cnd->cond));
+}
+
+void cond_broadcast(struct cond *cnd)
+{
+	CHECK_ERR(pthread_cond_broadcast(&cnd->cond));
+}
diff --git a/tools/perf/util/mutex.h b/tools/perf/util/mutex.h
new file mode 100644
index 000000000000..cfff32a902d9
--- /dev/null
+++ b/tools/perf/util/mutex.h
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __PERF_MUTEX_H
+#define __PERF_MUTEX_H
+
+#include <pthread.h>
+#include <stdbool.h>
+
+/*
+ * A wrapper around the mutex implementation that allows perf to error check
+ * usage, etc.
+ */
+struct mutex {
+	pthread_mutex_t lock;
+};
+
+/* A wrapper around the condition variable implementation. */
+struct cond {
+	pthread_cond_t cond;
+};
+
+/* Default initialize the mtx struct. */
+void mutex_init(struct mutex *mtx);
+/*
+ * Initialize the mtx struct and set the process-shared rather than default
+ * process-private attribute.
+ */
+void mutex_init_pshared(struct mutex *mtx);
+void mutex_destroy(struct mutex *mtx);
+
+void mutex_lock(struct mutex *mtx);
+void mutex_unlock(struct mutex *mtx);
+/* Tries to acquire the lock and returns true on success. */
+bool mutex_trylock(struct mutex *mtx);
+
+/* Default initialize the cond struct. */
+void cond_init(struct cond *cnd);
+/*
+ * Initialize the cond struct and specify the process-shared rather than default
+ * process-private attribute.
+ */
+void cond_init_pshared(struct cond *cnd);
+void cond_destroy(struct cond *cnd);
+
+void cond_wait(struct cond *cnd, struct mutex *mtx);
+void cond_signal(struct cond *cnd);
+void cond_broadcast(struct cond *cnd);
+
+#endif /* __PERF_MUTEX_H */
-- 
2.37.2.672.g94769d06f0-goog


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v4 02/18] perf bench: Update use of pthread mutex/cond
  2022-08-26 16:40 [PATCH v4 00/18] Mutex wrapper, locking and memory leak fixes Ian Rogers
  2022-08-26 16:40 ` [PATCH v4 01/18] perf mutex: Wrapped usage of mutex and cond Ian Rogers
@ 2022-08-26 16:40 ` Ian Rogers
  2022-08-26 16:40 ` [PATCH v4 03/18] perf tests: Avoid pthread.h inclusion Ian Rogers
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Ian Rogers @ 2022-08-26 16:40 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Thomas Gleixner, Darren Hart, Davidlohr Bueso,
	André Almeida, Nathan Chancellor, Nick Desaulniers, Tom Rix,
	Weiguo Li, Athira Rajeev, Thomas Richter, Ravi Bangoria,
	Dario Petrillo, Hewenliang, yaowenbin, Wenyu Liu, Song Liu,
	Andrii Nakryiko, Dave Marchevsky, Leo Yan, Kim Phillips,
	Pavithra Gurushankar, Alexandre Truong, Quentin Monnet,
	William Cohen, Andres Freund, Adrian Hunter, Martin Liška,
	Colin Ian King, James Clark, Fangrui Song, Stephane Eranian,
	Kajol Jain, Alexey Bayduraev, Riccardo Mancini, Andi Kleen,
	Masami Hiramatsu, Zechuan Chen, Jason Wang, Christophe JAILLET,
	Remi Bernon, linux-kernel, linux-perf-users, bpf, llvm
  Cc: Ian Rogers

Switch to the use of mutex wrappers that provide better error checking.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/bench/epoll-ctl.c           | 33 ++++-----
 tools/perf/bench/epoll-wait.c          | 33 ++++-----
 tools/perf/bench/futex-hash.c          | 33 ++++-----
 tools/perf/bench/futex-lock-pi.c       | 33 ++++-----
 tools/perf/bench/futex-requeue.c       | 33 ++++-----
 tools/perf/bench/futex-wake-parallel.c | 33 ++++-----
 tools/perf/bench/futex-wake.c          | 33 ++++-----
 tools/perf/bench/numa.c                | 93 ++++++++++----------------
 8 files changed, 153 insertions(+), 171 deletions(-)

diff --git a/tools/perf/bench/epoll-ctl.c b/tools/perf/bench/epoll-ctl.c
index 4256dc5d6236..521d1ff97b06 100644
--- a/tools/perf/bench/epoll-ctl.c
+++ b/tools/perf/bench/epoll-ctl.c
@@ -23,6 +23,7 @@
 #include <sys/eventfd.h>
 #include <perf/cpumap.h>
 
+#include "../util/mutex.h"
 #include "../util/stat.h"
 #include <subcmd/parse-options.h>
 #include "bench.h"
@@ -58,10 +59,10 @@ static unsigned int nested = 0;
 /* amount of fds to monitor, per thread */
 static unsigned int nfds = 64;
 
-static pthread_mutex_t thread_lock;
+static struct mutex thread_lock;
 static unsigned int threads_starting;
 static struct stats all_stats[EPOLL_NR_OPS];
-static pthread_cond_t thread_parent, thread_worker;
+static struct cond thread_parent, thread_worker;
 
 struct worker {
 	int tid;
@@ -174,12 +175,12 @@ static void *workerfn(void *arg)
 	struct timespec ts = { .tv_sec = 0,
 			       .tv_nsec = 250 };
 
-	pthread_mutex_lock(&thread_lock);
+	mutex_lock(&thread_lock);
 	threads_starting--;
 	if (!threads_starting)
-		pthread_cond_signal(&thread_parent);
-	pthread_cond_wait(&thread_worker, &thread_lock);
-	pthread_mutex_unlock(&thread_lock);
+		cond_signal(&thread_parent);
+	cond_wait(&thread_worker, &thread_lock);
+	mutex_unlock(&thread_lock);
 
 	/* Let 'em loose */
 	do {
@@ -367,9 +368,9 @@ int bench_epoll_ctl(int argc, const char **argv)
 	for (i = 0; i < EPOLL_NR_OPS; i++)
 		init_stats(&all_stats[i]);
 
-	pthread_mutex_init(&thread_lock, NULL);
-	pthread_cond_init(&thread_parent, NULL);
-	pthread_cond_init(&thread_worker, NULL);
+	mutex_init(&thread_lock);
+	cond_init(&thread_parent);
+	cond_init(&thread_worker);
 
 	threads_starting = nthreads;
 
@@ -377,11 +378,11 @@ int bench_epoll_ctl(int argc, const char **argv)
 
 	do_threads(worker, cpu);
 
-	pthread_mutex_lock(&thread_lock);
+	mutex_lock(&thread_lock);
 	while (threads_starting)
-		pthread_cond_wait(&thread_parent, &thread_lock);
-	pthread_cond_broadcast(&thread_worker);
-	pthread_mutex_unlock(&thread_lock);
+		cond_wait(&thread_parent, &thread_lock);
+	cond_broadcast(&thread_worker);
+	mutex_unlock(&thread_lock);
 
 	sleep(nsecs);
 	toggle_done(0, NULL, NULL);
@@ -394,9 +395,9 @@ int bench_epoll_ctl(int argc, const char **argv)
 	}
 
 	/* cleanup & report results */
-	pthread_cond_destroy(&thread_parent);
-	pthread_cond_destroy(&thread_worker);
-	pthread_mutex_destroy(&thread_lock);
+	cond_destroy(&thread_parent);
+	cond_destroy(&thread_worker);
+	mutex_destroy(&thread_lock);
 
 	for (i = 0; i < nthreads; i++) {
 		unsigned long t[EPOLL_NR_OPS];
diff --git a/tools/perf/bench/epoll-wait.c b/tools/perf/bench/epoll-wait.c
index 2728b0140853..c1cdf03c075d 100644
--- a/tools/perf/bench/epoll-wait.c
+++ b/tools/perf/bench/epoll-wait.c
@@ -79,6 +79,7 @@
 #include <perf/cpumap.h>
 
 #include "../util/stat.h"
+#include "../util/mutex.h"
 #include <subcmd/parse-options.h>
 #include "bench.h"
 
@@ -109,10 +110,10 @@ static bool multiq; /* use an epoll instance per thread */
 /* amount of fds to monitor, per thread */
 static unsigned int nfds = 64;
 
-static pthread_mutex_t thread_lock;
+static struct mutex thread_lock;
 static unsigned int threads_starting;
 static struct stats throughput_stats;
-static pthread_cond_t thread_parent, thread_worker;
+static struct cond thread_parent, thread_worker;
 
 struct worker {
 	int tid;
@@ -189,12 +190,12 @@ static void *workerfn(void *arg)
 	int to = nonblocking? 0 : -1;
 	int efd = multiq ? w->epollfd : epollfd;
 
-	pthread_mutex_lock(&thread_lock);
+	mutex_lock(&thread_lock);
 	threads_starting--;
 	if (!threads_starting)
-		pthread_cond_signal(&thread_parent);
-	pthread_cond_wait(&thread_worker, &thread_lock);
-	pthread_mutex_unlock(&thread_lock);
+		cond_signal(&thread_parent);
+	cond_wait(&thread_worker, &thread_lock);
+	mutex_unlock(&thread_lock);
 
 	do {
 		/*
@@ -485,9 +486,9 @@ int bench_epoll_wait(int argc, const char **argv)
 	       getpid(), nthreads, oneshot ? " (EPOLLONESHOT semantics)": "", nfds, nsecs);
 
 	init_stats(&throughput_stats);
-	pthread_mutex_init(&thread_lock, NULL);
-	pthread_cond_init(&thread_parent, NULL);
-	pthread_cond_init(&thread_worker, NULL);
+	mutex_init(&thread_lock);
+	cond_init(&thread_parent);
+	cond_init(&thread_worker);
 
 	threads_starting = nthreads;
 
@@ -495,11 +496,11 @@ int bench_epoll_wait(int argc, const char **argv)
 
 	do_threads(worker, cpu);
 
-	pthread_mutex_lock(&thread_lock);
+	mutex_lock(&thread_lock);
 	while (threads_starting)
-		pthread_cond_wait(&thread_parent, &thread_lock);
-	pthread_cond_broadcast(&thread_worker);
-	pthread_mutex_unlock(&thread_lock);
+		cond_wait(&thread_parent, &thread_lock);
+	cond_broadcast(&thread_worker);
+	mutex_unlock(&thread_lock);
 
 	/*
 	 * At this point the workers should be blocked waiting for read events
@@ -522,9 +523,9 @@ int bench_epoll_wait(int argc, const char **argv)
 		err(EXIT_FAILURE, "pthread_join");
 
 	/* cleanup & report results */
-	pthread_cond_destroy(&thread_parent);
-	pthread_cond_destroy(&thread_worker);
-	pthread_mutex_destroy(&thread_lock);
+	cond_destroy(&thread_parent);
+	cond_destroy(&thread_worker);
+	mutex_destroy(&thread_lock);
 
 	/* sort the array back before reporting */
 	if (randomize)
diff --git a/tools/perf/bench/futex-hash.c b/tools/perf/bench/futex-hash.c
index f05db4cf983d..2005a3fa3026 100644
--- a/tools/perf/bench/futex-hash.c
+++ b/tools/perf/bench/futex-hash.c
@@ -23,6 +23,7 @@
 #include <sys/mman.h>
 #include <perf/cpumap.h>
 
+#include "../util/mutex.h"
 #include "../util/stat.h"
 #include <subcmd/parse-options.h>
 #include "bench.h"
@@ -34,10 +35,10 @@ static bool done = false;
 static int futex_flag = 0;
 
 struct timeval bench__start, bench__end, bench__runtime;
-static pthread_mutex_t thread_lock;
+static struct mutex thread_lock;
 static unsigned int threads_starting;
 static struct stats throughput_stats;
-static pthread_cond_t thread_parent, thread_worker;
+static struct cond thread_parent, thread_worker;
 
 struct worker {
 	int tid;
@@ -73,12 +74,12 @@ static void *workerfn(void *arg)
 	unsigned int i;
 	unsigned long ops = w->ops; /* avoid cacheline bouncing */
 
-	pthread_mutex_lock(&thread_lock);
+	mutex_lock(&thread_lock);
 	threads_starting--;
 	if (!threads_starting)
-		pthread_cond_signal(&thread_parent);
-	pthread_cond_wait(&thread_worker, &thread_lock);
-	pthread_mutex_unlock(&thread_lock);
+		cond_signal(&thread_parent);
+	cond_wait(&thread_worker, &thread_lock);
+	mutex_unlock(&thread_lock);
 
 	do {
 		for (i = 0; i < params.nfutexes; i++, ops++) {
@@ -165,9 +166,9 @@ int bench_futex_hash(int argc, const char **argv)
 	       getpid(), params.nthreads, params.nfutexes, params.fshared ? "shared":"private", params.runtime);
 
 	init_stats(&throughput_stats);
-	pthread_mutex_init(&thread_lock, NULL);
-	pthread_cond_init(&thread_parent, NULL);
-	pthread_cond_init(&thread_worker, NULL);
+	mutex_init(&thread_lock);
+	cond_init(&thread_parent);
+	cond_init(&thread_worker);
 
 	threads_starting = params.nthreads;
 	pthread_attr_init(&thread_attr);
@@ -203,11 +204,11 @@ int bench_futex_hash(int argc, const char **argv)
 	CPU_FREE(cpuset);
 	pthread_attr_destroy(&thread_attr);
 
-	pthread_mutex_lock(&thread_lock);
+	mutex_lock(&thread_lock);
 	while (threads_starting)
-		pthread_cond_wait(&thread_parent, &thread_lock);
-	pthread_cond_broadcast(&thread_worker);
-	pthread_mutex_unlock(&thread_lock);
+		cond_wait(&thread_parent, &thread_lock);
+	cond_broadcast(&thread_worker);
+	mutex_unlock(&thread_lock);
 
 	sleep(params.runtime);
 	toggle_done(0, NULL, NULL);
@@ -219,9 +220,9 @@ int bench_futex_hash(int argc, const char **argv)
 	}
 
 	/* cleanup & report results */
-	pthread_cond_destroy(&thread_parent);
-	pthread_cond_destroy(&thread_worker);
-	pthread_mutex_destroy(&thread_lock);
+	cond_destroy(&thread_parent);
+	cond_destroy(&thread_worker);
+	mutex_destroy(&thread_lock);
 
 	for (i = 0; i < params.nthreads; i++) {
 		unsigned long t = bench__runtime.tv_sec > 0 ?
diff --git a/tools/perf/bench/futex-lock-pi.c b/tools/perf/bench/futex-lock-pi.c
index 0abb3f7ee24f..2d0417949727 100644
--- a/tools/perf/bench/futex-lock-pi.c
+++ b/tools/perf/bench/futex-lock-pi.c
@@ -8,6 +8,7 @@
 #include <pthread.h>
 
 #include <signal.h>
+#include "../util/mutex.h"
 #include "../util/stat.h"
 #include <subcmd/parse-options.h>
 #include <linux/compiler.h>
@@ -34,10 +35,10 @@ static u_int32_t global_futex = 0;
 static struct worker *worker;
 static bool done = false;
 static int futex_flag = 0;
-static pthread_mutex_t thread_lock;
+static struct mutex thread_lock;
 static unsigned int threads_starting;
 static struct stats throughput_stats;
-static pthread_cond_t thread_parent, thread_worker;
+static struct cond thread_parent, thread_worker;
 
 static struct bench_futex_parameters params = {
 	.runtime  = 10,
@@ -83,12 +84,12 @@ static void *workerfn(void *arg)
 	struct worker *w = (struct worker *) arg;
 	unsigned long ops = w->ops;
 
-	pthread_mutex_lock(&thread_lock);
+	mutex_lock(&thread_lock);
 	threads_starting--;
 	if (!threads_starting)
-		pthread_cond_signal(&thread_parent);
-	pthread_cond_wait(&thread_worker, &thread_lock);
-	pthread_mutex_unlock(&thread_lock);
+		cond_signal(&thread_parent);
+	cond_wait(&thread_worker, &thread_lock);
+	mutex_unlock(&thread_lock);
 
 	do {
 		int ret;
@@ -197,9 +198,9 @@ int bench_futex_lock_pi(int argc, const char **argv)
 	       getpid(), params.nthreads, params.runtime);
 
 	init_stats(&throughput_stats);
-	pthread_mutex_init(&thread_lock, NULL);
-	pthread_cond_init(&thread_parent, NULL);
-	pthread_cond_init(&thread_worker, NULL);
+	mutex_init(&thread_lock);
+	cond_init(&thread_parent);
+	cond_init(&thread_worker);
 
 	threads_starting = params.nthreads;
 	pthread_attr_init(&thread_attr);
@@ -208,11 +209,11 @@ int bench_futex_lock_pi(int argc, const char **argv)
 	create_threads(worker, thread_attr, cpu);
 	pthread_attr_destroy(&thread_attr);
 
-	pthread_mutex_lock(&thread_lock);
+	mutex_lock(&thread_lock);
 	while (threads_starting)
-		pthread_cond_wait(&thread_parent, &thread_lock);
-	pthread_cond_broadcast(&thread_worker);
-	pthread_mutex_unlock(&thread_lock);
+		cond_wait(&thread_parent, &thread_lock);
+	cond_broadcast(&thread_worker);
+	mutex_unlock(&thread_lock);
 
 	sleep(params.runtime);
 	toggle_done(0, NULL, NULL);
@@ -224,9 +225,9 @@ int bench_futex_lock_pi(int argc, const char **argv)
 	}
 
 	/* cleanup & report results */
-	pthread_cond_destroy(&thread_parent);
-	pthread_cond_destroy(&thread_worker);
-	pthread_mutex_destroy(&thread_lock);
+	cond_destroy(&thread_parent);
+	cond_destroy(&thread_worker);
+	mutex_destroy(&thread_lock);
 
 	for (i = 0; i < params.nthreads; i++) {
 		unsigned long t = bench__runtime.tv_sec > 0 ?
diff --git a/tools/perf/bench/futex-requeue.c b/tools/perf/bench/futex-requeue.c
index b6faabfafb8e..69ad896f556c 100644
--- a/tools/perf/bench/futex-requeue.c
+++ b/tools/perf/bench/futex-requeue.c
@@ -15,6 +15,7 @@
 #include <pthread.h>
 
 #include <signal.h>
+#include "../util/mutex.h"
 #include "../util/stat.h"
 #include <subcmd/parse-options.h>
 #include <linux/compiler.h>
@@ -34,8 +35,8 @@ static u_int32_t futex1 = 0, futex2 = 0;
 
 static pthread_t *worker;
 static bool done = false;
-static pthread_mutex_t thread_lock;
-static pthread_cond_t thread_parent, thread_worker;
+static struct mutex thread_lock;
+static struct cond thread_parent, thread_worker;
 static struct stats requeuetime_stats, requeued_stats;
 static unsigned int threads_starting;
 static int futex_flag = 0;
@@ -82,12 +83,12 @@ static void *workerfn(void *arg __maybe_unused)
 {
 	int ret;
 
-	pthread_mutex_lock(&thread_lock);
+	mutex_lock(&thread_lock);
 	threads_starting--;
 	if (!threads_starting)
-		pthread_cond_signal(&thread_parent);
-	pthread_cond_wait(&thread_worker, &thread_lock);
-	pthread_mutex_unlock(&thread_lock);
+		cond_signal(&thread_parent);
+	cond_wait(&thread_worker, &thread_lock);
+	mutex_unlock(&thread_lock);
 
 	while (1) {
 		if (!params.pi) {
@@ -209,9 +210,9 @@ int bench_futex_requeue(int argc, const char **argv)
 	init_stats(&requeued_stats);
 	init_stats(&requeuetime_stats);
 	pthread_attr_init(&thread_attr);
-	pthread_mutex_init(&thread_lock, NULL);
-	pthread_cond_init(&thread_parent, NULL);
-	pthread_cond_init(&thread_worker, NULL);
+	mutex_init(&thread_lock);
+	cond_init(&thread_parent);
+	cond_init(&thread_worker);
 
 	for (j = 0; j < bench_repeat && !done; j++) {
 		unsigned int nrequeued = 0, wakeups = 0;
@@ -221,11 +222,11 @@ int bench_futex_requeue(int argc, const char **argv)
 		block_threads(worker, thread_attr, cpu);
 
 		/* make sure all threads are already blocked */
-		pthread_mutex_lock(&thread_lock);
+		mutex_lock(&thread_lock);
 		while (threads_starting)
-			pthread_cond_wait(&thread_parent, &thread_lock);
-		pthread_cond_broadcast(&thread_worker);
-		pthread_mutex_unlock(&thread_lock);
+			cond_wait(&thread_parent, &thread_lock);
+		cond_broadcast(&thread_worker);
+		mutex_unlock(&thread_lock);
 
 		usleep(100000);
 
@@ -297,9 +298,9 @@ int bench_futex_requeue(int argc, const char **argv)
 	}
 
 	/* cleanup & report results */
-	pthread_cond_destroy(&thread_parent);
-	pthread_cond_destroy(&thread_worker);
-	pthread_mutex_destroy(&thread_lock);
+	cond_destroy(&thread_parent);
+	cond_destroy(&thread_worker);
+	mutex_destroy(&thread_lock);
 	pthread_attr_destroy(&thread_attr);
 
 	print_summary();
diff --git a/tools/perf/bench/futex-wake-parallel.c b/tools/perf/bench/futex-wake-parallel.c
index e47f46a3a47e..6682e49d0ee0 100644
--- a/tools/perf/bench/futex-wake-parallel.c
+++ b/tools/perf/bench/futex-wake-parallel.c
@@ -10,6 +10,7 @@
 #include "bench.h"
 #include <linux/compiler.h>
 #include "../util/debug.h"
+#include "../util/mutex.h"
 
 #ifndef HAVE_PTHREAD_BARRIER
 int bench_futex_wake_parallel(int argc __maybe_unused, const char **argv __maybe_unused)
@@ -49,8 +50,8 @@ static u_int32_t futex = 0;
 
 static pthread_t *blocked_worker;
 static bool done = false;
-static pthread_mutex_t thread_lock;
-static pthread_cond_t thread_parent, thread_worker;
+static struct mutex thread_lock;
+static struct cond thread_parent, thread_worker;
 static pthread_barrier_t barrier;
 static struct stats waketime_stats, wakeup_stats;
 static unsigned int threads_starting;
@@ -125,12 +126,12 @@ static void wakeup_threads(struct thread_data *td, pthread_attr_t thread_attr)
 
 static void *blocked_workerfn(void *arg __maybe_unused)
 {
-	pthread_mutex_lock(&thread_lock);
+	mutex_lock(&thread_lock);
 	threads_starting--;
 	if (!threads_starting)
-		pthread_cond_signal(&thread_parent);
-	pthread_cond_wait(&thread_worker, &thread_lock);
-	pthread_mutex_unlock(&thread_lock);
+		cond_signal(&thread_parent);
+	cond_wait(&thread_worker, &thread_lock);
+	mutex_unlock(&thread_lock);
 
 	while (1) { /* handle spurious wakeups */
 		if (futex_wait(&futex, 0, NULL, futex_flag) != EINTR)
@@ -294,9 +295,9 @@ int bench_futex_wake_parallel(int argc, const char **argv)
 	init_stats(&waketime_stats);
 
 	pthread_attr_init(&thread_attr);
-	pthread_mutex_init(&thread_lock, NULL);
-	pthread_cond_init(&thread_parent, NULL);
-	pthread_cond_init(&thread_worker, NULL);
+	mutex_init(&thread_lock);
+	cond_init(&thread_parent);
+	cond_init(&thread_worker);
 
 	for (j = 0; j < bench_repeat && !done; j++) {
 		waking_worker = calloc(params.nwakes, sizeof(*waking_worker));
@@ -307,11 +308,11 @@ int bench_futex_wake_parallel(int argc, const char **argv)
 		block_threads(blocked_worker, thread_attr, cpu);
 
 		/* make sure all threads are already blocked */
-		pthread_mutex_lock(&thread_lock);
+		mutex_lock(&thread_lock);
 		while (threads_starting)
-			pthread_cond_wait(&thread_parent, &thread_lock);
-		pthread_cond_broadcast(&thread_worker);
-		pthread_mutex_unlock(&thread_lock);
+			cond_wait(&thread_parent, &thread_lock);
+		cond_broadcast(&thread_worker);
+		mutex_unlock(&thread_lock);
 
 		usleep(100000);
 
@@ -332,9 +333,9 @@ int bench_futex_wake_parallel(int argc, const char **argv)
 	}
 
 	/* cleanup & report results */
-	pthread_cond_destroy(&thread_parent);
-	pthread_cond_destroy(&thread_worker);
-	pthread_mutex_destroy(&thread_lock);
+	cond_destroy(&thread_parent);
+	cond_destroy(&thread_worker);
+	mutex_destroy(&thread_lock);
 	pthread_attr_destroy(&thread_attr);
 
 	print_summary();
diff --git a/tools/perf/bench/futex-wake.c b/tools/perf/bench/futex-wake.c
index 201a3555f09a..9ecab6620a87 100644
--- a/tools/perf/bench/futex-wake.c
+++ b/tools/perf/bench/futex-wake.c
@@ -14,6 +14,7 @@
 #include <pthread.h>
 
 #include <signal.h>
+#include "../util/mutex.h"
 #include "../util/stat.h"
 #include <subcmd/parse-options.h>
 #include <linux/compiler.h>
@@ -34,8 +35,8 @@ static u_int32_t futex1 = 0;
 
 static pthread_t *worker;
 static bool done = false;
-static pthread_mutex_t thread_lock;
-static pthread_cond_t thread_parent, thread_worker;
+static struct mutex thread_lock;
+static struct cond thread_parent, thread_worker;
 static struct stats waketime_stats, wakeup_stats;
 static unsigned int threads_starting;
 static int futex_flag = 0;
@@ -65,12 +66,12 @@ static const char * const bench_futex_wake_usage[] = {
 
 static void *workerfn(void *arg __maybe_unused)
 {
-	pthread_mutex_lock(&thread_lock);
+	mutex_lock(&thread_lock);
 	threads_starting--;
 	if (!threads_starting)
-		pthread_cond_signal(&thread_parent);
-	pthread_cond_wait(&thread_worker, &thread_lock);
-	pthread_mutex_unlock(&thread_lock);
+		cond_signal(&thread_parent);
+	cond_wait(&thread_worker, &thread_lock);
+	mutex_unlock(&thread_lock);
 
 	while (1) {
 		if (futex_wait(&futex1, 0, NULL, futex_flag) != EINTR)
@@ -178,9 +179,9 @@ int bench_futex_wake(int argc, const char **argv)
 	init_stats(&wakeup_stats);
 	init_stats(&waketime_stats);
 	pthread_attr_init(&thread_attr);
-	pthread_mutex_init(&thread_lock, NULL);
-	pthread_cond_init(&thread_parent, NULL);
-	pthread_cond_init(&thread_worker, NULL);
+	mutex_init(&thread_lock);
+	cond_init(&thread_parent);
+	cond_init(&thread_worker);
 
 	for (j = 0; j < bench_repeat && !done; j++) {
 		unsigned int nwoken = 0;
@@ -190,11 +191,11 @@ int bench_futex_wake(int argc, const char **argv)
 		block_threads(worker, thread_attr, cpu);
 
 		/* make sure all threads are already blocked */
-		pthread_mutex_lock(&thread_lock);
+		mutex_lock(&thread_lock);
 		while (threads_starting)
-			pthread_cond_wait(&thread_parent, &thread_lock);
-		pthread_cond_broadcast(&thread_worker);
-		pthread_mutex_unlock(&thread_lock);
+			cond_wait(&thread_parent, &thread_lock);
+		cond_broadcast(&thread_worker);
+		mutex_unlock(&thread_lock);
 
 		usleep(100000);
 
@@ -224,9 +225,9 @@ int bench_futex_wake(int argc, const char **argv)
 	}
 
 	/* cleanup & report results */
-	pthread_cond_destroy(&thread_parent);
-	pthread_cond_destroy(&thread_worker);
-	pthread_mutex_destroy(&thread_lock);
+	cond_destroy(&thread_parent);
+	cond_destroy(&thread_worker);
+	mutex_destroy(&thread_lock);
 	pthread_attr_destroy(&thread_attr);
 
 	print_summary();
diff --git a/tools/perf/bench/numa.c b/tools/perf/bench/numa.c
index 20eed1e53f80..e78dedf9e682 100644
--- a/tools/perf/bench/numa.c
+++ b/tools/perf/bench/numa.c
@@ -6,8 +6,6 @@
  */
 
 #include <inttypes.h>
-/* For the CLR_() macros */
-#include <pthread.h>
 
 #include <subcmd/parse-options.h>
 #include "../util/cloexec.h"
@@ -35,6 +33,7 @@
 #include <linux/zalloc.h>
 
 #include "../util/header.h"
+#include "../util/mutex.h"
 #include <numa.h>
 #include <numaif.h>
 
@@ -67,7 +66,7 @@ struct thread_data {
 	u64			system_time_ns;
 	u64			user_time_ns;
 	double			speed_gbs;
-	pthread_mutex_t		*process_lock;
+	struct mutex		*process_lock;
 };
 
 /* Parameters set by options: */
@@ -137,16 +136,16 @@ struct params {
 struct global_info {
 	u8			*data;
 
-	pthread_mutex_t		startup_mutex;
-	pthread_cond_t		startup_cond;
+	struct mutex		startup_mutex;
+	struct cond		startup_cond;
 	int			nr_tasks_started;
 
-	pthread_mutex_t		start_work_mutex;
-	pthread_cond_t		start_work_cond;
+	struct mutex		start_work_mutex;
+	struct cond		start_work_cond;
 	int			nr_tasks_working;
 	bool			start_work;
 
-	pthread_mutex_t		stop_work_mutex;
+	struct mutex		stop_work_mutex;
 	u64			bytes_done;
 
 	struct thread_data	*threads;
@@ -524,30 +523,6 @@ static void * setup_private_data(ssize_t bytes)
 	return alloc_data(bytes, MAP_PRIVATE, 0, g->p.init_cpu0,  g->p.thp, g->p.init_random);
 }
 
-/*
- * Return a process-shared (global) mutex:
- */
-static void init_global_mutex(pthread_mutex_t *mutex)
-{
-	pthread_mutexattr_t attr;
-
-	pthread_mutexattr_init(&attr);
-	pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
-	pthread_mutex_init(mutex, &attr);
-}
-
-/*
- * Return a process-shared (global) condition variable:
- */
-static void init_global_cond(pthread_cond_t *cond)
-{
-	pthread_condattr_t attr;
-
-	pthread_condattr_init(&attr);
-	pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
-	pthread_cond_init(cond, &attr);
-}
-
 static int parse_cpu_list(const char *arg)
 {
 	p0.cpu_list_str = strdup(arg);
@@ -1220,22 +1195,22 @@ static void *worker_thread(void *__tdata)
 	}
 
 	if (g->p.serialize_startup) {
-		pthread_mutex_lock(&g->startup_mutex);
+		mutex_lock(&g->startup_mutex);
 		g->nr_tasks_started++;
 		/* The last thread wakes the main process. */
 		if (g->nr_tasks_started == g->p.nr_tasks)
-			pthread_cond_signal(&g->startup_cond);
+			cond_signal(&g->startup_cond);
 
-		pthread_mutex_unlock(&g->startup_mutex);
+		mutex_unlock(&g->startup_mutex);
 
 		/* Here we will wait for the main process to start us all at once: */
-		pthread_mutex_lock(&g->start_work_mutex);
+		mutex_lock(&g->start_work_mutex);
 		g->start_work = false;
 		g->nr_tasks_working++;
 		while (!g->start_work)
-			pthread_cond_wait(&g->start_work_cond, &g->start_work_mutex);
+			cond_wait(&g->start_work_cond, &g->start_work_mutex);
 
-		pthread_mutex_unlock(&g->start_work_mutex);
+		mutex_unlock(&g->start_work_mutex);
 	}
 
 	gettimeofday(&start0, NULL);
@@ -1254,17 +1229,17 @@ static void *worker_thread(void *__tdata)
 		val += do_work(thread_data,  g->p.bytes_thread,  0,          1,		l, val);
 
 		if (g->p.sleep_usecs) {
-			pthread_mutex_lock(td->process_lock);
+			mutex_lock(td->process_lock);
 			usleep(g->p.sleep_usecs);
-			pthread_mutex_unlock(td->process_lock);
+			mutex_unlock(td->process_lock);
 		}
 		/*
 		 * Amount of work to be done under a process-global lock:
 		 */
 		if (g->p.bytes_process_locked) {
-			pthread_mutex_lock(td->process_lock);
+			mutex_lock(td->process_lock);
 			val += do_work(process_data, g->p.bytes_process_locked, thread_nr,  g->p.nr_threads,	l, val);
-			pthread_mutex_unlock(td->process_lock);
+			mutex_unlock(td->process_lock);
 		}
 
 		work_done = g->p.bytes_global + g->p.bytes_process +
@@ -1361,9 +1336,9 @@ static void *worker_thread(void *__tdata)
 
 	free_data(thread_data, g->p.bytes_thread);
 
-	pthread_mutex_lock(&g->stop_work_mutex);
+	mutex_lock(&g->stop_work_mutex);
 	g->bytes_done += bytes_done;
-	pthread_mutex_unlock(&g->stop_work_mutex);
+	mutex_unlock(&g->stop_work_mutex);
 
 	return NULL;
 }
@@ -1373,7 +1348,7 @@ static void *worker_thread(void *__tdata)
  */
 static void worker_process(int process_nr)
 {
-	pthread_mutex_t process_lock;
+	struct mutex process_lock;
 	struct thread_data *td;
 	pthread_t *pthreads;
 	u8 *process_data;
@@ -1381,7 +1356,7 @@ static void worker_process(int process_nr)
 	int ret;
 	int t;
 
-	pthread_mutex_init(&process_lock, NULL);
+	mutex_init(&process_lock);
 	set_taskname("process %d", process_nr);
 
 	/*
@@ -1540,11 +1515,11 @@ static int init(void)
 	g->data = setup_shared_data(g->p.bytes_global);
 
 	/* Startup serialization: */
-	init_global_mutex(&g->start_work_mutex);
-	init_global_cond(&g->start_work_cond);
-	init_global_mutex(&g->startup_mutex);
-	init_global_cond(&g->startup_cond);
-	init_global_mutex(&g->stop_work_mutex);
+	mutex_init_pshared(&g->start_work_mutex);
+	cond_init_pshared(&g->start_work_cond);
+	mutex_init_pshared(&g->startup_mutex);
+	cond_init_pshared(&g->startup_cond);
+	mutex_init_pshared(&g->stop_work_mutex);
 
 	init_thread_data();
 
@@ -1633,17 +1608,17 @@ static int __bench_numa(const char *name)
 		 * Wait for all the threads to start up. The last thread will
 		 * signal this process.
 		 */
-		pthread_mutex_lock(&g->startup_mutex);
+		mutex_lock(&g->startup_mutex);
 		while (g->nr_tasks_started != g->p.nr_tasks)
-			pthread_cond_wait(&g->startup_cond, &g->startup_mutex);
+			cond_wait(&g->startup_cond, &g->startup_mutex);
 
-		pthread_mutex_unlock(&g->startup_mutex);
+		mutex_unlock(&g->startup_mutex);
 
 		/* Wait for all threads to be at the start_work_cond. */
 		while (!threads_ready) {
-			pthread_mutex_lock(&g->start_work_mutex);
+			mutex_lock(&g->start_work_mutex);
 			threads_ready = (g->nr_tasks_working == g->p.nr_tasks);
-			pthread_mutex_unlock(&g->start_work_mutex);
+			mutex_unlock(&g->start_work_mutex);
 			if (!threads_ready)
 				usleep(1);
 		}
@@ -1661,10 +1636,10 @@ static int __bench_numa(const char *name)
 
 		start = stop;
 		/* Start all threads running. */
-		pthread_mutex_lock(&g->start_work_mutex);
+		mutex_lock(&g->start_work_mutex);
 		g->start_work = true;
-		pthread_mutex_unlock(&g->start_work_mutex);
-		pthread_cond_broadcast(&g->start_work_cond);
+		mutex_unlock(&g->start_work_mutex);
+		cond_broadcast(&g->start_work_cond);
 	} else {
 		gettimeofday(&start, NULL);
 	}
-- 
2.37.2.672.g94769d06f0-goog


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v4 03/18] perf tests: Avoid pthread.h inclusion
  2022-08-26 16:40 [PATCH v4 00/18] Mutex wrapper, locking and memory leak fixes Ian Rogers
  2022-08-26 16:40 ` [PATCH v4 01/18] perf mutex: Wrapped usage of mutex and cond Ian Rogers
  2022-08-26 16:40 ` [PATCH v4 02/18] perf bench: Update use of pthread mutex/cond Ian Rogers
@ 2022-08-26 16:40 ` Ian Rogers
  2022-08-26 16:40 ` [PATCH v4 04/18] perf hist: Update use of pthread mutex Ian Rogers
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Ian Rogers @ 2022-08-26 16:40 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Thomas Gleixner, Darren Hart, Davidlohr Bueso,
	André Almeida, Nathan Chancellor, Nick Desaulniers, Tom Rix,
	Weiguo Li, Athira Rajeev, Thomas Richter, Ravi Bangoria,
	Dario Petrillo, Hewenliang, yaowenbin, Wenyu Liu, Song Liu,
	Andrii Nakryiko, Dave Marchevsky, Leo Yan, Kim Phillips,
	Pavithra Gurushankar, Alexandre Truong, Quentin Monnet,
	William Cohen, Andres Freund, Adrian Hunter, Martin Liška,
	Colin Ian King, James Clark, Fangrui Song, Stephane Eranian,
	Kajol Jain, Alexey Bayduraev, Riccardo Mancini, Andi Kleen,
	Masami Hiramatsu, Zechuan Chen, Jason Wang, Christophe JAILLET,
	Remi Bernon, linux-kernel, linux-perf-users, bpf, llvm
  Cc: Ian Rogers

pthread.h is being included for the side-effect of getting sched.h and
macros like CPU_CLR. Switch to directly using sched.h, or if that is
already present, just remove the pthread.h inclusion entirely.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/tests/mmap-basic.c              | 2 --
 tools/perf/tests/openat-syscall-all-cpus.c | 2 +-
 tools/perf/tests/perf-record.c             | 2 --
 3 files changed, 1 insertion(+), 5 deletions(-)

diff --git a/tools/perf/tests/mmap-basic.c b/tools/perf/tests/mmap-basic.c
index dfb6173b2a82..21b5e68179d7 100644
--- a/tools/perf/tests/mmap-basic.c
+++ b/tools/perf/tests/mmap-basic.c
@@ -1,8 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <errno.h>
 #include <inttypes.h>
-/* For the CLR_() macros */
-#include <pthread.h>
 #include <stdlib.h>
 #include <perf/cpumap.h>
 
diff --git a/tools/perf/tests/openat-syscall-all-cpus.c b/tools/perf/tests/openat-syscall-all-cpus.c
index 90828ae03ef5..f3275be83a33 100644
--- a/tools/perf/tests/openat-syscall-all-cpus.c
+++ b/tools/perf/tests/openat-syscall-all-cpus.c
@@ -2,7 +2,7 @@
 #include <errno.h>
 #include <inttypes.h>
 /* For the CPU_* macros */
-#include <pthread.h>
+#include <sched.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
diff --git a/tools/perf/tests/perf-record.c b/tools/perf/tests/perf-record.c
index 6a001fcfed68..b386ade9ed06 100644
--- a/tools/perf/tests/perf-record.c
+++ b/tools/perf/tests/perf-record.c
@@ -2,8 +2,6 @@
 #include <errno.h>
 #include <inttypes.h>
 #include <linux/string.h>
-/* For the CLR_() macros */
-#include <pthread.h>
 
 #include <sched.h>
 #include <perf/mmap.h>
-- 
2.37.2.672.g94769d06f0-goog


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v4 04/18] perf hist: Update use of pthread mutex
  2022-08-26 16:40 [PATCH v4 00/18] Mutex wrapper, locking and memory leak fixes Ian Rogers
                   ` (2 preceding siblings ...)
  2022-08-26 16:40 ` [PATCH v4 03/18] perf tests: Avoid pthread.h inclusion Ian Rogers
@ 2022-08-26 16:40 ` Ian Rogers
  2022-08-26 16:40 ` [PATCH v4 05/18] perf bpf: Remove unused pthread.h include Ian Rogers
  2022-08-27  7:04 ` [PATCH v4 00/18] Mutex wrapper, locking and memory leak fixes Adrian Hunter
  5 siblings, 0 replies; 11+ messages in thread
From: Ian Rogers @ 2022-08-26 16:40 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Thomas Gleixner, Darren Hart, Davidlohr Bueso,
	André Almeida, Nathan Chancellor, Nick Desaulniers, Tom Rix,
	Weiguo Li, Athira Rajeev, Thomas Richter, Ravi Bangoria,
	Dario Petrillo, Hewenliang, yaowenbin, Wenyu Liu, Song Liu,
	Andrii Nakryiko, Dave Marchevsky, Leo Yan, Kim Phillips,
	Pavithra Gurushankar, Alexandre Truong, Quentin Monnet,
	William Cohen, Andres Freund, Adrian Hunter, Martin Liška,
	Colin Ian King, James Clark, Fangrui Song, Stephane Eranian,
	Kajol Jain, Alexey Bayduraev, Riccardo Mancini, Andi Kleen,
	Masami Hiramatsu, Zechuan Chen, Jason Wang, Christophe JAILLET,
	Remi Bernon, linux-kernel, linux-perf-users, bpf, llvm
  Cc: Ian Rogers

Switch to the use of mutex wrappers that provide better error checking.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-top.c | 8 ++++----
 tools/perf/util/hist.c   | 6 +++---
 tools/perf/util/hist.h   | 4 ++--
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index fd8fd913c533..14e60f6f219c 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -220,7 +220,7 @@ static void perf_top__record_precise_ip(struct perf_top *top,
 		 * This function is now called with he->hists->lock held.
 		 * Release it before going to sleep.
 		 */
-		pthread_mutex_unlock(&he->hists->lock);
+		mutex_unlock(&he->hists->lock);
 
 		if (err == -ERANGE && !he->ms.map->erange_warned)
 			ui__warn_map_erange(he->ms.map, sym, ip);
@@ -230,7 +230,7 @@ static void perf_top__record_precise_ip(struct perf_top *top,
 			sleep(1);
 		}
 
-		pthread_mutex_lock(&he->hists->lock);
+		mutex_lock(&he->hists->lock);
 	}
 }
 
@@ -836,12 +836,12 @@ static void perf_event__process_sample(struct perf_tool *tool,
 		else
 			iter.ops = &hist_iter_normal;
 
-		pthread_mutex_lock(&hists->lock);
+		mutex_lock(&hists->lock);
 
 		if (hist_entry_iter__add(&iter, &al, top->max_stack, top) < 0)
 			pr_err("Problem incrementing symbol period, skipping event\n");
 
-		pthread_mutex_unlock(&hists->lock);
+		mutex_unlock(&hists->lock);
 	}
 
 	addr_location__put(&al);
diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
index 1c085ab56534..698add038cec 100644
--- a/tools/perf/util/hist.c
+++ b/tools/perf/util/hist.c
@@ -1622,13 +1622,13 @@ struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists)
 {
 	struct rb_root_cached *root;
 
-	pthread_mutex_lock(&hists->lock);
+	mutex_lock(&hists->lock);
 
 	root = hists->entries_in;
 	if (++hists->entries_in > &hists->entries_in_array[1])
 		hists->entries_in = &hists->entries_in_array[0];
 
-	pthread_mutex_unlock(&hists->lock);
+	mutex_unlock(&hists->lock);
 
 	return root;
 }
@@ -2805,7 +2805,7 @@ int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
 	hists->entries_in = &hists->entries_in_array[0];
 	hists->entries_collapsed = RB_ROOT_CACHED;
 	hists->entries = RB_ROOT_CACHED;
-	pthread_mutex_init(&hists->lock, NULL);
+	mutex_init(&hists->lock);
 	hists->socket_filter = -1;
 	hists->hpp_list = hpp_list;
 	INIT_LIST_HEAD(&hists->hpp_formats);
diff --git a/tools/perf/util/hist.h b/tools/perf/util/hist.h
index 7ed4648d2fc2..508428b2c1b2 100644
--- a/tools/perf/util/hist.h
+++ b/tools/perf/util/hist.h
@@ -4,10 +4,10 @@
 
 #include <linux/rbtree.h>
 #include <linux/types.h>
-#include <pthread.h>
 #include "evsel.h"
 #include "color.h"
 #include "events_stats.h"
+#include "mutex.h"
 
 struct hist_entry;
 struct hist_entry_ops;
@@ -98,7 +98,7 @@ struct hists {
 	const struct dso	*dso_filter;
 	const char		*uid_filter_str;
 	const char		*symbol_filter_str;
-	pthread_mutex_t		lock;
+	struct mutex		lock;
 	struct hists_stats	stats;
 	u64			event_stream;
 	u16			col_len[HISTC_NR_COLS];
-- 
2.37.2.672.g94769d06f0-goog


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v4 05/18] perf bpf: Remove unused pthread.h include
  2022-08-26 16:40 [PATCH v4 00/18] Mutex wrapper, locking and memory leak fixes Ian Rogers
                   ` (3 preceding siblings ...)
  2022-08-26 16:40 ` [PATCH v4 04/18] perf hist: Update use of pthread mutex Ian Rogers
@ 2022-08-26 16:40 ` Ian Rogers
  2022-08-27  7:04 ` [PATCH v4 00/18] Mutex wrapper, locking and memory leak fixes Adrian Hunter
  5 siblings, 0 replies; 11+ messages in thread
From: Ian Rogers @ 2022-08-26 16:40 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Thomas Gleixner, Darren Hart, Davidlohr Bueso,
	André Almeida, Nathan Chancellor, Nick Desaulniers, Tom Rix,
	Weiguo Li, Athira Rajeev, Thomas Richter, Ravi Bangoria,
	Dario Petrillo, Hewenliang, yaowenbin, Wenyu Liu, Song Liu,
	Andrii Nakryiko, Dave Marchevsky, Leo Yan, Kim Phillips,
	Pavithra Gurushankar, Alexandre Truong, Quentin Monnet,
	William Cohen, Andres Freund, Adrian Hunter, Martin Liška,
	Colin Ian King, James Clark, Fangrui Song, Stephane Eranian,
	Kajol Jain, Alexey Bayduraev, Riccardo Mancini, Andi Kleen,
	Masami Hiramatsu, Zechuan Chen, Jason Wang, Christophe JAILLET,
	Remi Bernon, linux-kernel, linux-perf-users, bpf, llvm
  Cc: Ian Rogers

No pthread usage in bpf-event.h.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/bpf-event.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/tools/perf/util/bpf-event.h b/tools/perf/util/bpf-event.h
index 144a8a24cc69..1bcbd4fb6c66 100644
--- a/tools/perf/util/bpf-event.h
+++ b/tools/perf/util/bpf-event.h
@@ -4,7 +4,6 @@
 
 #include <linux/compiler.h>
 #include <linux/rbtree.h>
-#include <pthread.h>
 #include <api/fd/array.h>
 #include <stdio.h>
 
-- 
2.37.2.672.g94769d06f0-goog


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 00/18] Mutex wrapper, locking and memory leak fixes
  2022-08-26 16:40 [PATCH v4 00/18] Mutex wrapper, locking and memory leak fixes Ian Rogers
                   ` (4 preceding siblings ...)
  2022-08-26 16:40 ` [PATCH v4 05/18] perf bpf: Remove unused pthread.h include Ian Rogers
@ 2022-08-27  7:04 ` Adrian Hunter
  2022-08-27 15:18   ` Arnaldo Carvalho de Melo
  5 siblings, 1 reply; 11+ messages in thread
From: Adrian Hunter @ 2022-08-27  7:04 UTC (permalink / raw)
  To: Ian Rogers, Peter Zijlstra, Ingo Molnar,
	Arnaldo Carvalho de Melo, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, Thomas Gleixner, Darren Hart,
	Davidlohr Bueso, André Almeida, Nathan Chancellor,
	Nick Desaulniers, Tom Rix, Weiguo Li, Athira Rajeev,
	Thomas Richter, Ravi Bangoria, Dario Petrillo, Hewenliang,
	yaowenbin, Wenyu Liu, Song Liu, Andrii Nakryiko, Dave Marchevsky,
	Leo Yan, Kim Phillips, Pavithra Gurushankar, Alexandre Truong,
	Quentin Monnet, William Cohen, Andres Freund, Martin Liška,
	Colin Ian King, James Clark, Fangrui Song, Stephane Eranian,
	Kajol Jain, Alexey Bayduraev, Riccardo Mancini, Andi Kleen,
	Masami Hiramatsu, Zechuan Chen, Jason Wang, Christophe JAILLET,
	Remi Bernon, linux-kernel, linux-perf-users, bpf, llvm

On 26/08/22 19:40, Ian Rogers wrote:
> When fixing a locking race and memory leak in:
> https://lore.kernel.org/linux-perf-users/20211118193714.2293728-1-irogers@google.com/
> 
> It was requested that debug mutex code be separated out into its own
> files. This was, in part, done by Pavithra Gurushankar in:
> https://lore.kernel.org/lkml/20220727111954.105118-1-gpavithrasha@gmail.com/
> 
> These patches fix issues with the previous patches, add in the
> original dso->nsinfo fix and then build on our mutex wrapper with
> clang's -Wthread-safety analysis. The analysis found missing unlocks
> in builtin-sched.c which are fixed and -Wthread-safety is enabled by
> default when building with clang.
> 
> v4. Adds a comment for the trylock result, fixes the new line (missed
>     in v3) and removes two blank lines as suggested by Adrian Hunter.
> v3. Adds a missing new line to the error messages and removes the
>     pshared argument to mutex_init by having two functions, mutex_init
>     and mutex_init_pshared. These changes were suggested by Adrian Hunter.
> v2. Breaks apart changes that s/pthread_mutex/mutex/g and the lock
>     annotations as requested by Arnaldo and Namhyung. A boolean is
>     added to builtin-sched.c to terminate thread funcs rather than
>     leaving them blocked on delted mutexes.
> 
> Ian Rogers (17):
>   perf bench: Update use of pthread mutex/cond
>   perf tests: Avoid pthread.h inclusion
>   perf hist: Update use of pthread mutex
>   perf bpf: Remove unused pthread.h include
>   perf lock: Remove unused pthread.h include
>   perf record: Update use of pthread mutex
>   perf sched: Update use of pthread mutex
>   perf ui: Update use of pthread mutex
>   perf mmap: Remove unnecessary pthread.h include
>   perf dso: Update use of pthread mutex
>   perf annotate: Update use of pthread mutex
>   perf top: Update use of pthread mutex
>   perf dso: Hold lock when accessing nsinfo
>   perf mutex: Add thread safety annotations
>   perf sched: Fixes for thread safety analysis
>   perf top: Fixes for thread safety analysis
>   perf build: Enable -Wthread-safety with clang
> 
> Pavithra Gurushankar (1):
>   perf mutex: Wrapped usage of mutex and cond

For all patches except 9 and 16

Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 00/18] Mutex wrapper, locking and memory leak fixes
  2022-08-27  7:04 ` [PATCH v4 00/18] Mutex wrapper, locking and memory leak fixes Adrian Hunter
@ 2022-08-27 15:18   ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 11+ messages in thread
From: Arnaldo Carvalho de Melo @ 2022-08-27 15:18 UTC (permalink / raw)
  To: Adrian Hunter
  Cc: Ian Rogers, Peter Zijlstra, Ingo Molnar, Mark Rutland,
	Alexander Shishkin, Jiri Olsa, Namhyung Kim, Thomas Gleixner,
	Darren Hart, Davidlohr Bueso, André Almeida,
	Nathan Chancellor, Nick Desaulniers, Tom Rix, Weiguo Li,
	Athira Rajeev, Thomas Richter, Ravi Bangoria, Dario Petrillo,
	Hewenliang, yaowenbin, Wenyu Liu, Song Liu, Andrii Nakryiko,
	Dave Marchevsky, Leo Yan, Kim Phillips, Pavithra Gurushankar,
	Alexandre Truong, Quentin Monnet, William Cohen, Andres Freund,
	Martin Liška, Colin Ian King, James Clark, Fangrui Song,
	Stephane Eranian, Kajol Jain, Alexey Bayduraev, Riccardo Mancini,
	Andi Kleen, Masami Hiramatsu, Zechuan Chen, Jason Wang,
	Christophe JAILLET, Remi Bernon, linux-kernel, linux-perf-users,
	bpf, llvm

Em Sat, Aug 27, 2022 at 10:04:44AM +0300, Adrian Hunter escreveu:
> On 26/08/22 19:40, Ian Rogers wrote:
> > When fixing a locking race and memory leak in:
> > https://lore.kernel.org/linux-perf-users/20211118193714.2293728-1-irogers@google.com/
> > 
> > It was requested that debug mutex code be separated out into its own
> > files. This was, in part, done by Pavithra Gurushankar in:
> > https://lore.kernel.org/lkml/20220727111954.105118-1-gpavithrasha@gmail.com/
> > 
> > These patches fix issues with the previous patches, add in the
> > original dso->nsinfo fix and then build on our mutex wrapper with
> > clang's -Wthread-safety analysis. The analysis found missing unlocks
> > in builtin-sched.c which are fixed and -Wthread-safety is enabled by
> > default when building with clang.
> > 
> > v4. Adds a comment for the trylock result, fixes the new line (missed
> >     in v3) and removes two blank lines as suggested by Adrian Hunter.
> > v3. Adds a missing new line to the error messages and removes the
> >     pshared argument to mutex_init by having two functions, mutex_init
> >     and mutex_init_pshared. These changes were suggested by Adrian Hunter.
> > v2. Breaks apart changes that s/pthread_mutex/mutex/g and the lock
> >     annotations as requested by Arnaldo and Namhyung. A boolean is
> >     added to builtin-sched.c to terminate thread funcs rather than
> >     leaving them blocked on delted mutexes.
> > 
> > Ian Rogers (17):
> >   perf bench: Update use of pthread mutex/cond
> >   perf tests: Avoid pthread.h inclusion
> >   perf hist: Update use of pthread mutex
> >   perf bpf: Remove unused pthread.h include
> >   perf lock: Remove unused pthread.h include
> >   perf record: Update use of pthread mutex
> >   perf sched: Update use of pthread mutex
> >   perf ui: Update use of pthread mutex
> >   perf mmap: Remove unnecessary pthread.h include
> >   perf dso: Update use of pthread mutex
> >   perf annotate: Update use of pthread mutex
> >   perf top: Update use of pthread mutex
> >   perf dso: Hold lock when accessing nsinfo
> >   perf mutex: Add thread safety annotations
> >   perf sched: Fixes for thread safety analysis
> >   perf top: Fixes for thread safety analysis
> >   perf build: Enable -Wthread-safety with clang
> > 
> > Pavithra Gurushankar (1):
> >   perf mutex: Wrapped usage of mutex and cond
> 
> For all patches except 9 and 16
> 
> Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>

Added it, did the usual round of tests and pushed out perf/core, we can
continue from there.

It also contains Adrian's "multiple recording time ranges" series.

Thanks!

- Arnaldo

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 00/18] Mutex wrapper, locking and memory leak fixes
  2022-08-26 19:08 ` Namhyung Kim
@ 2022-08-26 19:21   ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 11+ messages in thread
From: Arnaldo Carvalho de Melo @ 2022-08-26 19:21 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Ian Rogers, Peter Zijlstra, Ingo Molnar, Mark Rutland,
	Alexander Shishkin, Jiri Olsa, Thomas Gleixner, Darren Hart,
	Davidlohr Bueso, André Almeida, Nathan Chancellor,
	Nick Desaulniers, Tom Rix, Weiguo Li, Athira Rajeev,
	Thomas Richter, Ravi Bangoria, Dario Petrillo, Hewenliang,
	yaowenbin, Wenyu Liu, Song Liu, Andrii Nakryiko, Dave Marchevsky,
	Leo Yan, Kim Phillips, Pavithra Gurushankar, Alexandre Truong,
	Quentin Monnet, William Cohen, Andres Freund, Adrian Hunter,
	Martin Liška, Colin Ian King, James Clark, Fangrui Song,
	Stephane Eranian, Kajol Jain, Alexey Bayduraev, Riccardo Mancini,
	Andi Kleen, Masami Hiramatsu, Zechuan Chen, Jason Wang,
	Christophe JAILLET, Remi Bernon, linux-kernel, linux-perf-users,
	bpf, llvm

Em Fri, Aug 26, 2022 at 12:08:06PM -0700, Namhyung Kim escreveu:
> On Fri, Aug 26, 2022 at 9:42 AM Ian Rogers <irogers@google.com> wrote:
> >
> > When fixing a locking race and memory leak in:
> > https://lore.kernel.org/linux-perf-users/20211118193714.2293728-1-irogers@google.com/
> >
> > It was requested that debug mutex code be separated out into its own
> > files. This was, in part, done by Pavithra Gurushankar in:
> > https://lore.kernel.org/lkml/20220727111954.105118-1-gpavithrasha@gmail.com/
> >
> > These patches fix issues with the previous patches, add in the
> > original dso->nsinfo fix and then build on our mutex wrapper with
> > clang's -Wthread-safety analysis. The analysis found missing unlocks
> > in builtin-sched.c which are fixed and -Wthread-safety is enabled by
> > default when building with clang.
> >
> > v4. Adds a comment for the trylock result, fixes the new line (missed
> >     in v3) and removes two blank lines as suggested by Adrian Hunter.
> > v3. Adds a missing new line to the error messages and removes the
> >     pshared argument to mutex_init by having two functions, mutex_init
> >     and mutex_init_pshared. These changes were suggested by Adrian Hunter.
> > v2. Breaks apart changes that s/pthread_mutex/mutex/g and the lock
> >     annotations as requested by Arnaldo and Namhyung. A boolean is
> >     added to builtin-sched.c to terminate thread funcs rather than
> >     leaving them blocked on delted mutexes.
> >
> > Ian Rogers (17):
> >   perf bench: Update use of pthread mutex/cond
> >   perf tests: Avoid pthread.h inclusion
> >   perf hist: Update use of pthread mutex
> >   perf bpf: Remove unused pthread.h include
> >   perf lock: Remove unused pthread.h include
> >   perf record: Update use of pthread mutex
> >   perf sched: Update use of pthread mutex
> >   perf ui: Update use of pthread mutex
> >   perf mmap: Remove unnecessary pthread.h include
> >   perf dso: Update use of pthread mutex
> >   perf annotate: Update use of pthread mutex
> >   perf top: Update use of pthread mutex
> >   perf dso: Hold lock when accessing nsinfo
> >   perf mutex: Add thread safety annotations
> >   perf sched: Fixes for thread safety analysis
> >   perf top: Fixes for thread safety analysis
> >   perf build: Enable -Wthread-safety with clang
> >
> > Pavithra Gurushankar (1):
> >   perf mutex: Wrapped usage of mutex and cond
> 
> For the patches 1-7 and 10-13
> 
> Acked-by: Namhyung Kim <namhyung@kernel.org>

Thanks added it, will try to get what is done so far merged and then we
can go on addressing the other issues brought up in this thread.

- Arnaldo

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 00/18] Mutex wrapper, locking and memory leak fixes
  2022-08-26 16:42 Ian Rogers
@ 2022-08-26 19:08 ` Namhyung Kim
  2022-08-26 19:21   ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 11+ messages in thread
From: Namhyung Kim @ 2022-08-26 19:08 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Thomas Gleixner,
	Darren Hart, Davidlohr Bueso, André Almeida,
	Nathan Chancellor, Nick Desaulniers, Tom Rix, Weiguo Li,
	Athira Rajeev, Thomas Richter, Ravi Bangoria, Dario Petrillo,
	Hewenliang, yaowenbin, Wenyu Liu, Song Liu, Andrii Nakryiko,
	Dave Marchevsky, Leo Yan, Kim Phillips, Pavithra Gurushankar,
	Alexandre Truong, Quentin Monnet, William Cohen, Andres Freund,
	Adrian Hunter, Martin Liška, Colin Ian King, James Clark,
	Fangrui Song, Stephane Eranian, Kajol Jain, Alexey Bayduraev,
	Riccardo Mancini, Andi Kleen, Masami Hiramatsu, Zechuan Chen,
	Jason Wang, Christophe JAILLET, Remi Bernon, linux-kernel,
	linux-perf-users, bpf, llvm

On Fri, Aug 26, 2022 at 9:42 AM Ian Rogers <irogers@google.com> wrote:
>
> When fixing a locking race and memory leak in:
> https://lore.kernel.org/linux-perf-users/20211118193714.2293728-1-irogers@google.com/
>
> It was requested that debug mutex code be separated out into its own
> files. This was, in part, done by Pavithra Gurushankar in:
> https://lore.kernel.org/lkml/20220727111954.105118-1-gpavithrasha@gmail.com/
>
> These patches fix issues with the previous patches, add in the
> original dso->nsinfo fix and then build on our mutex wrapper with
> clang's -Wthread-safety analysis. The analysis found missing unlocks
> in builtin-sched.c which are fixed and -Wthread-safety is enabled by
> default when building with clang.
>
> v4. Adds a comment for the trylock result, fixes the new line (missed
>     in v3) and removes two blank lines as suggested by Adrian Hunter.
> v3. Adds a missing new line to the error messages and removes the
>     pshared argument to mutex_init by having two functions, mutex_init
>     and mutex_init_pshared. These changes were suggested by Adrian Hunter.
> v2. Breaks apart changes that s/pthread_mutex/mutex/g and the lock
>     annotations as requested by Arnaldo and Namhyung. A boolean is
>     added to builtin-sched.c to terminate thread funcs rather than
>     leaving them blocked on delted mutexes.
>
> Ian Rogers (17):
>   perf bench: Update use of pthread mutex/cond
>   perf tests: Avoid pthread.h inclusion
>   perf hist: Update use of pthread mutex
>   perf bpf: Remove unused pthread.h include
>   perf lock: Remove unused pthread.h include
>   perf record: Update use of pthread mutex
>   perf sched: Update use of pthread mutex
>   perf ui: Update use of pthread mutex
>   perf mmap: Remove unnecessary pthread.h include
>   perf dso: Update use of pthread mutex
>   perf annotate: Update use of pthread mutex
>   perf top: Update use of pthread mutex
>   perf dso: Hold lock when accessing nsinfo
>   perf mutex: Add thread safety annotations
>   perf sched: Fixes for thread safety analysis
>   perf top: Fixes for thread safety analysis
>   perf build: Enable -Wthread-safety with clang
>
> Pavithra Gurushankar (1):
>   perf mutex: Wrapped usage of mutex and cond

For the patches 1-7 and 10-13

Acked-by: Namhyung Kim <namhyung@kernel.org>

Thanks,
Namhyung


>
>  tools/perf/Makefile.config                 |   5 +
>  tools/perf/bench/epoll-ctl.c               |  33 +++---
>  tools/perf/bench/epoll-wait.c              |  33 +++---
>  tools/perf/bench/futex-hash.c              |  33 +++---
>  tools/perf/bench/futex-lock-pi.c           |  33 +++---
>  tools/perf/bench/futex-requeue.c           |  33 +++---
>  tools/perf/bench/futex-wake-parallel.c     |  33 +++---
>  tools/perf/bench/futex-wake.c              |  33 +++---
>  tools/perf/bench/numa.c                    |  93 ++++++----------
>  tools/perf/builtin-inject.c                |   4 +
>  tools/perf/builtin-lock.c                  |   1 -
>  tools/perf/builtin-record.c                |  13 ++-
>  tools/perf/builtin-sched.c                 | 105 +++++++++---------
>  tools/perf/builtin-top.c                   |  45 ++++----
>  tools/perf/tests/mmap-basic.c              |   2 -
>  tools/perf/tests/openat-syscall-all-cpus.c |   2 +-
>  tools/perf/tests/perf-record.c             |   2 -
>  tools/perf/ui/browser.c                    |  20 ++--
>  tools/perf/ui/browsers/annotate.c          |  12 +--
>  tools/perf/ui/setup.c                      |   5 +-
>  tools/perf/ui/tui/helpline.c               |   5 +-
>  tools/perf/ui/tui/progress.c               |   8 +-
>  tools/perf/ui/tui/setup.c                  |   8 +-
>  tools/perf/ui/tui/util.c                   |  18 ++--
>  tools/perf/ui/ui.h                         |   4 +-
>  tools/perf/util/Build                      |   1 +
>  tools/perf/util/annotate.c                 |  15 +--
>  tools/perf/util/annotate.h                 |   4 +-
>  tools/perf/util/bpf-event.h                |   1 -
>  tools/perf/util/build-id.c                 |  12 ++-
>  tools/perf/util/dso.c                      |  19 ++--
>  tools/perf/util/dso.h                      |   4 +-
>  tools/perf/util/hist.c                     |   6 +-
>  tools/perf/util/hist.h                     |   4 +-
>  tools/perf/util/map.c                      |   3 +
>  tools/perf/util/mmap.h                     |   1 -
>  tools/perf/util/mutex.c                    | 119 +++++++++++++++++++++
>  tools/perf/util/mutex.h                    | 108 +++++++++++++++++++
>  tools/perf/util/probe-event.c              |   3 +
>  tools/perf/util/symbol.c                   |   4 +-
>  tools/perf/util/top.h                      |   5 +-
>  41 files changed, 569 insertions(+), 323 deletions(-)
>  create mode 100644 tools/perf/util/mutex.c
>  create mode 100644 tools/perf/util/mutex.h
>
> --
> 2.37.2.672.g94769d06f0-goog
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v4 00/18] Mutex wrapper, locking and memory leak fixes
@ 2022-08-26 16:42 Ian Rogers
  2022-08-26 19:08 ` Namhyung Kim
  0 siblings, 1 reply; 11+ messages in thread
From: Ian Rogers @ 2022-08-26 16:42 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Thomas Gleixner, Darren Hart, Davidlohr Bueso,
	André Almeida, Nathan Chancellor, Nick Desaulniers, Tom Rix,
	Weiguo Li, Athira Rajeev, Thomas Richter, Ravi Bangoria,
	Dario Petrillo, Hewenliang, yaowenbin, Wenyu Liu, Song Liu,
	Andrii Nakryiko, Dave Marchevsky, Leo Yan, Kim Phillips,
	Pavithra Gurushankar, Alexandre Truong, Quentin Monnet,
	William Cohen, Andres Freund, Adrian Hunter, Martin Liška,
	Colin Ian King, James Clark, Fangrui Song, Stephane Eranian,
	Kajol Jain, Alexey Bayduraev, Riccardo Mancini, Andi Kleen,
	Masami Hiramatsu, Zechuan Chen, Jason Wang, Christophe JAILLET,
	Remi Bernon, linux-kernel, linux-perf-users, bpf, llvm
  Cc: Ian Rogers

When fixing a locking race and memory leak in:
https://lore.kernel.org/linux-perf-users/20211118193714.2293728-1-irogers@google.com/

It was requested that debug mutex code be separated out into its own
files. This was, in part, done by Pavithra Gurushankar in:
https://lore.kernel.org/lkml/20220727111954.105118-1-gpavithrasha@gmail.com/

These patches fix issues with the previous patches, add in the
original dso->nsinfo fix and then build on our mutex wrapper with
clang's -Wthread-safety analysis. The analysis found missing unlocks
in builtin-sched.c which are fixed and -Wthread-safety is enabled by
default when building with clang.

v4. Adds a comment for the trylock result, fixes the new line (missed
    in v3) and removes two blank lines as suggested by Adrian Hunter.
v3. Adds a missing new line to the error messages and removes the
    pshared argument to mutex_init by having two functions, mutex_init
    and mutex_init_pshared. These changes were suggested by Adrian Hunter.
v2. Breaks apart changes that s/pthread_mutex/mutex/g and the lock
    annotations as requested by Arnaldo and Namhyung. A boolean is
    added to builtin-sched.c to terminate thread funcs rather than
    leaving them blocked on delted mutexes.

Ian Rogers (17):
  perf bench: Update use of pthread mutex/cond
  perf tests: Avoid pthread.h inclusion
  perf hist: Update use of pthread mutex
  perf bpf: Remove unused pthread.h include
  perf lock: Remove unused pthread.h include
  perf record: Update use of pthread mutex
  perf sched: Update use of pthread mutex
  perf ui: Update use of pthread mutex
  perf mmap: Remove unnecessary pthread.h include
  perf dso: Update use of pthread mutex
  perf annotate: Update use of pthread mutex
  perf top: Update use of pthread mutex
  perf dso: Hold lock when accessing nsinfo
  perf mutex: Add thread safety annotations
  perf sched: Fixes for thread safety analysis
  perf top: Fixes for thread safety analysis
  perf build: Enable -Wthread-safety with clang

Pavithra Gurushankar (1):
  perf mutex: Wrapped usage of mutex and cond

 tools/perf/Makefile.config                 |   5 +
 tools/perf/bench/epoll-ctl.c               |  33 +++---
 tools/perf/bench/epoll-wait.c              |  33 +++---
 tools/perf/bench/futex-hash.c              |  33 +++---
 tools/perf/bench/futex-lock-pi.c           |  33 +++---
 tools/perf/bench/futex-requeue.c           |  33 +++---
 tools/perf/bench/futex-wake-parallel.c     |  33 +++---
 tools/perf/bench/futex-wake.c              |  33 +++---
 tools/perf/bench/numa.c                    |  93 ++++++----------
 tools/perf/builtin-inject.c                |   4 +
 tools/perf/builtin-lock.c                  |   1 -
 tools/perf/builtin-record.c                |  13 ++-
 tools/perf/builtin-sched.c                 | 105 +++++++++---------
 tools/perf/builtin-top.c                   |  45 ++++----
 tools/perf/tests/mmap-basic.c              |   2 -
 tools/perf/tests/openat-syscall-all-cpus.c |   2 +-
 tools/perf/tests/perf-record.c             |   2 -
 tools/perf/ui/browser.c                    |  20 ++--
 tools/perf/ui/browsers/annotate.c          |  12 +--
 tools/perf/ui/setup.c                      |   5 +-
 tools/perf/ui/tui/helpline.c               |   5 +-
 tools/perf/ui/tui/progress.c               |   8 +-
 tools/perf/ui/tui/setup.c                  |   8 +-
 tools/perf/ui/tui/util.c                   |  18 ++--
 tools/perf/ui/ui.h                         |   4 +-
 tools/perf/util/Build                      |   1 +
 tools/perf/util/annotate.c                 |  15 +--
 tools/perf/util/annotate.h                 |   4 +-
 tools/perf/util/bpf-event.h                |   1 -
 tools/perf/util/build-id.c                 |  12 ++-
 tools/perf/util/dso.c                      |  19 ++--
 tools/perf/util/dso.h                      |   4 +-
 tools/perf/util/hist.c                     |   6 +-
 tools/perf/util/hist.h                     |   4 +-
 tools/perf/util/map.c                      |   3 +
 tools/perf/util/mmap.h                     |   1 -
 tools/perf/util/mutex.c                    | 119 +++++++++++++++++++++
 tools/perf/util/mutex.h                    | 108 +++++++++++++++++++
 tools/perf/util/probe-event.c              |   3 +
 tools/perf/util/symbol.c                   |   4 +-
 tools/perf/util/top.h                      |   5 +-
 41 files changed, 569 insertions(+), 323 deletions(-)
 create mode 100644 tools/perf/util/mutex.c
 create mode 100644 tools/perf/util/mutex.h

-- 
2.37.2.672.g94769d06f0-goog


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2022-08-27 15:18 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-26 16:40 [PATCH v4 00/18] Mutex wrapper, locking and memory leak fixes Ian Rogers
2022-08-26 16:40 ` [PATCH v4 01/18] perf mutex: Wrapped usage of mutex and cond Ian Rogers
2022-08-26 16:40 ` [PATCH v4 02/18] perf bench: Update use of pthread mutex/cond Ian Rogers
2022-08-26 16:40 ` [PATCH v4 03/18] perf tests: Avoid pthread.h inclusion Ian Rogers
2022-08-26 16:40 ` [PATCH v4 04/18] perf hist: Update use of pthread mutex Ian Rogers
2022-08-26 16:40 ` [PATCH v4 05/18] perf bpf: Remove unused pthread.h include Ian Rogers
2022-08-27  7:04 ` [PATCH v4 00/18] Mutex wrapper, locking and memory leak fixes Adrian Hunter
2022-08-27 15:18   ` Arnaldo Carvalho de Melo
2022-08-26 16:42 Ian Rogers
2022-08-26 19:08 ` Namhyung Kim
2022-08-26 19:21   ` Arnaldo Carvalho de Melo

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).