From patchwork Tue Aug 23 22:09:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952634 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C029AC32772 for ; Tue, 23 Aug 2022 22:09:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232025AbiHWWJn (ORCPT ); Tue, 23 Aug 2022 18:09:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231965AbiHWWJi (ORCPT ); Tue, 23 Aug 2022 18:09:38 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C3F172693 for ; Tue, 23 Aug 2022 15:09:36 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-31f5960500bso260525937b3.14 for ; Tue, 23 Aug 2022 15:09:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=HGXvwC70P4lQM2SClDMy1A450ci+Pd66+W81CaUVSbs=; b=YFJ+R6mwgQ9oKPaH6psZN8+PmZ8nr2tYvLI9upGLW9XRXR1kFqWJkfj4abLrpb3Hrc I968K2izzHbGreVMfH8Nre+zZcAEfu79EUU7ARSahNut/NPEWGTZ/0RJIpb9FQ4ZeExj mks3mn1D+5g4txFEgf+WJpGfe8nWo0W0SuDvtP5/bBfxdEDgVId5hifdMaH8iuAU9XB4 GeCtI9QzJZTsCskOkuJ2Ef7gOLeI9B615teO2AZ5TMbj/c7w3o9BzFvx1u6UjyzWkrlZ rF3g5v5vteJqRivGfJQ1Gpyv0cKcxn+Infe4UjnEBCp7GyYaoCfHNQM+d9iTVS4Si+CB RHIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=HGXvwC70P4lQM2SClDMy1A450ci+Pd66+W81CaUVSbs=; b=Ojxpn6XmvlcpfrFGPdDDIE3fzKiXPGlBAgPTUBTkUSiPYS2/aBRC0ybj/Tngf7hzgm u2wTKTFJQoOnWNS6mSpd/XKvQpOw8L4k4uzXlDoEh0BoCaErSM3eBtceZt2v0FiHXMLu /c8t34mR7YtHAKFkospSyas3F/m4kyp8UMBbjMpGbHX49wWFC4U4UGZ1P+tCTFM1Mw80 RY/UudhOq6purGIbI2sCrwvKhVgx+mWeRmnhPuXzABlnAEKDKao7bNqV5X390uwmo0Ep TsHHGvULSLmjlNvGbSpFkj6msypza9UYnWz2OM2nc6Ikp3nZ/7VVrQvPWxDfim+j9oW8 xDTA== X-Gm-Message-State: ACgBeo1JodZGV7fnNUkZX7z011Lz+Q5geRL0bNZcxbHMZElGD7T5Ll3N g8YOFUs3f7Hyhedk6GZBgN5xsUzGjJyH X-Google-Smtp-Source: AA6agR6wsVs9Ektezw4eqFarF6XaQ48d0ENVXVoZVMJxS1RzWjeg+uj9OG2/9mmpV9nP7+j6GnE/maf+tFs8 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a05:6902:1502:b0:695:a994:475c with SMTP id q2-20020a056902150200b00695a994475cmr11974185ybu.547.1661292575542; Tue, 23 Aug 2022 15:09:35 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:05 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-2-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 01/18] perf mutex: Wrapped usage of mutex and cond From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org From: Pavithra Gurushankar 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 Signed-off-by: Ian Rogers --- tools/perf/util/Build | 1 + tools/perf/util/mutex.c | 97 +++++++++++++++++++++++++++++++++++++++++ tools/perf/util/mutex.h | 43 ++++++++++++++++++ 3 files changed, 141 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..d12cf0714268 --- /dev/null +++ b/tools/perf/util/mutex.c @@ -0,0 +1,97 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "mutex.h" + +#include "debug.h" +#include +#include + +static void check_err(const char *fn, int err) +{ + char sbuf[STRERR_BUFSIZE]; + + if (err == 0) + return; + + pr_err("%s error: '%s'", fn, str_error_r(err, sbuf, sizeof(sbuf))); +} + +#define CHECK_ERR(err) check_err(__func__, err) + +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) + pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); + + CHECK_ERR(pthread_mutex_init(&mtx->lock, &attr)); + CHECK_ERR(pthread_mutexattr_destroy(&attr)); +} + +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; +} + +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_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..952276ad83bd --- /dev/null +++ b/tools/perf/util/mutex.h @@ -0,0 +1,43 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __PERF_MUTEX_H +#define __PERF_MUTEX_H + +#include +#include + +/* + * 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; +}; + +/* + * Initialize the mtx struct, if pshared is set then specify the process-shared + * rather than default process-private attribute. + */ +void mutex_init(struct mutex *mtx, bool pshared); +void mutex_destroy(struct mutex *mtx); + +void mutex_lock(struct mutex *mtx); +void mutex_unlock(struct mutex *mtx); +bool mutex_trylock(struct mutex *mtx); + +/* + * Initialize the cond struct, if pshared is set then specify the process-shared + * rather than default process-private attribute. + */ +void cond_init(struct cond *cnd, bool pshared); +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 */ From patchwork Tue Aug 23 22:09:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952635 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B38FAC32793 for ; Tue, 23 Aug 2022 22:09:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231972AbiHWWJx (ORCPT ); Tue, 23 Aug 2022 18:09:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232215AbiHWWJs (ORCPT ); Tue, 23 Aug 2022 18:09:48 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72BC072B7D for ; Tue, 23 Aug 2022 15:09:42 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-333b218f2cbso259785297b3.0 for ; Tue, 23 Aug 2022 15:09:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=2yMunCd23swtFtHtFTobwU8PN6huvkMRVNmVJbz5uDo=; b=WTBvoMJt3SrG9G3TPW6YXgHaQNeI/lOPPsdQS7dGQn/th+dg26bPoDStLmDXkaSReE HgMFq0QunOTwuIHjg6vRmpvvw8ZA6haI4uNOOdwsIlxvfabjRzftmXo51yntA6pZNSgF 4MOGesYEinvkY2xMKJ4V/YpTJokQDGL8r347kPuD1puBwmgAAuiDPTrs6VEWfhZqCk4L ksyn7OYuGc9ICBjtofrUqwH9rAalIdNC/7X/lIGtt4B+Pa1qYECH9ASZNwsMuy3KnVhU UWL/8bodlA03nhTAhtsV2wNLO/uRT7a6zN4htB/HLoignrB5s/hwea8L2Bz+V5CWrf+L VunA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=2yMunCd23swtFtHtFTobwU8PN6huvkMRVNmVJbz5uDo=; b=WhUK0J+JC5WrgH+mOh7CKB4KnDnQzOstE6kF2Nun4PWu5+r4Y9IDaQC5lvyC8LXLpy aktAKV77A+6gPZrro915F4hsO0CEG5J59DUfobieXwQmBDKzC1reWTeFnuX6D5n993Zt RW80f0jwSRoSD2YqJDCf00YXnafxR+3SxLU6DQ4pdSj3Joab6JrDpKYCqXO5Xrl5SYum Myb5eJ+079Ho3s3zZj0Ob2LSMn+t0SpDnpQ47DTK8WZgIKuaxoo/c8WW+OIx8Caxri2F 8huvQMjOJN9fSwmWcGYOGqf/f8iBJEW0IwoR9WAjXdzHUIyM/X/H+625YqZ/b8/wWFMx jZlg== X-Gm-Message-State: ACgBeo0v+sBHJqGZbwYYR+UGDD6VGNYOlFvkK1W2eWjOFx1G1J5E0/QC 8eXbldrxQA+rXPY/olcS/g3IijkxH+jH X-Google-Smtp-Source: AA6agR7hHXqxFQUuZeUzTJRNL63/FbZpi+j6/Z+IbWk6WyHbvFZbSlsb7BMKdZsNlo1nBgOcRVk40hqCIeMm X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a05:6902:2cb:b0:684:aebe:49ab with SMTP id w11-20020a05690202cb00b00684aebe49abmr26094341ybh.242.1661292581705; Tue, 23 Aug 2022 15:09:41 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:06 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-3-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 02/18] perf bench: Update use of pthread mutex/cond From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Switch to the use of mutex wrappers that provide better error checking. Signed-off-by: Ian Rogers --- 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..82309add47d1 100644 --- a/tools/perf/bench/epoll-ctl.c +++ b/tools/perf/bench/epoll-ctl.c @@ -23,6 +23,7 @@ #include #include +#include "../util/mutex.h" #include "../util/stat.h" #include #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, /*pshared=*/false); + cond_init(&thread_parent, /*pshared=*/false); + cond_init(&thread_worker, /*pshared=*/false); 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..2ed20c55105d 100644 --- a/tools/perf/bench/epoll-wait.c +++ b/tools/perf/bench/epoll-wait.c @@ -79,6 +79,7 @@ #include #include "../util/stat.h" +#include "../util/mutex.h" #include #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, /*pshared=*/false); + cond_init(&thread_parent, /*pshared=*/false); + cond_init(&thread_worker, /*pshared=*/false); 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..b564ff7ba9a0 100644 --- a/tools/perf/bench/futex-hash.c +++ b/tools/perf/bench/futex-hash.c @@ -23,6 +23,7 @@ #include #include +#include "../util/mutex.h" #include "../util/stat.h" #include #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, /*pshared=*/false); + cond_init(&thread_parent, /*pshared=*/false); + cond_init(&thread_worker, /*pshared=*/false); 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..106b5554030f 100644 --- a/tools/perf/bench/futex-lock-pi.c +++ b/tools/perf/bench/futex-lock-pi.c @@ -8,6 +8,7 @@ #include #include +#include "../util/mutex.h" #include "../util/stat.h" #include #include @@ -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, /*pshared=*/false); + cond_init(&thread_parent, /*pshared=*/false); + cond_init(&thread_worker, /*pshared=*/false); 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..d09509289f1d 100644 --- a/tools/perf/bench/futex-requeue.c +++ b/tools/perf/bench/futex-requeue.c @@ -15,6 +15,7 @@ #include #include +#include "../util/mutex.h" #include "../util/stat.h" #include #include @@ -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, /*pshared=*/false); + cond_init(&thread_parent, /*pshared=*/false); + cond_init(&thread_worker, /*pshared=*/false); 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..5eb2e5a2a813 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 #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, /*pshared=*/false); + cond_init(&thread_parent, /*pshared=*/false); + cond_init(&thread_worker, /*pshared=*/false); 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..eb161a755197 100644 --- a/tools/perf/bench/futex-wake.c +++ b/tools/perf/bench/futex-wake.c @@ -14,6 +14,7 @@ #include #include +#include "../util/mutex.h" #include "../util/stat.h" #include #include @@ -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, /*pshared=*/false); + cond_init(&thread_parent, /*pshared=*/false); + cond_init(&thread_worker, /*pshared=*/false); 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..934f1fb2d723 100644 --- a/tools/perf/bench/numa.c +++ b/tools/perf/bench/numa.c @@ -6,8 +6,6 @@ */ #include -/* For the CLR_() macros */ -#include #include #include "../util/cloexec.h" @@ -35,6 +33,7 @@ #include #include "../util/header.h" +#include "../util/mutex.h" #include #include @@ -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, /*pshared=*/false); 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(&g->start_work_mutex, /*pshared=*/true); + cond_init(&g->start_work_cond, /*pshared=*/true); + mutex_init(&g->startup_mutex, /*pshared=*/true); + cond_init(&g->startup_cond, /*pshared=*/true); + mutex_init(&g->stop_work_mutex, /*pshared=*/true); 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); } From patchwork Tue Aug 23 22:09:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952636 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7EA46C3F6B0 for ; Tue, 23 Aug 2022 22:10:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231965AbiHWWKH (ORCPT ); Tue, 23 Aug 2022 18:10:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232228AbiHWWJu (ORCPT ); Tue, 23 Aug 2022 18:09:50 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95F2C72EDF for ; Tue, 23 Aug 2022 15:09:48 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-3360c0f0583so260328877b3.2 for ; Tue, 23 Aug 2022 15:09:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=kkLvqnwJBxJuW02GoX/GzvRRDzNYFeMugLbS0btBnq0=; b=e6ngnHPDhkltUA2Qe5zmEdlTyuD45SX8Jsfw1VV8ZkmETjJDHgsgmaU6+Ii1Tg3TQm ll6jtu/Ru0rxjBk5HXYer0JDoY/Jv9/zt1vqYjvVySwZqIrk/1v/bCePVtmomAp30NS7 uUJKTpGi1ROCtcPCczPy9FJoZrr2+6LJ7As7L0TGaK/1FkAT5tPquWbKkWBKq1M1J/nj vGFBu47TxqL9MLW5W4UpXosmdKx6cCYepinl6LUyrwVr0NKjjPAbdFJZnRV0YE0c3hoZ W3C5etBA1zpWAnWavN+12y0MOD6vmTq2/yExS9MtTptgmRJutzfATnCnjwkDW1urvERj uwRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=kkLvqnwJBxJuW02GoX/GzvRRDzNYFeMugLbS0btBnq0=; b=dR6OmDca959uhbkjRTAmMD3Z783E7qzQLUwDEsRzPMu8yXROTUp15/u1sk9dUEVWF+ 05XfCMjE/4QkRBiO4tQsmGKVWSHWYfuRSJlg0deCEA5R6AIV2+B93D6e35i9DprcSxd0 q1HYokqDtU8eS3cjg+sJdsFjL9h0JvCmgTH/pmkHhf5CpcypokwvMQsHs2neue9KxKCZ YJ1SZktOxH3fXaCJoSwp8Z73s6g02gU0NHquu+zWF/S9pyyFaIxG9oRMOojJElgnDkTJ eL3sX1OLcgncYsG1KNcuzxsMnws2k+bY2aSoOl3xwSny+p05T/hJH4zaFZMRSqI3X2HU nssA== X-Gm-Message-State: ACgBeo3TD95U2JFTPxswrFz5Kp1p41qI2Sc1suXEiESocWMUQDSLOIRg i2UbVeSnWgp6N2gPWKyEJdjpvmoXyAmw X-Google-Smtp-Source: AA6agR6N8kt7AM2aQ94ryR8hNNMcF5mqi5Ag2ltB36PH3t4SgS+qEZg78HFwWstkZXFHdAlmPQ08L45AqAGN X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a0d:eb45:0:b0:333:f813:6c79 with SMTP id u66-20020a0deb45000000b00333f8136c79mr28058674ywe.384.1661292587561; Tue, 23 Aug 2022 15:09:47 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:07 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-4-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 03/18] perf tests: Avoid pthread.h inclusion From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org 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 --- 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 #include -/* For the CLR_() macros */ -#include #include #include 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 #include /* For the CPU_* macros */ -#include +#include #include #include 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 #include #include -/* For the CLR_() macros */ -#include #include #include From patchwork Tue Aug 23 22:09:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952637 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 34AEDC32772 for ; Tue, 23 Aug 2022 22:10:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232021AbiHWWKM (ORCPT ); Tue, 23 Aug 2022 18:10:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232007AbiHWWKF (ORCPT ); Tue, 23 Aug 2022 18:10:05 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E685D74B81 for ; Tue, 23 Aug 2022 15:09:53 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-33580e26058so260607257b3.4 for ; Tue, 23 Aug 2022 15:09:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=1eSbxV8xsUDpc2lqCmn8NmCq3Cnp8BvOk6OZp7THu6w=; b=n2OTj1VLp6o80LdM8jnNvYoBYsCyZzoBStuGJULDdMtK0CWmBVdgrwHF1OaGt+Qwi5 eRuRw8mi7HBI8UOxklnAv/41dyMbKaRy1Ta8W9ULGpyZVcLLTz3DwtDqd67GTyKGWGNc UtgVw7As6Jm1/J3mUL9fyRwlIIauby2UvWTWfwctcgAHWhKndmG6tcCi3ocg+hN5MBHD 7PrzHzK4NfxF67H7EHTvglpl3oGvbpj+O2s9rEBxedMdYyfuoSgT7++ssJ87Cs60V+kJ 1STe+qDjpbrUAdHdO2E34BmTFGbwg/ny4RW9A/kENMkvcl3uei22V7ZagcnPlJSMxpzh H0zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=1eSbxV8xsUDpc2lqCmn8NmCq3Cnp8BvOk6OZp7THu6w=; b=d6CE9oZOgf/jUATNamODGYHoetbXBFTSEBUG+XfDu6VIGZ1UQK1DrwJP9DQMVlvYjj y8QTitzJl/Aj3GubfxYasntcUTUsZMb8Hc/tiFbh6+m00C60vYsCJTXLodDVKjhbfG2v sriy+OHSN0rHs2xlgD4bvvby9P51ddDvUPlOTO5O8glnhf27WEbtoyumgmkqdjLeGElz luziRzkCuT/st2jLIHxynrMVtT0n4wHMyibab8F5+GD6mGO3I5EYLRe/jeh0X4NQKx2M 8ZXykg2ZsTSBXkW8ZK56F1X0gP3UcplMkxMMrZ0tB1BG7T6lDBvmt/2khnBqyBw7JRcZ n1mw== X-Gm-Message-State: ACgBeo0FqvHvwSIiVRBZCTqeRKlgOxpvzXC0ci5lUVzSnhufBMftJF9r GvV1fZz0f0Ff0fvtCCL4gixrCWBw5OwA X-Google-Smtp-Source: AA6agR5NQD+yIEJJ17qmVE5UxbRtTg+1QVTswl9OUhRV+oUsoSDQ/p9MEH1i7Y0xqkhVQ9084bu0eUFCQccE X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a5b:24c:0:b0:68f:b0d7:79d9 with SMTP id g12-20020a5b024c000000b0068fb0d779d9mr24803235ybp.26.1661292593105; Tue, 23 Aug 2022 15:09:53 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:08 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-5-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 04/18] perf hist: Update use of pthread mutex From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Switch to the use of mutex wrappers that provide better error checking. Signed-off-by: Ian Rogers --- 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..bfce88e5eb0d 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, /*pshared=*/false); 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 #include -#include #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]; From patchwork Tue Aug 23 22:09:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952638 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4659DC32772 for ; Tue, 23 Aug 2022 22:10:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232411AbiHWWKl (ORCPT ); Tue, 23 Aug 2022 18:10:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232490AbiHWWKL (ORCPT ); Tue, 23 Aug 2022 18:10:11 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1FDAC7549C for ; Tue, 23 Aug 2022 15:10:00 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-334d894afd8so261949977b3.19 for ; Tue, 23 Aug 2022 15:09:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=gT8uGhkPz2QVnzj86f2E281NRyIthHuKkag96DoN/yw=; b=HwVO7m10rOh/3z2v+99/vN50dBJN/AH7VMgWiBoZmpDohvsq0tBv5/cTMrlymx/MOp NzjiIFBvO3kC483MX3eO1YhzMufbSi8rArxF3hwBN+PRxCigNMkJbuHJp1BpgAsYcGtK xMMP9GOnGVEC4usMZ0z8NM9+PnyrOkUSCUhuTQTwIlFDxLmDOuqdmfqRQejdBzQCpOHP RuKH3kz+Nc+u5yio8jwIrQRyc+WxLBygENFC3jryDaUBwb3cm76KzzVe7qDK2mur2ERK vY4M/iXJCWauvG638kIRnVjLoGxefxSYN1WBVRPEmNHY21KRJo6fLu7Vs2S8ZGuZ84Pz raYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=gT8uGhkPz2QVnzj86f2E281NRyIthHuKkag96DoN/yw=; b=IejKy0v+qiCjIT1D6Q3JVi6Qs+lym7Uyo/H0ZUD3z0Ur5K23g2+QfTRlWcDY8KlxqD lBZ01hlmvxHrVACdkH8lddvQMbyaSArE934RbShsI83v8q17WTNH0otAdhM1/P+ToKDl y63klqaqmYGxbvMNZ2iZIPjrNsm9DRGQPY00BRGbUmKPaWp3lnkIHHsfUOtBmwzp00ba DyL+Iw+L6xPHbIU2CDEFM0OxytB/DF23GRDo7zBOgNXRzcnI5UCTRh8+aCIwmTCyUhmQ V1cCv7NBzM5lBrhzUI+hN8VrOWc2P1bs6pbElxprSt1Podl1Birwc42a0nTBlEyEVg/j 5BNA== X-Gm-Message-State: ACgBeo1bWFneLBWu5jiu8CUG6oGzHUGf3vjTdgJzYFdGR58liJaBBoR8 O4QhGfTTIW3bq/lw08OZzaVFAQIBHcWX X-Google-Smtp-Source: AA6agR6oFWKl1LddABFgQgXo6ykLQYEDKjNJ/iL3zLeafkrEI61E24RPG1snXZgYhRAfWO0hv9++V5PTY/iq X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a81:8003:0:b0:336:be9e:df56 with SMTP id q3-20020a818003000000b00336be9edf56mr27645979ywf.92.1661292599033; Tue, 23 Aug 2022 15:09:59 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:09 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-6-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 05/18] perf bpf: Remove unused pthread.h include From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org No pthread usage in bpf-event.h. Signed-off-by: Ian Rogers --- 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 #include -#include #include #include From patchwork Tue Aug 23 22:09:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952639 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 97E5BC38142 for ; Tue, 23 Aug 2022 22:10:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232503AbiHWWKx (ORCPT ); Tue, 23 Aug 2022 18:10:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232373AbiHWWKX (ORCPT ); Tue, 23 Aug 2022 18:10:23 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D686E73332 for ; Tue, 23 Aug 2022 15:10:06 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-334f49979a0so264120487b3.10 for ; Tue, 23 Aug 2022 15:10:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=TZmRLUQ2d7TXmU69l5rNy1CTfW+D1QluUT9t1SCPaRQ=; b=XJdlmzgS/G5JBfz64SCGEvKo+qmDECH61tMA+nVEkvkjhvqErPn6we7JRZ3YTRjB9g sZBj7cabo5j8BoTz70VIH1o6q4J/BmSbQiX/1GZaCFQbtR4OR6jl27Qcflg0ASIvB2pn cXj5qngzepTVIfzGx+7wKUSjI+RWPVw7p6wOSURROtcw0Zz2mRmIVcYW30gyI6oDVuyA rBtwoxSBeWs6jGTk64xQrPsqirJPXfAZXGzwIlb3ZDEFqKuf8SnodQ3HfgnJNV6AN+KX Tpyp2Bq4TqzFSAROlx6kOHFRjKK0wTDXetElB7AzefGaOTXHAp8BURfCT5TkmiA8+IhR /ikA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=TZmRLUQ2d7TXmU69l5rNy1CTfW+D1QluUT9t1SCPaRQ=; b=g+iXaR4x9nH5ZOkt9M6DFujloyskaNrsZudz3SDxJ5OmkjDC36E46Mc98tS505vnM/ 2nhtMywrHhEtqrUq4dHlnbWimNgJfGhJJQHQGcw49mtS7C2ut53SlfQ3lr9LoTm0CA/5 3d+YyIzaqztvOHr7KCMt9LV53oprn8kjBgcJ9iF79+ku5hstqdlDw2RLW1sIhlsJuIcO wkJsTH+DzC9I3silHY2TsUniMZoQ6C2qWsiblu15OrSMZN55zfv8CT2FAztJ3l1uCays w3RM+CkbRXBJBoxDEfsVFoKbbnmosXcYD3YT7oo+z89rFIm7TPe9S91SaDT6GOdOQ3bh gdHw== X-Gm-Message-State: ACgBeo39rRp9EWcNc6+Bov5awRjWgemjU8rUHyOH5niJe5U0KHQFTGY0 mw6Q19mdJ5NVSjjPyp+SgJ5YVUxRW5O9 X-Google-Smtp-Source: AA6agR7a0VTxMqTg4C3l82b1BpKQRn7OO5P4Ch8FMwflD77Z6ASQKHyZ78AkmQHPc0XrIuaejBrso+emaXu2 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a25:2493:0:b0:695:839:3a5a with SMTP id k141-20020a252493000000b0069508393a5amr21422493ybk.468.1661292605692; Tue, 23 Aug 2022 15:10:05 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:10 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-7-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 06/18] perf lock: Remove unused pthread.h include From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org No pthread usage in builtin-lock.c. Signed-off-by: Ian Rogers --- tools/perf/builtin-lock.c | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index dd11d3471baf..70197c0593b1 100644 --- a/tools/perf/builtin-lock.c +++ b/tools/perf/builtin-lock.c @@ -28,7 +28,6 @@ #include #include #include -#include #include #include From patchwork Tue Aug 23 22:09:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952640 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 41791C32772 for ; Tue, 23 Aug 2022 22:11:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230236AbiHWWLH (ORCPT ); Tue, 23 Aug 2022 18:11:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232878AbiHWWKc (ORCPT ); Tue, 23 Aug 2022 18:10:32 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BB7175FD4 for ; Tue, 23 Aug 2022 15:10:12 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-33931a5c133so175242227b3.17 for ; Tue, 23 Aug 2022 15:10:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=dmh/92MRm7j95zVGcUZ890xL7HIjX5BjkDj+86ZwSV4=; b=BqrCSk/jfE2Eg/kfl3hCAbhvBnIz/GqHBYIYJLFqyhBi497Ll9GGI+raBBPi7M+8Ib 99+xF9k5162F1hpJBqyAcw/RX+5CtTsRAEwHHOB6NUTG3zVhdWd5fPjWDSjJW3Hm0Fvp wQbdiz1uiBz2ncduZi2qpaSl3gV75mc+fyitmLVe680j7a8WEyipRhqLJSaSXlWZyMcy W6ejqPOQwH5fWVFTIaiPfZivm1L4HDYTazwFG937PO1YN9evW4HA5ZAIZube3cBEmhgR yLhvdJHy4Rh+rZbIu/zSzGtXs2aDe4hwA/RJkV583cWdSHgQ06NuAu6//viZN3vlrUV2 gDPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=dmh/92MRm7j95zVGcUZ890xL7HIjX5BjkDj+86ZwSV4=; b=IKvdIHJ9vOFZeFeA5BR/GJBmg4fNylupAo8ji5FBjgKIL/aI8zqG0DjOgrruBU690s +IrzcJ2/sJwU8yFv57OIlV1D7YaeQw50T0sQCyLpck/0ttBhpjM3XBSdYTf96yflqbYS 5b+zgK7ZcdThKiYMqsKluUvBzR2GPlW+uuzXSNZNqyS3shRjkew+eMu9pFmRdJEhvus1 bqhbDqM6yR0Qt3deYqJQ8w633VrrWev8oXrFMOv0QxVEhaxS9OQCCmXxEoE4CeclgY5K fmPleuLS4mlBytMn4X0Frl2Y+NN/RZ9iowKMCd101NesXGpRFbGMjIUry9l/oSyjyZu1 rZAw== X-Gm-Message-State: ACgBeo3R9XM0nWYQ3o6IbXm3X1ygGDeBoS/t0ypcoWdY6f6gFY6n7Tgy I10gT8O7DjSA+gNVtMAzgpAOH8yCYyC9 X-Google-Smtp-Source: AA6agR6BxJxr/i2FE3SRx08sPUB3srSjsfLBMouB5rTu5H1ZsvNLTfL/kFPNdM7yI+a6YV4HNJOiWFHE6X2R X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a81:90f:0:b0:339:7fcd:6bac with SMTP id 15-20020a81090f000000b003397fcd6bacmr18285803ywj.239.1661292611451; Tue, 23 Aug 2022 15:10:11 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:11 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-8-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 07/18] perf record: Update use of pthread mutex From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Switch to the use of mutex wrappers that provide better error checking for synth_lock. Signed-off-by: Ian Rogers --- tools/perf/builtin-record.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 4713f0f3a6cf..02eb85677e99 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -21,6 +21,7 @@ #include "util/evsel.h" #include "util/debug.h" #include "util/mmap.h" +#include "util/mutex.h" #include "util/target.h" #include "util/session.h" #include "util/tool.h" @@ -608,17 +609,18 @@ static int process_synthesized_event(struct perf_tool *tool, return record__write(rec, NULL, event, event->header.size); } +static struct mutex synth_lock; + static int process_locked_synthesized_event(struct perf_tool *tool, union perf_event *event, struct perf_sample *sample __maybe_unused, struct machine *machine __maybe_unused) { - static pthread_mutex_t synth_lock = PTHREAD_MUTEX_INITIALIZER; int ret; - pthread_mutex_lock(&synth_lock); + mutex_lock(&synth_lock); ret = process_synthesized_event(tool, event, sample, machine); - pthread_mutex_unlock(&synth_lock); + mutex_unlock(&synth_lock); return ret; } @@ -1917,6 +1919,7 @@ static int record__synthesize(struct record *rec, bool tail) } if (rec->opts.nr_threads_synthesize > 1) { + mutex_init(&synth_lock, /*pshared=*/false); perf_set_multithreaded(); f = process_locked_synthesized_event; } @@ -1930,8 +1933,10 @@ static int record__synthesize(struct record *rec, bool tail) rec->opts.nr_threads_synthesize); } - if (rec->opts.nr_threads_synthesize > 1) + if (rec->opts.nr_threads_synthesize > 1) { perf_set_singlethreaded(); + mutex_destroy(&synth_lock); + } out: return err; From patchwork Tue Aug 23 22:09:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952641 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D3C7FC32793 for ; Tue, 23 Aug 2022 22:11:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232384AbiHWWLM (ORCPT ); Tue, 23 Aug 2022 18:11:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232394AbiHWWKk (ORCPT ); Tue, 23 Aug 2022 18:10:40 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA4B976775 for ; Tue, 23 Aug 2022 15:10:17 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-337ed9110c2so218344567b3.15 for ; Tue, 23 Aug 2022 15:10:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=Nmevlmvs95ReeU17PDLcRwDmF7At1E+WajEKdTw91Uw=; b=hXjcQMwEEXc+2FJP5jEonGJDp4VLxuhr1wun6nJzM1xtMqAxuo6mMhgPP4DKVcuwW8 JZczjdphY1anQu5exGDh9QAMxQ1VDlvRdQ+9SVHGNEuuceniHynOfl1u1aYRviBVtka0 K4Vzv4LKwjO6xxBvvpGAZ82OfVoKpLN5oP172Ny1EBrNhL48LaA2N+yqJq5e1dA6c4Lo QUYp55vx6QJSXxwUFPLaYapa/p2Z5NfR4+3J4TuRixFDnOenAcT+IhkGM3SSdZyKHkNB EHDQrYngxThuXYI0SNOsS5mwu/df0Upm3k9ak5yU0raZYr+qWqGZyvKLSKtUTdxkIAwW JPFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=Nmevlmvs95ReeU17PDLcRwDmF7At1E+WajEKdTw91Uw=; b=T47YWsUsgS2bOtGbEKTo+0zE2eAFgTwdeuqhNZvthgZ9DocBVDV7lLr8edqKc6ebWA ZsNGPqHOyx464MRpbsCfhFTJWQ/q+NMoH3Y1LOAlM6P25IlqexDbSBoQAloYbk6VYNf4 5tE/H7P+XgN8Q6ocd6FZZP8omU6dRu5a/yR6BJr8gWc4xdqwIcIdBxm058UUTS/qzhPW lt1zrUkY5Cip2ENu8cn28o+/WqeyDz3OcwjKufe+dUxVnKB4tsUjSAygu8MTTs4QoVKO ZE87zTeZJHMby3CDTySQyupVWdVaVSbc13wqac5YQXO3RqwIPRiJSKoRrzEbuch+oHNN pasg== X-Gm-Message-State: ACgBeo0XJuet0nUc5QVLaNdJn69EPR0ATVdmcbv6qLYy/CXxrC1n7gl/ u/gqGbD3bfTC2tQF94lTyymAaW29y21I X-Google-Smtp-Source: AA6agR58yS8SWyAsuyow+y3Swvz6Od4Ewgb3I2udMsMCY+3ZF5TUoxJtMxy2sIUwcahrUYTeq37Vsh4E27Sx X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a81:1946:0:b0:334:9d98:c562 with SMTP id 67-20020a811946000000b003349d98c562mr27886187ywz.425.1661292617059; Tue, 23 Aug 2022 15:10:17 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:12 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-9-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 08/18] perf sched: Update use of pthread mutex From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Switch to the use of mutex wrappers that provide better error checking. Update cmd_sched so that we always explicitly destroy the mutexes. Signed-off-by: Ian Rogers --- tools/perf/builtin-sched.c | 67 ++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index 2f6cd1b8b662..0f52f73be896 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -7,6 +7,7 @@ #include "util/evlist.h" #include "util/evsel.h" #include "util/evsel_fprintf.h" +#include "util/mutex.h" #include "util/symbol.h" #include "util/thread.h" #include "util/header.h" @@ -184,8 +185,8 @@ struct perf_sched { struct task_desc **pid_to_task; struct task_desc **tasks; const struct trace_sched_handler *tp_handler; - pthread_mutex_t start_work_mutex; - pthread_mutex_t work_done_wait_mutex; + struct mutex start_work_mutex; + struct mutex work_done_wait_mutex; int profile_cpu; /* * Track the current task - that way we can know whether there's any @@ -635,10 +636,8 @@ static void *thread_func(void *ctx) again: ret = sem_post(&this_task->ready_for_work); BUG_ON(ret); - ret = pthread_mutex_lock(&sched->start_work_mutex); - BUG_ON(ret); - ret = pthread_mutex_unlock(&sched->start_work_mutex); - BUG_ON(ret); + mutex_lock(&sched->start_work_mutex); + mutex_unlock(&sched->start_work_mutex); cpu_usage_0 = get_cpu_usage_nsec_self(fd); @@ -652,10 +651,8 @@ static void *thread_func(void *ctx) ret = sem_post(&this_task->work_done_sem); BUG_ON(ret); - ret = pthread_mutex_lock(&sched->work_done_wait_mutex); - BUG_ON(ret); - ret = pthread_mutex_unlock(&sched->work_done_wait_mutex); - BUG_ON(ret); + mutex_lock(&sched->work_done_wait_mutex); + mutex_unlock(&sched->work_done_wait_mutex); goto again; } @@ -672,10 +669,8 @@ static void create_tasks(struct perf_sched *sched) err = pthread_attr_setstacksize(&attr, (size_t) max(16 * 1024, (int)PTHREAD_STACK_MIN)); BUG_ON(err); - err = pthread_mutex_lock(&sched->start_work_mutex); - BUG_ON(err); - err = pthread_mutex_lock(&sched->work_done_wait_mutex); - BUG_ON(err); + mutex_lock(&sched->start_work_mutex); + mutex_lock(&sched->work_done_wait_mutex); for (i = 0; i < sched->nr_tasks; i++) { struct sched_thread_parms *parms = malloc(sizeof(*parms)); BUG_ON(parms == NULL); @@ -699,7 +694,7 @@ static void wait_for_tasks(struct perf_sched *sched) sched->start_time = get_nsecs(); sched->cpu_usage = 0; - pthread_mutex_unlock(&sched->work_done_wait_mutex); + mutex_unlock(&sched->work_done_wait_mutex); for (i = 0; i < sched->nr_tasks; i++) { task = sched->tasks[i]; @@ -707,12 +702,11 @@ static void wait_for_tasks(struct perf_sched *sched) BUG_ON(ret); sem_init(&task->ready_for_work, 0, 0); } - ret = pthread_mutex_lock(&sched->work_done_wait_mutex); - BUG_ON(ret); + mutex_lock(&sched->work_done_wait_mutex); cpu_usage_0 = get_cpu_usage_nsec_parent(); - pthread_mutex_unlock(&sched->start_work_mutex); + mutex_unlock(&sched->start_work_mutex); for (i = 0; i < sched->nr_tasks; i++) { task = sched->tasks[i]; @@ -734,8 +728,7 @@ static void wait_for_tasks(struct perf_sched *sched) sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) + sched->parent_cpu_usage)/sched->replay_repeat; - ret = pthread_mutex_lock(&sched->start_work_mutex); - BUG_ON(ret); + mutex_lock(&sched->start_work_mutex); for (i = 0; i < sched->nr_tasks; i++) { task = sched->tasks[i]; @@ -3430,8 +3423,6 @@ int cmd_sched(int argc, const char **argv) }, .cmp_pid = LIST_HEAD_INIT(sched.cmp_pid), .sort_list = LIST_HEAD_INIT(sched.sort_list), - .start_work_mutex = PTHREAD_MUTEX_INITIALIZER, - .work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER, .sort_order = default_sort_order, .replay_repeat = 10, .profile_cpu = -1, @@ -3545,8 +3536,10 @@ int cmd_sched(int argc, const char **argv) .fork_event = replay_fork_event, }; unsigned int i; - int ret; + int ret = 0; + mutex_init(&sched.start_work_mutex, /*pshared=*/false); + mutex_init(&sched.work_done_wait_mutex, /*pshared=*/false); for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++) sched.curr_pid[i] = -1; @@ -3558,11 +3551,10 @@ int cmd_sched(int argc, const char **argv) /* * Aliased to 'perf script' for now: */ - if (!strcmp(argv[0], "script")) - return cmd_script(argc, argv); - - if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) { - return __cmd_record(argc, argv); + if (!strcmp(argv[0], "script")) { + ret = cmd_script(argc, argv); + } else if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) { + ret = __cmd_record(argc, argv); } else if (strlen(argv[0]) > 2 && strstarts("latency", argv[0])) { sched.tp_handler = &lat_ops; if (argc > 1) { @@ -3571,7 +3563,7 @@ int cmd_sched(int argc, const char **argv) usage_with_options(latency_usage, latency_options); } setup_sorting(&sched, latency_options, latency_usage); - return perf_sched__lat(&sched); + ret = perf_sched__lat(&sched); } else if (!strcmp(argv[0], "map")) { if (argc) { argc = parse_options(argc, argv, map_options, map_usage, 0); @@ -3580,7 +3572,7 @@ int cmd_sched(int argc, const char **argv) } sched.tp_handler = &map_ops; setup_sorting(&sched, latency_options, latency_usage); - return perf_sched__map(&sched); + ret = perf_sched__map(&sched); } else if (strlen(argv[0]) > 2 && strstarts("replay", argv[0])) { sched.tp_handler = &replay_ops; if (argc) { @@ -3588,7 +3580,7 @@ int cmd_sched(int argc, const char **argv) if (argc) usage_with_options(replay_usage, replay_options); } - return perf_sched__replay(&sched); + ret = perf_sched__replay(&sched); } else if (!strcmp(argv[0], "timehist")) { if (argc) { argc = parse_options(argc, argv, timehist_options, @@ -3604,16 +3596,21 @@ int cmd_sched(int argc, const char **argv) parse_options_usage(NULL, timehist_options, "w", true); if (sched.show_next) parse_options_usage(NULL, timehist_options, "n", true); - return -EINVAL; + ret = -EINVAL; + goto out; } ret = symbol__validate_sym_arguments(); if (ret) - return ret; + goto out; - return perf_sched__timehist(&sched); + ret = perf_sched__timehist(&sched); } else { usage_with_options(sched_usage, sched_options); } - return 0; +out: + mutex_destroy(&sched.start_work_mutex); + mutex_destroy(&sched.work_done_wait_mutex); + + return ret; } From patchwork Tue Aug 23 22:09:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952642 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA2ACC32796 for ; Tue, 23 Aug 2022 22:11:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232372AbiHWWL0 (ORCPT ); Tue, 23 Aug 2022 18:11:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232311AbiHWWKx (ORCPT ); Tue, 23 Aug 2022 18:10:53 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD0D477E89 for ; Tue, 23 Aug 2022 15:10:23 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-3360c0f0583so260346257b3.2 for ; Tue, 23 Aug 2022 15:10:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=pchsPb0q/3M596TWShJAL2CjEne0833N+uxiCelfZ4s=; b=Odecrv1If+FMva4pnjFdp2Y6sfSqkFu8zdScmwdC2ZskRIj8GRzINulF1y6pcEv9up JXbiuSVxJEyPcQewCOAToOcgB3wBfxPSxKK0o7IKKY0wq5g7+H7WIziuzADEsABaPQcs EEAIuMD63GSiJLVRq5qfb3Oj5TiLbm2r9MQlo7dKbwagH4Ab6Y9Wxex0gsZf3Zr8rs5L uE3lZZYnykc7V45aUX1vtbngrBDr1a/1SofnqHX+xllKQuf9G5cviZvX9PmnzXnAbIMN 8wx8JSaj4GQp2qAkniRJzYHk+A7tydF3Rn7hhRbhqeA4rsEvKv5rSNyv6SaG44p9aG+X K+uA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=pchsPb0q/3M596TWShJAL2CjEne0833N+uxiCelfZ4s=; b=PYnCY0is67fdC1q5OrpnWxhn31t8XprZ095s6Ctoklqag03ZFoYAAzr/bjobZe4Df/ X8C3sN9cBAVcFsiTbkfA9Cn3ULOexQaFHAhRL4L+8ui5hWLA0PMaKUCRUdppQ5BKPUxP g3sKtchhR900D4vLUP0kKNY3GQ5zjjQ3KZ9ojNvN1SWMJz5m6LrOrOn3h8TRAXN0LcQ9 7vks8KyIFDcXJwYOn0S1k1hwrZi/f2p4Svx1xT7ACeCEdjHHo+SaQAq+EGOilTIsBiFF asgvyoxEg39pZNSzpTZIPKPfiELqAK7f3i0Phv9nJMwHsG5rrCiD9oZypZIAR7mBbe1q 73oQ== X-Gm-Message-State: ACgBeo07BDUVdYwA3DYre4rdD+EIdN1w0OMxsNK1sKVsXln7Gkn4R6G1 23jJ4CtvO5+MeTzuk00wKcNU03Hrn37U X-Google-Smtp-Source: AA6agR7hHbAq4uohe3oz+HwH6KghfM+zPkcQ6XmgaiG9Fo36n2oNWZm1KHIbogcMXI+oTbi3TINLNMe56vaI X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a25:780e:0:b0:671:3386:8129 with SMTP id t14-20020a25780e000000b0067133868129mr25551495ybc.404.1661292622945; Tue, 23 Aug 2022 15:10:22 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:13 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-10-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 09/18] perf ui: Update use of pthread mutex From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Switch to the use of mutex wrappers that provide better error checking. Signed-off-by: Ian Rogers --- tools/perf/ui/browser.c | 20 ++++++++++---------- tools/perf/ui/browsers/annotate.c | 2 +- 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 ++-- 8 files changed, 35 insertions(+), 35 deletions(-) diff --git a/tools/perf/ui/browser.c b/tools/perf/ui/browser.c index fa5bd5c20e96..78fb01d6ad63 100644 --- a/tools/perf/ui/browser.c +++ b/tools/perf/ui/browser.c @@ -268,9 +268,9 @@ void __ui_browser__show_title(struct ui_browser *browser, const char *title) void ui_browser__show_title(struct ui_browser *browser, const char *title) { - pthread_mutex_lock(&ui__lock); + mutex_lock(&ui__lock); __ui_browser__show_title(browser, title); - pthread_mutex_unlock(&ui__lock); + mutex_unlock(&ui__lock); } int ui_browser__show(struct ui_browser *browser, const char *title, @@ -284,7 +284,7 @@ int ui_browser__show(struct ui_browser *browser, const char *title, browser->refresh_dimensions(browser); - pthread_mutex_lock(&ui__lock); + mutex_lock(&ui__lock); __ui_browser__show_title(browser, title); browser->title = title; @@ -295,16 +295,16 @@ int ui_browser__show(struct ui_browser *browser, const char *title, va_end(ap); if (err > 0) ui_helpline__push(browser->helpline); - pthread_mutex_unlock(&ui__lock); + mutex_unlock(&ui__lock); return err ? 0 : -1; } void ui_browser__hide(struct ui_browser *browser) { - pthread_mutex_lock(&ui__lock); + mutex_lock(&ui__lock); ui_helpline__pop(); zfree(&browser->helpline); - pthread_mutex_unlock(&ui__lock); + mutex_unlock(&ui__lock); } static void ui_browser__scrollbar_set(struct ui_browser *browser) @@ -352,9 +352,9 @@ static int __ui_browser__refresh(struct ui_browser *browser) int ui_browser__refresh(struct ui_browser *browser) { - pthread_mutex_lock(&ui__lock); + mutex_lock(&ui__lock); __ui_browser__refresh(browser); - pthread_mutex_unlock(&ui__lock); + mutex_unlock(&ui__lock); return 0; } @@ -390,10 +390,10 @@ int ui_browser__run(struct ui_browser *browser, int delay_secs) while (1) { off_t offset; - pthread_mutex_lock(&ui__lock); + mutex_lock(&ui__lock); err = __ui_browser__refresh(browser); SLsmg_refresh(); - pthread_mutex_unlock(&ui__lock); + mutex_unlock(&ui__lock); if (err < 0) break; diff --git a/tools/perf/ui/browsers/annotate.c b/tools/perf/ui/browsers/annotate.c index 44ba900828f6..b8747e8dd9ea 100644 --- a/tools/perf/ui/browsers/annotate.c +++ b/tools/perf/ui/browsers/annotate.c @@ -8,11 +8,11 @@ #include "../../util/hist.h" #include "../../util/sort.h" #include "../../util/map.h" +#include "../../util/mutex.h" #include "../../util/symbol.h" #include "../../util/evsel.h" #include "../../util/evlist.h" #include -#include #include #include #include diff --git a/tools/perf/ui/setup.c b/tools/perf/ui/setup.c index 700335cde618..fd10dc6baf07 100644 --- a/tools/perf/ui/setup.c +++ b/tools/perf/ui/setup.c @@ -1,5 +1,4 @@ // SPDX-License-Identifier: GPL-2.0 -#include #include #include @@ -8,7 +7,7 @@ #include "../util/hist.h" #include "ui.h" -pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER; +struct mutex ui__lock; void *perf_gtk_handle; int use_browser = -1; @@ -76,6 +75,7 @@ int stdio__config_color(const struct option *opt __maybe_unused, void setup_browser(bool fallback_to_pager) { + mutex_init(&ui__lock, /*pshared=*/false); if (use_browser < 2 && (!isatty(1) || dump_trace)) use_browser = 0; @@ -118,4 +118,5 @@ void exit_browser(bool wait_for_ok) default: break; } + mutex_destroy(&ui__lock); } diff --git a/tools/perf/ui/tui/helpline.c b/tools/perf/ui/tui/helpline.c index 298d6af82fdd..db4952f5990b 100644 --- a/tools/perf/ui/tui/helpline.c +++ b/tools/perf/ui/tui/helpline.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include @@ -33,7 +32,7 @@ static int tui_helpline__show(const char *format, va_list ap) int ret; static int backlog; - pthread_mutex_lock(&ui__lock); + mutex_lock(&ui__lock); ret = vscnprintf(ui_helpline__last_msg + backlog, sizeof(ui_helpline__last_msg) - backlog, format, ap); backlog += ret; @@ -45,7 +44,7 @@ static int tui_helpline__show(const char *format, va_list ap) SLsmg_refresh(); backlog = 0; } - pthread_mutex_unlock(&ui__lock); + mutex_unlock(&ui__lock); return ret; } diff --git a/tools/perf/ui/tui/progress.c b/tools/perf/ui/tui/progress.c index 3d74af5a7ece..71b6c8d9474f 100644 --- a/tools/perf/ui/tui/progress.c +++ b/tools/perf/ui/tui/progress.c @@ -45,7 +45,7 @@ static void tui_progress__update(struct ui_progress *p) } ui__refresh_dimensions(false); - pthread_mutex_lock(&ui__lock); + mutex_lock(&ui__lock); y = SLtt_Screen_Rows / 2 - 2; SLsmg_set_color(0); SLsmg_draw_box(y, 0, 3, SLtt_Screen_Cols); @@ -56,7 +56,7 @@ static void tui_progress__update(struct ui_progress *p) bar = ((SLtt_Screen_Cols - 2) * p->curr) / p->total; SLsmg_fill_region(y, 1, 1, bar, ' '); SLsmg_refresh(); - pthread_mutex_unlock(&ui__lock); + mutex_unlock(&ui__lock); } static void tui_progress__finish(void) @@ -67,12 +67,12 @@ static void tui_progress__finish(void) return; ui__refresh_dimensions(false); - pthread_mutex_lock(&ui__lock); + mutex_lock(&ui__lock); y = SLtt_Screen_Rows / 2 - 2; SLsmg_set_color(0); SLsmg_fill_region(y, 0, 3, SLtt_Screen_Cols, ' '); SLsmg_refresh(); - pthread_mutex_unlock(&ui__lock); + mutex_unlock(&ui__lock); } static struct ui_progress_ops tui_progress__ops = { diff --git a/tools/perf/ui/tui/setup.c b/tools/perf/ui/tui/setup.c index b1be59b4e2a4..a3b8c397c24d 100644 --- a/tools/perf/ui/tui/setup.c +++ b/tools/perf/ui/tui/setup.c @@ -29,10 +29,10 @@ void ui__refresh_dimensions(bool force) { if (force || ui__need_resize) { ui__need_resize = 0; - pthread_mutex_lock(&ui__lock); + mutex_lock(&ui__lock); SLtt_get_screen_size(); SLsmg_reinit_smg(); - pthread_mutex_unlock(&ui__lock); + mutex_unlock(&ui__lock); } } @@ -170,10 +170,10 @@ void ui__exit(bool wait_for_ok) "Press any key...", 0); SLtt_set_cursor_visibility(1); - if (!pthread_mutex_trylock(&ui__lock)) { + if (mutex_trylock(&ui__lock)) { SLsmg_refresh(); SLsmg_reset_smg(); - pthread_mutex_unlock(&ui__lock); + mutex_unlock(&ui__lock); } SLang_reset_tty(); perf_error__unregister(&perf_tui_eops); diff --git a/tools/perf/ui/tui/util.c b/tools/perf/ui/tui/util.c index 0f562e2cb1e8..3c5174854ac8 100644 --- a/tools/perf/ui/tui/util.c +++ b/tools/perf/ui/tui/util.c @@ -95,7 +95,7 @@ int ui_browser__input_window(const char *title, const char *text, char *input, t = sep + 1; } - pthread_mutex_lock(&ui__lock); + mutex_lock(&ui__lock); max_len += 2; nr_lines += 8; @@ -125,17 +125,17 @@ int ui_browser__input_window(const char *title, const char *text, char *input, SLsmg_write_nstring((char *)exit_msg, max_len); SLsmg_refresh(); - pthread_mutex_unlock(&ui__lock); + mutex_unlock(&ui__lock); x += 2; len = 0; key = ui__getch(delay_secs); while (key != K_TIMER && key != K_ENTER && key != K_ESC) { - pthread_mutex_lock(&ui__lock); + mutex_lock(&ui__lock); if (key == K_BKSPC) { if (len == 0) { - pthread_mutex_unlock(&ui__lock); + mutex_unlock(&ui__lock); goto next_key; } SLsmg_gotorc(y, x + --len); @@ -147,7 +147,7 @@ int ui_browser__input_window(const char *title, const char *text, char *input, } SLsmg_refresh(); - pthread_mutex_unlock(&ui__lock); + mutex_unlock(&ui__lock); /* XXX more graceful overflow handling needed */ if (len == sizeof(buf) - 1) { @@ -215,19 +215,19 @@ void __ui__info_window(const char *title, const char *text, const char *exit_msg void ui__info_window(const char *title, const char *text) { - pthread_mutex_lock(&ui__lock); + mutex_lock(&ui__lock); __ui__info_window(title, text, NULL); SLsmg_refresh(); - pthread_mutex_unlock(&ui__lock); + mutex_unlock(&ui__lock); } int ui__question_window(const char *title, const char *text, const char *exit_msg, int delay_secs) { - pthread_mutex_lock(&ui__lock); + mutex_lock(&ui__lock); __ui__info_window(title, text, exit_msg); SLsmg_refresh(); - pthread_mutex_unlock(&ui__lock); + mutex_unlock(&ui__lock); return ui__getch(delay_secs); } diff --git a/tools/perf/ui/ui.h b/tools/perf/ui/ui.h index 9b6fdf06e1d2..99f8d2fe9bc5 100644 --- a/tools/perf/ui/ui.h +++ b/tools/perf/ui/ui.h @@ -2,11 +2,11 @@ #ifndef _PERF_UI_H_ #define _PERF_UI_H_ 1 -#include +#include "../util/mutex.h" #include #include -extern pthread_mutex_t ui__lock; +extern struct mutex ui__lock; extern void *perf_gtk_handle; extern int use_browser; From patchwork Tue Aug 23 22:09:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952643 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6EB81C32772 for ; Tue, 23 Aug 2022 22:11:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232321AbiHWWL1 (ORCPT ); Tue, 23 Aug 2022 18:11:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36684 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232614AbiHWWK7 (ORCPT ); Tue, 23 Aug 2022 18:10:59 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4527C74DF2 for ; Tue, 23 Aug 2022 15:10:29 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-33352499223so260379637b3.8 for ; Tue, 23 Aug 2022 15:10:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=hI/abfVc/lpVnBGw0/IBypco5YlPGa9tkPSCqJ9uDZQ=; b=a0O0YxQ/mlClsh++0Eih5r8XCyQ2PWf4eHqbSAVf87G0uhjjV4kbUziQxWvdsEefYx idvXzO4xoEBWQvhOQHdXgIWgRMQZRpCedNhRsfArcBWCTGaYSh2qqeFdSQaL//ZenxmE bnx6vifkA68uo0CDX3CQZ+CSynMjQha/1cR514WJafX1myCBmm7/vRQkqn15p4EBevqQ f/SGa4Emdng/0J5qRratt4QhRZaPGGIa1R+oZftnBCVr3OWQORW0vGKyEDtPHlVaXJsa XzwqISY11EGfxyIVSGnS84xPYIglUGI3DCk12vMe/00OrB/Kzs2cQbbnW06pEvm+TWBu GWJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=hI/abfVc/lpVnBGw0/IBypco5YlPGa9tkPSCqJ9uDZQ=; b=vsBDtsyA9F1/aLoQAndH37P11IC5XSHtdJZDEIrTDiTChp2IApH2VMP/gGMxcQLmse FSKAcZy9chg5wThA0xbFU0XNEeSMLyXiq5xULAVDZdcLH9fitugt/MWjyM80f6D0bbZn AgLOppdqrI7nxGkaKoXOcKAMjAwn4Mo0g2lk714ex0YzJfLJK/TDQfbIvmB94nzgZQdU f/Xv+45Dp7KrjRCNls3qyM4FIShR0YRRmZIWwQFVLQ9wOXVwkhrtENJnCEOIh4Ba0Q7v u35u9rOl9MCMpVdRLRPeNXLkKg+we+J8awFKHyIT4CDHPeng0j/UmokK2KwvOSNwuuJh kZug== X-Gm-Message-State: ACgBeo3zJi/74XNXDCiM1rJx8cCcokUyqHDiGVuJ0YJyz6W4XhBLoqo2 1weN4oUmedSrCZD1nfIaqcxR+i/NKZw7 X-Google-Smtp-Source: AA6agR6qFYD5g1FqAqGPVeImurrh/ZkYJjB3EL0kLaZmGTBXEapeVBFxkCQ4lK/axFqO/Hk6hdUZnkE+qyBL X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a81:d0d:0:b0:333:99b1:44f1 with SMTP id 13-20020a810d0d000000b0033399b144f1mr26891746ywn.288.1661292628860; Tue, 23 Aug 2022 15:10:28 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:14 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-11-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 10/18] perf mmap: Remove unnecessary pthread.h include From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org The comment says it is for cpu_set_t which isn't used in the header. Signed-off-by: Ian Rogers --- tools/perf/util/mmap.h | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/perf/util/mmap.h b/tools/perf/util/mmap.h index cd8b0777473b..cd4ccec7f361 100644 --- a/tools/perf/util/mmap.h +++ b/tools/perf/util/mmap.h @@ -9,7 +9,6 @@ #include #include #include -#include // for cpu_set_t #ifdef HAVE_AIO_SUPPORT #include #endif From patchwork Tue Aug 23 22:09:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952644 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55EA2C32789 for ; Tue, 23 Aug 2022 22:11:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232815AbiHWWLr (ORCPT ); Tue, 23 Aug 2022 18:11:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232228AbiHWWLJ (ORCPT ); Tue, 23 Aug 2022 18:11:09 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50D57785A2 for ; Tue, 23 Aug 2022 15:10:36 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-335ff2ef600so261867867b3.18 for ; Tue, 23 Aug 2022 15:10:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=4T63H/7ulEFz0y7iN4SapyGcMaYqwoIDMM7/trBdyIM=; b=NG04TAKrMqD5yuCJUiZXkkkxUS8d8EDfmvANyx1CxoAGugcPRW6r4NL9uieW+hdrer TEyjfRtOTqmKH5WoG3B1dRvOEJgNEZ9oLTqhQLVZFGugh2K4zYhk1WMMtULqw7Pw45My wwRrDgOSSc0G2aalSmQbzGyz26agS4lYVgedBX7iIs9Q032UgiMNe5T49G2x5Wed58X1 9Auuzt6oP9N3htE+FULLeAFtcpSlCOhwOugg8yvVImkhngV570xfN4TZv56Yc4cG0rgs kPLoic4tgb4tdlQLRaIeJK8GtZzR6WAfW547xvhh25gg3AfBJXkNGJc0qFSPi8RRQjXi yvOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=4T63H/7ulEFz0y7iN4SapyGcMaYqwoIDMM7/trBdyIM=; b=4ljq0eHkmXV0EKzs+vy/BQwzPunpfi7P62CZ+McnuPh4LngeaM6BvH3xO5+vjKIxAA xeWzy6NXpOBJZSuv7WumDWtA16FY+D0775C2jKu6+ycPKXznaKXd7vKOhkLrgY5QKVKI VH7k7430CCLeOA575AfHoKKOiwkcrM7oxrIacj6DTe0gs4DWGiJd3CLIrUgACvoSJngd 5m3U9n4c5G299AoxGL1rMPjW48ZBZs1HnHFX7dFTg3M3iuA9jb47g77DwUanfwgX6bUr zJMZGml8I3HP190efAE8C06rFZiXk7pmfQhoRgh100Gn2fef1/FtsarObyN3Zwu/crCF nNcg== X-Gm-Message-State: ACgBeo10FfHn5X6rXbUGh1oDGo7jBT2QeXAG/WUX+nOYUXGGAsjcgIJa m14Z6qgLy2JFERfoZxsGqDbnoqaCo1Fx X-Google-Smtp-Source: AA6agR5fLgUmwOPH9u5c9mealhHzc7vyBsAFZgGD99BelmVEDn8hDSG9RnnaQJ2rHTdAVQEq7MRalAMAmhjQ X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a25:6e87:0:b0:692:3f71:86c8 with SMTP id j129-20020a256e87000000b006923f7186c8mr27447174ybc.295.1661292635305; Tue, 23 Aug 2022 15:10:35 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:15 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-12-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 11/18] perf dso: Update use of pthread mutex From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Switch to the use of mutex wrappers that provide better error checking. Signed-off-by: Ian Rogers --- tools/perf/util/dso.c | 12 ++++++------ tools/perf/util/dso.h | 4 ++-- tools/perf/util/symbol.c | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 5ac13958d1bd..c7a5b42d1311 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -795,7 +795,7 @@ dso_cache__free(struct dso *dso) struct rb_root *root = &dso->data.cache; struct rb_node *next = rb_first(root); - pthread_mutex_lock(&dso->lock); + mutex_lock(&dso->lock); while (next) { struct dso_cache *cache; @@ -804,7 +804,7 @@ dso_cache__free(struct dso *dso) rb_erase(&cache->rb_node, root); free(cache); } - pthread_mutex_unlock(&dso->lock); + mutex_unlock(&dso->lock); } static struct dso_cache *__dso_cache__find(struct dso *dso, u64 offset) @@ -841,7 +841,7 @@ dso_cache__insert(struct dso *dso, struct dso_cache *new) struct dso_cache *cache; u64 offset = new->offset; - pthread_mutex_lock(&dso->lock); + mutex_lock(&dso->lock); while (*p != NULL) { u64 end; @@ -862,7 +862,7 @@ dso_cache__insert(struct dso *dso, struct dso_cache *new) cache = NULL; out: - pthread_mutex_unlock(&dso->lock); + mutex_unlock(&dso->lock); return cache; } @@ -1297,7 +1297,7 @@ struct dso *dso__new_id(const char *name, struct dso_id *id) dso->root = NULL; INIT_LIST_HEAD(&dso->node); INIT_LIST_HEAD(&dso->data.open_entry); - pthread_mutex_init(&dso->lock, NULL); + mutex_init(&dso->lock, /*pshared=*/false); refcount_set(&dso->refcnt, 1); } @@ -1336,7 +1336,7 @@ void dso__delete(struct dso *dso) dso__free_a2l(dso); zfree(&dso->symsrc_filename); nsinfo__zput(dso->nsinfo); - pthread_mutex_destroy(&dso->lock); + mutex_destroy(&dso->lock); free(dso); } diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h index 66981c7a9a18..58d94175e714 100644 --- a/tools/perf/util/dso.h +++ b/tools/perf/util/dso.h @@ -2,7 +2,6 @@ #ifndef __PERF_DSO #define __PERF_DSO -#include #include #include #include @@ -11,6 +10,7 @@ #include #include #include "build-id.h" +#include "mutex.h" struct machine; struct map; @@ -145,7 +145,7 @@ struct dso_cache { struct auxtrace_cache; struct dso { - pthread_mutex_t lock; + struct mutex lock; struct list_head node; struct rb_node rb_node; /* rbtree node sorted by long name */ struct rb_root *root; /* root of rbtree that rb_node is in */ diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index a4b22caa7c24..656d9b4dd456 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -1800,7 +1800,7 @@ int dso__load(struct dso *dso, struct map *map) } nsinfo__mountns_enter(dso->nsinfo, &nsc); - pthread_mutex_lock(&dso->lock); + mutex_lock(&dso->lock); /* check again under the dso->lock */ if (dso__loaded(dso)) { @@ -1964,7 +1964,7 @@ int dso__load(struct dso *dso, struct map *map) ret = 0; out: dso__set_loaded(dso); - pthread_mutex_unlock(&dso->lock); + mutex_unlock(&dso->lock); nsinfo__mountns_exit(&nsc); return ret; From patchwork Tue Aug 23 22:09:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952650 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 50712C3F6B0 for ; Tue, 23 Aug 2022 22:12:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232997AbiHWWMC (ORCPT ); Tue, 23 Aug 2022 18:12:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232837AbiHWWLV (ORCPT ); Tue, 23 Aug 2022 18:11:21 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D58F786D9 for ; Tue, 23 Aug 2022 15:10:41 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-33931a5c133so175257597b3.17 for ; Tue, 23 Aug 2022 15:10:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=CD8iIk6b6MevHATdYAYkXOpPAgu6T52eXiNNa5NTXNQ=; b=Sp5e3PVhmJZPPFBxfKL1C2F0+f7Jxdr8GKQ1TOIUb1lpNlKpPffF/LF9kYQJcxziAr ajXhcsI40hEecVyu5WYoMmRGLB+4LQBGE2MvIMDXRxcfVZna8l8ndUEVWrpNN1eUtIIF AQKPOGGdTdUlJQIZpaVRAtVmumRevpCc1g2bgckfNPpUXqW01BiysF5rUJRFxanBplsh vcAYYHQmb0jfWJQ7PvXzMhcFPIQqUjOH5v5YL7HOP7yE2qmSvuLTnUpcMC9nD0ftsLVN 9UJ6iFlIMXAdkhK7tODtFehZkAhvmO93zvWLm2D7jlHPR8z+qvljvHVxsLa1O6PS0Apr rvdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=CD8iIk6b6MevHATdYAYkXOpPAgu6T52eXiNNa5NTXNQ=; b=kqgjgrc6GZB9Ss92LRPJsEFLo317bSEKD6lSzMmyJtsoYMAiBAwIL4vJgC9/jeswNe tNqUCCUOpkyvvC9tONvT/3Y60/dKEKZCR3iP+41/4dGa09yFXmqkD8cdHo1dk2D/RnAw 92UY1cdFyT7cKzZOSOZdOzqwarC096irMSfkUpiPovtqHe4r56QnpvjLY4t9Fl3IKUNY soMIiJFsEYH3GxrgpI/FOIEWH2oFjDJATOJ/JtEJiYQYiGMQe2hqi5QJilGGSVTZ794D OjUEvf0Wt6rbzl/2Cr31NEqcetFY0EkJbARGs/GgPjC0K+iv1veQTJ13StUCuXRHhyw0 K6bA== X-Gm-Message-State: ACgBeo1XhZCngzGgWwnfaxKcKrUheYeU4/rkvqBdpz8YI0FCxThsr553 aS7sik91VdflURksdPAE/KmXRdjW19vG X-Google-Smtp-Source: AA6agR5dYPPc3nXta6yRb4iuog0i402MQztfisaA6q2bKYPSrT32TWWe9qFlDykmDZCryWwmvhmYN3aDOOOt X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a0d:f741:0:b0:332:efb3:f21b with SMTP id h62-20020a0df741000000b00332efb3f21bmr29670067ywf.85.1661292641202; Tue, 23 Aug 2022 15:10:41 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:16 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-13-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 12/18] perf annotate: Update use of pthread mutex From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Switch to the use of mutex wrappers that provide better error checking. Signed-off-by: Ian Rogers --- tools/perf/builtin-top.c | 14 +++++++------- tools/perf/ui/browsers/annotate.c | 10 +++++----- tools/perf/util/annotate.c | 13 ++++++------- tools/perf/util/annotate.h | 4 ++-- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 14e60f6f219c..b96bb9a23ac0 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -136,10 +136,10 @@ static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he) } notes = symbol__annotation(sym); - pthread_mutex_lock(¬es->lock); + mutex_lock(¬es->lock); if (!symbol__hists(sym, top->evlist->core.nr_entries)) { - pthread_mutex_unlock(¬es->lock); + mutex_unlock(¬es->lock); pr_err("Not enough memory for annotating '%s' symbol!\n", sym->name); sleep(1); @@ -155,7 +155,7 @@ static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he) pr_err("Couldn't annotate %s: %s\n", sym->name, msg); } - pthread_mutex_unlock(¬es->lock); + mutex_unlock(¬es->lock); return err; } @@ -208,12 +208,12 @@ static void perf_top__record_precise_ip(struct perf_top *top, notes = symbol__annotation(sym); - if (pthread_mutex_trylock(¬es->lock)) + if (!mutex_trylock(¬es->lock)) return; err = hist_entry__inc_addr_samples(he, sample, evsel, ip); - pthread_mutex_unlock(¬es->lock); + mutex_unlock(¬es->lock); if (unlikely(err)) { /* @@ -250,7 +250,7 @@ static void perf_top__show_details(struct perf_top *top) symbol = he->ms.sym; notes = symbol__annotation(symbol); - pthread_mutex_lock(¬es->lock); + mutex_lock(¬es->lock); symbol__calc_percent(symbol, evsel); @@ -271,7 +271,7 @@ static void perf_top__show_details(struct perf_top *top) if (more != 0) printf("%d lines not displayed, maybe increase display entries [e]\n", more); out_unlock: - pthread_mutex_unlock(¬es->lock); + mutex_unlock(¬es->lock); } static void perf_top__resort_hists(struct perf_top *t) diff --git a/tools/perf/ui/browsers/annotate.c b/tools/perf/ui/browsers/annotate.c index b8747e8dd9ea..9bc1076374ff 100644 --- a/tools/perf/ui/browsers/annotate.c +++ b/tools/perf/ui/browsers/annotate.c @@ -319,7 +319,7 @@ static void annotate_browser__calc_percent(struct annotate_browser *browser, browser->entries = RB_ROOT; - pthread_mutex_lock(¬es->lock); + mutex_lock(¬es->lock); symbol__calc_percent(sym, evsel); @@ -348,7 +348,7 @@ static void annotate_browser__calc_percent(struct annotate_browser *browser, } disasm_rb_tree__insert(browser, &pos->al); } - pthread_mutex_unlock(¬es->lock); + mutex_unlock(¬es->lock); browser->curr_hot = rb_last(&browser->entries); } @@ -474,10 +474,10 @@ static bool annotate_browser__callq(struct annotate_browser *browser, } notes = symbol__annotation(dl->ops.target.sym); - pthread_mutex_lock(¬es->lock); + mutex_lock(¬es->lock); if (!symbol__hists(dl->ops.target.sym, evsel->evlist->core.nr_entries)) { - pthread_mutex_unlock(¬es->lock); + mutex_unlock(¬es->lock); ui__warning("Not enough memory for annotating '%s' symbol!\n", dl->ops.target.sym->name); return true; @@ -486,7 +486,7 @@ static bool annotate_browser__callq(struct annotate_browser *browser, target_ms.maps = ms->maps; target_ms.map = ms->map; target_ms.sym = dl->ops.target.sym; - pthread_mutex_unlock(¬es->lock); + mutex_unlock(¬es->lock); symbol__tui_annotate(&target_ms, evsel, hbt, browser->opts); sym_title(ms->sym, ms->map, title, sizeof(title), browser->opts->percent_type); ui_browser__show_title(&browser->b, title); diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 2c6a485c3de5..29d804d76145 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -35,7 +35,6 @@ #include "arch/common.h" #include "namespaces.h" #include -#include #include #include #include @@ -821,7 +820,7 @@ void symbol__annotate_zero_histograms(struct symbol *sym) { struct annotation *notes = symbol__annotation(sym); - pthread_mutex_lock(¬es->lock); + mutex_lock(¬es->lock); if (notes->src != NULL) { memset(notes->src->histograms, 0, notes->src->nr_histograms * notes->src->sizeof_sym_hist); @@ -829,7 +828,7 @@ void symbol__annotate_zero_histograms(struct symbol *sym) memset(notes->src->cycles_hist, 0, symbol__size(sym) * sizeof(struct cyc_hist)); } - pthread_mutex_unlock(¬es->lock); + mutex_unlock(¬es->lock); } static int __symbol__account_cycles(struct cyc_hist *ch, @@ -1086,7 +1085,7 @@ void annotation__compute_ipc(struct annotation *notes, size_t size) notes->hit_insn = 0; notes->cover_insn = 0; - pthread_mutex_lock(¬es->lock); + mutex_lock(¬es->lock); for (offset = size - 1; offset >= 0; --offset) { struct cyc_hist *ch; @@ -1105,7 +1104,7 @@ void annotation__compute_ipc(struct annotation *notes, size_t size) notes->have_cycles = true; } } - pthread_mutex_unlock(¬es->lock); + mutex_unlock(¬es->lock); } int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample, @@ -1258,13 +1257,13 @@ int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool r void annotation__init(struct annotation *notes) { - pthread_mutex_init(¬es->lock, NULL); + mutex_init(¬es->lock, /*pshared=*/false); } void annotation__exit(struct annotation *notes) { annotated_source__delete(notes->src); - pthread_mutex_destroy(¬es->lock); + mutex_destroy(¬es->lock); } static void annotation_line__add(struct annotation_line *al, struct list_head *head) diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h index 986f2bbe4870..3cbd883e4d7a 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -8,9 +8,9 @@ #include #include #include -#include #include #include "symbol_conf.h" +#include "mutex.h" #include "spark.h" struct hist_browser_timer; @@ -273,7 +273,7 @@ struct annotated_source { }; struct annotation { - pthread_mutex_t lock; + struct mutex lock; u64 max_coverage; u64 start; u64 hit_cycles; From patchwork Tue Aug 23 22:09:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952651 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 295DDC32789 for ; Tue, 23 Aug 2022 22:12:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233085AbiHWWMI (ORCPT ); Tue, 23 Aug 2022 18:12:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36612 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232882AbiHWWLZ (ORCPT ); Tue, 23 Aug 2022 18:11:25 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9BF5D792EF for ; Tue, 23 Aug 2022 15:10:48 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-334370e5ab9so262805587b3.22 for ; Tue, 23 Aug 2022 15:10:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=arpz1x4wRB6/QNxRP3dkfAc2/txeMiNYmprTIBVUU+o=; b=Lcngrn9oCl+hI8knJyVxJa01Zj/XUqMyM00L3enlNUWFOQaV5c7f+jWKUKP401YcoR 8+rsFImJkb6xicnZAcBiL/fnxVDDr9OmN4yqwUyKemZ2yRGruoH2O5rQaytqfZ01p80A WvcNF9ceSAqsIQt/9vdGPYhAGmn8imRm1T0lGBbGySYZpObNgQBdzN0oQCt8PLhktXNB +FOppc5FXbLCWS9wP3dpt04f7xAS4Us5RbB7Exck2lSdEViTRrSLA7utLf5vK+4GO0JL gn2r35hBP1EuNzw/Yd5KkL7F/A5sAYGL46hUlkQ1SqTLi3cTzQhBQ2kciKeylzgbzlHP zPBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=arpz1x4wRB6/QNxRP3dkfAc2/txeMiNYmprTIBVUU+o=; b=JmdjYjbcookA2Pkro6qpimd0/wV72cyU02dDFV/N10yBjbO+eCzdxMGJOnx8iCcM6e D9fIF0Q+wBL/Om/4f7h/rnD/rxG7zQgDM6682/571LvUOy9rOjozSjb/cy29ZKlR3fmM l6RqAjx+CFK9ipATtu4QXHIWnfhQag2JmHoZywjAUF6JpYRFIzG0igr8SMrMJAza5a2j az9A7lWHGwLyANceQKnAW1d41A2JPOJLSRC60/vW3XgsR/TG4HEXZp+Bg7Bf9bw9HuqU 94JbXFFdDFTsBcBnnLPaLGS4juHued0exyrOtHH48WhbIMQFHLNk/lvbtcw4zQfFShP9 ku7g== X-Gm-Message-State: ACgBeo29aXc4JLTQWwCrp+fFqSfZmpSoWp0nk230kf4NJjhaBwnnf6+l MrHCT0lehJvxCiSyQZjDSRwKLuQVGd0a X-Google-Smtp-Source: AA6agR7eIYIOCFyqOpz14ZguJ/5cNVPSssWyoD3gTfA0QBKYmaULVXXkeowKHq/vD/xDBhmaKFfg43GneYK0 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a5b:24c:0:b0:68f:b0d7:79d9 with SMTP id g12-20020a5b024c000000b0068fb0d779d9mr24806647ybp.26.1661292647538; Tue, 23 Aug 2022 15:10:47 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:17 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-14-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 13/18] perf top: Update use of pthread mutex From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Switch to the use of mutex wrappers that provide better error checking. Signed-off-by: Ian Rogers --- tools/perf/builtin-top.c | 18 +++++++++--------- tools/perf/util/top.h | 5 +++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index b96bb9a23ac0..3757292bfe86 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -893,10 +893,10 @@ static void perf_top__mmap_read_idx(struct perf_top *top, int idx) perf_mmap__consume(&md->core); if (top->qe.rotate) { - pthread_mutex_lock(&top->qe.mutex); + mutex_lock(&top->qe.mutex); top->qe.rotate = false; - pthread_cond_signal(&top->qe.cond); - pthread_mutex_unlock(&top->qe.mutex); + cond_signal(&top->qe.cond); + mutex_unlock(&top->qe.mutex); } } @@ -1100,10 +1100,10 @@ static void *process_thread(void *arg) out = rotate_queues(top); - pthread_mutex_lock(&top->qe.mutex); + mutex_lock(&top->qe.mutex); top->qe.rotate = true; - pthread_cond_wait(&top->qe.cond, &top->qe.mutex); - pthread_mutex_unlock(&top->qe.mutex); + cond_wait(&top->qe.cond, &top->qe.mutex); + mutex_unlock(&top->qe.mutex); if (ordered_events__flush(out, OE_FLUSH__TOP)) pr_err("failed to process events\n"); @@ -1217,8 +1217,8 @@ static void init_process_thread(struct perf_top *top) ordered_events__set_copy_on_queue(&top->qe.data[0], true); ordered_events__set_copy_on_queue(&top->qe.data[1], true); top->qe.in = &top->qe.data[0]; - pthread_mutex_init(&top->qe.mutex, NULL); - pthread_cond_init(&top->qe.cond, NULL); + mutex_init(&top->qe.mutex, /*pshared=*/false); + cond_init(&top->qe.cond, /*pshared=*/false); } static int __cmd_top(struct perf_top *top) @@ -1349,7 +1349,7 @@ static int __cmd_top(struct perf_top *top) out_join: pthread_join(thread, NULL); out_join_thread: - pthread_cond_signal(&top->qe.cond); + cond_signal(&top->qe.cond); pthread_join(thread_process, NULL); return ret; } diff --git a/tools/perf/util/top.h b/tools/perf/util/top.h index 1c2c0a838430..a8b0d79bd96c 100644 --- a/tools/perf/util/top.h +++ b/tools/perf/util/top.h @@ -5,6 +5,7 @@ #include "tool.h" #include "evswitch.h" #include "annotate.h" +#include "mutex.h" #include "ordered-events.h" #include "record.h" #include @@ -53,8 +54,8 @@ struct perf_top { struct ordered_events *in; struct ordered_events data[2]; bool rotate; - pthread_mutex_t mutex; - pthread_cond_t cond; + struct mutex mutex; + struct cond cond; } qe; }; From patchwork Tue Aug 23 22:09:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952652 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A6E6BC32772 for ; Tue, 23 Aug 2022 22:12:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230521AbiHWWM1 (ORCPT ); Tue, 23 Aug 2022 18:12:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232376AbiHWWLv (ORCPT ); Tue, 23 Aug 2022 18:11:51 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D8BF79A65 for ; Tue, 23 Aug 2022 15:10:59 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-335ff2ef600so261878237b3.18 for ; Tue, 23 Aug 2022 15:10:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=4qzUasdtAm954UzRSsL64Femn+0Nzy+IBuEBOWWP/QY=; b=cHl4Z24zLShIZ9jM6xvU6PSPFlPePPNxpwU4YRFMyapYLSTyVGWe71Y7pKHCcPQl1x pyTnHH4JPsyUpZ54JJZqo9/L4036WuYAsVKFAfKpuvGa7GGPGUdZfOYjjYhNeAaCecP4 NK01vFn/N/ddzeizyNoLb0fQ4qIksCWuO0WsVcKdX4QTtW+a2vuY9gngpBRhmu1KqcLB AwjgB9xmV3lUI+pMaAUrIoCWVhGHG8dGDALsTwdsAmeAA38XT3bpcWQuEgwPVYjEcDAr XtpzRoRhXRxJ3d8xW6+04dVtmdU16MhQm9t9447rT367gDkzrZJgxngzOfgV2Qx8DWbO tldQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=4qzUasdtAm954UzRSsL64Femn+0Nzy+IBuEBOWWP/QY=; b=VzEbCTj5iLvKTxBFw9oo2JmU/FxHWNpTr52T0q+EDGz3emzuF/m3kZ8f497ADwEMNo F4UXrZr6vfSEZOSAoN6hDRIsSQ249dtVXbOs0I+fDkzPqhMud1cNROfpIjlu6SGezH0P GU7rOs16MsoaFQnFgWEw+o3fQYPRmwVinHYsyVpze4KvMW8NBl/J6q6z2GG0BVlYi5yc DLsSVM/qmT6PpR4bTUKvcgmT8RvMO9JnEoe5VmdzKTof9Dm79/PsL+HtR2ZNOGwof8Yz Qe7wOT/0sCnSQ95CG6jmDyXmnwfvh1j/8ZoeHnLbJaaObhsE/UCUdtXu/BP9/s7+LkSz 3g0g== X-Gm-Message-State: ACgBeo0RxoszGjCga0Epf3DYeD2lYQG2pRBiI94KMHEUwSMxHKqYxXEW EeEa3aN+8Fq2KWqNveDQa5d0MQcJ6TFY X-Google-Smtp-Source: AA6agR6KpBoAko5h2ZEqfJEZD9u2QG5oKscnWYPYDk9FpYP+FjWPMVgX+tkWTtuFytTV74u4QuCbkG8atvo9 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a0d:c9c3:0:b0:334:e708:7b05 with SMTP id l186-20020a0dc9c3000000b00334e7087b05mr28598701ywd.98.1661292658943; Tue, 23 Aug 2022 15:10:58 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:18 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-15-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 14/18] perf dso: Hold lock when accessing nsinfo From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org There may be threads racing to update dso->nsinfo: https://lore.kernel.org/linux-perf-users/CAP-5=fWZH20L4kv-BwVtGLwR=Em3AOOT+Q4QGivvQuYn5AsPRg@mail.gmail.com/ Holding the dso->lock avoids use-after-free, memory leaks and other such bugs. Apply the fix in: https://lore.kernel.org/linux-perf-users/20211118193714.2293728-1-irogers@google.com/ of there being a missing nsinfo__put now that the accesses are data race free. Fixes test "Lookup mmap thread" when compiled with address sanitizer. Signed-off-by: Ian Rogers --- tools/perf/builtin-inject.c | 4 ++++ tools/perf/util/annotate.c | 2 ++ tools/perf/util/build-id.c | 12 +++++++++--- tools/perf/util/dso.c | 7 ++++++- tools/perf/util/map.c | 3 +++ tools/perf/util/probe-event.c | 3 +++ tools/perf/util/symbol.c | 2 +- 7 files changed, 28 insertions(+), 5 deletions(-) diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index 2a0f992ca0be..2a914eaf6425 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -433,8 +433,10 @@ static struct dso *findnew_dso(int pid, int tid, const char *filename, } if (dso) { + mutex_lock(&dso->lock); nsinfo__put(dso->nsinfo); dso->nsinfo = nsi; + mutex_unlock(&dso->lock); } else nsinfo__put(nsi); @@ -617,6 +619,7 @@ static int dso__read_build_id(struct dso *dso) if (dso->has_build_id) return 0; + mutex_lock(&dso->lock); nsinfo__mountns_enter(dso->nsinfo, &nsc); if (filename__read_build_id(dso->long_name, &dso->bid) > 0) dso->has_build_id = true; @@ -630,6 +633,7 @@ static int dso__read_build_id(struct dso *dso) free(new_name); } nsinfo__mountns_exit(&nsc); + mutex_unlock(&dso->lock); return dso->has_build_id ? 0 : -1; } diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 29d804d76145..1bbfbc8e1554 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -1697,6 +1697,7 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil */ __symbol__join_symfs(filename, filename_size, dso->long_name); + mutex_lock(&dso->lock); if (access(filename, R_OK) && errno == ENOENT && dso->nsinfo) { char *new_name = filename_with_chroot(dso->nsinfo->pid, filename); @@ -1705,6 +1706,7 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil free(new_name); } } + mutex_unlock(&dso->lock); } free(build_id_path); diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c index ec18ed5caf3e..a839b30c981b 100644 --- a/tools/perf/util/build-id.c +++ b/tools/perf/util/build-id.c @@ -898,11 +898,15 @@ static int filename__read_build_id_ns(const char *filename, static bool dso__build_id_mismatch(struct dso *dso, const char *name) { struct build_id bid; + bool ret = false; - if (filename__read_build_id_ns(name, &bid, dso->nsinfo) < 0) - return false; + mutex_lock(&dso->lock); + if (filename__read_build_id_ns(name, &bid, dso->nsinfo) >= 0) + ret = !dso__build_id_equal(dso, &bid); - return !dso__build_id_equal(dso, &bid); + mutex_unlock(&dso->lock); + + return ret; } static int dso__cache_build_id(struct dso *dso, struct machine *machine, @@ -941,8 +945,10 @@ static int dso__cache_build_id(struct dso *dso, struct machine *machine, if (!is_kallsyms && dso__build_id_mismatch(dso, name)) goto out_free; + mutex_lock(&dso->lock); ret = build_id_cache__add_b(&dso->bid, name, dso->nsinfo, is_kallsyms, is_vdso, proper_name, root_dir); + mutex_unlock(&dso->lock); out_free: free(allocated_name); return ret; diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index c7a5b42d1311..89e1b93cb874 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -501,6 +501,7 @@ static int __open_dso(struct dso *dso, struct machine *machine) if (!name) return -ENOMEM; + mutex_lock(&dso->lock); if (machine) root_dir = machine->root_dir; @@ -541,6 +542,7 @@ static int __open_dso(struct dso *dso, struct machine *machine) unlink(name); out: + mutex_unlock(&dso->lock); free(name); return fd; } @@ -559,8 +561,11 @@ static int open_dso(struct dso *dso, struct machine *machine) int fd; struct nscookie nsc; - if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE) + if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE) { + mutex_lock(&dso->lock); nsinfo__mountns_enter(dso->nsinfo, &nsc); + mutex_unlock(&dso->lock); + } fd = __open_dso(dso, machine); if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE) nsinfo__mountns_exit(&nsc); diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index e0aa4a254583..f3a3d9b3a40d 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -181,7 +181,10 @@ struct map *map__new(struct machine *machine, u64 start, u64 len, if (!(prot & PROT_EXEC)) dso__set_loaded(dso); } + mutex_lock(&dso->lock); + nsinfo__put(dso->nsinfo); dso->nsinfo = nsi; + mutex_unlock(&dso->lock); if (build_id__is_defined(bid)) { dso__set_build_id(dso, bid); diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index 785246ff4179..0c24bc7afbca 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -29,6 +29,7 @@ #include "color.h" #include "map.h" #include "maps.h" +#include "mutex.h" #include "symbol.h" #include #include "trace-event.h" /* For __maybe_unused */ @@ -180,8 +181,10 @@ struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user) map = dso__new_map(target); if (map && map->dso) { + mutex_lock(&map->dso->lock); nsinfo__put(map->dso->nsinfo); map->dso->nsinfo = nsinfo__get(nsi); + mutex_unlock(&map->dso->lock); } return map; } else { diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 656d9b4dd456..a3a165ae933a 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -1791,6 +1791,7 @@ int dso__load(struct dso *dso, struct map *map) char newmapname[PATH_MAX]; const char *map_path = dso->long_name; + mutex_lock(&dso->lock); perfmap = strncmp(dso->name, "/tmp/perf-", 10) == 0; if (perfmap) { if (dso->nsinfo && (dso__find_perf_map(newmapname, @@ -1800,7 +1801,6 @@ int dso__load(struct dso *dso, struct map *map) } nsinfo__mountns_enter(dso->nsinfo, &nsc); - mutex_lock(&dso->lock); /* check again under the dso->lock */ if (dso__loaded(dso)) { From patchwork Tue Aug 23 22:09:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952653 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0EC00C32772 for ; Tue, 23 Aug 2022 22:12:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233219AbiHWWMm (ORCPT ); Tue, 23 Aug 2022 18:12:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233051AbiHWWMG (ORCPT ); Tue, 23 Aug 2022 18:12:06 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0DCD77AC3C for ; Tue, 23 Aug 2022 15:11:05 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-335ff2ef600so261881307b3.18 for ; Tue, 23 Aug 2022 15:11:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=oBQ8fNk49ilPWpXTVU4R9YnuRnoXvu3xuE3Ff0vlliw=; b=ZtQXSc3nLMgidV0RkXHAnNfb+wLDHFqtJ2m2CLNTkr9/mk1Pb2lwWkDQDUIRWunlg+ JiLUE2TGYDpGiLgCmsBuVaVEg1BcTU5heky1RUQMVhyxvVDnaw00zccMmaCPs8/Ydk3o RcGVndc2p39zpI5Ai5vGpaTxsKlv/L3zgINK+6jbONVRdMRxTmm28HFq9/6vHIJGzl+i c52JNYUJjH7z2noWJjC8TFLzblNOHBxhyqaYt3CPSgiJNGkZvPuxbWJPYQ2tYJL2kaAq uvEClhlGisibgw2+5AV1jT+xjgGe7vw33oSqWtBSsX3Rllh1pIbzBIeDCtsqXAgUpbuu PKdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=oBQ8fNk49ilPWpXTVU4R9YnuRnoXvu3xuE3Ff0vlliw=; b=R2KFKvuU+gR2u0QlM8UpDC0RgeypKmi2tf9wIrCXCEAxsUOM6oPWZEeDXC9KUgp1Nj dy6sGHm/MbgjffbJHbrLK2fpf1gBAUlO4k0s1nXKIPQA7u01uKxbex/mbH702xu7UEEI Cj1N9PeoKz8qtJZi0Qz2AlOLvSV8xIxC3cXE3iuH6CrBcynhzBxyZZsW8Tgbdc+p+3YG jvnwvAjL4G64KCu8jtRxhUjHMXbtA7A9iHlCrl0XBaGloxUydy9JtjXOF+nx9MebcmjH z25khLVCmR4mJA6sJv5DwxQg6MwdMzzVl6qcf3Kc4E3oMh/POX7Qy6DA+zSWmsVzFdpN C4/Q== X-Gm-Message-State: ACgBeo1wkZ4m+YIyxVXsaRohTkCRt0jx+439E2z698wJrJkIzOOfTJB7 qakj8c+2ybA0rZGCCDMqUg8f53L/6wQk X-Google-Smtp-Source: AA6agR4b/l59aAfX2CCgdljg/llF2RNbdpziMN0iJsvjeCzIe3p/c4gad5OdNLA9Z/E64+34TQ87ZO+tJ3li X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a0d:dcc7:0:b0:325:314c:9a30 with SMTP id f190-20020a0ddcc7000000b00325314c9a30mr28547074ywe.121.1661292664801; Tue, 23 Aug 2022 15:11:04 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:19 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-16-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 15/18] perf mutex: Add thread safety annotations From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Add thread safety annotations to struct mutex so that when compiled with clang's -Wthread-safety warnings are generated for erroneous lock patterns. NO_THREAD_SAFETY_ANALYSIS is needed for mutex_lock/mutex_unlock as the analysis doesn't under pthread calls. Signed-off-by: Ian Rogers --- tools/perf/util/mutex.c | 2 ++ tools/perf/util/mutex.h | 72 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/mutex.c b/tools/perf/util/mutex.c index d12cf0714268..c936557d8bbb 100644 --- a/tools/perf/util/mutex.c +++ b/tools/perf/util/mutex.c @@ -40,11 +40,13 @@ void mutex_destroy(struct mutex *mtx) } void mutex_lock(struct mutex *mtx) + NO_THREAD_SAFETY_ANALYSIS { CHECK_ERR(pthread_mutex_lock(&mtx->lock)); } void mutex_unlock(struct mutex *mtx) + NO_THREAD_SAFETY_ANALYSIS { CHECK_ERR(pthread_mutex_unlock(&mtx->lock)); } diff --git a/tools/perf/util/mutex.h b/tools/perf/util/mutex.h index 952276ad83bd..6c2062d41a4e 100644 --- a/tools/perf/util/mutex.h +++ b/tools/perf/util/mutex.h @@ -5,11 +5,73 @@ #include #include +/* + * A function-like feature checking macro that is a wrapper around + * `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a + * nonzero constant integer if the attribute is supported or 0 if not. + */ +#ifdef __has_attribute +#define HAVE_ATTRIBUTE(x) __has_attribute(x) +#else +#define HAVE_ATTRIBUTE(x) 0 +#endif + + +#if HAVE_ATTRIBUTE(guarded_by) && HAVE_ATTRIBUTE(pt_guarded_by) && \ + HAVE_ATTRIBUTE(lockable) && HAVE_ATTRIBUTE(exclusive_lock_function) && \ + HAVE_ATTRIBUTE(exclusive_trylock_function) && HAVE_ATTRIBUTE(exclusive_locks_required) && \ + HAVE_ATTRIBUTE(no_thread_safety_analysis) + +/* Documents if a shared field or global variable needs to be protected by a mutex. */ +#define GUARDED_BY(x) __attribute__((guarded_by(x))) + +/* + * Documents if the memory location pointed to by a pointer should be guarded by + * a mutex when dereferencing the pointer. + */ +#define PT_GUARDED_BY(x) __attribute__((pt_guarded_by(x))) + +/* Documents if a type is a lockable type. */ +#define LOCKABLE __attribute__((capability("lockable"))) + +/* Documents functions that acquire a lock in the body of a function, and do not release it. */ +#define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__((exclusive_lock_function(__VA_ARGS__))) + +/* + * Documents functions that expect a lock to be held on entry to the function, + * and release it in the body of the function. + */ +#define UNLOCK_FUNCTION(...) __attribute__((unlock_function(__VA_ARGS__))) + +/* Documents functions that try to acquire a lock, and return success or failure. */ +#define EXCLUSIVE_TRYLOCK_FUNCTION(...) \ + __attribute__((exclusive_trylock_function(__VA_ARGS__))) + + +/* Documents a function that expects a mutex to be held prior to entry. */ +#define EXCLUSIVE_LOCKS_REQUIRED(...) __attribute__((exclusive_locks_required(__VA_ARGS__))) + +/* Turns off thread safety checking within the body of a particular function. */ +#define NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis)) + +#else + +#define GUARDED_BY(x) +#define PT_GUARDED_BY(x) +#define LOCKABLE +#define EXCLUSIVE_LOCK_FUNCTION(...) +#define UNLOCK_FUNCTION(...) +#define EXCLUSIVE_TRYLOCK_FUNCTION(...) +#define EXCLUSIVE_LOCKS_REQUIRED(...) +#define NO_THREAD_SAFETY_ANALYSIS + +#endif + /* * A wrapper around the mutex implementation that allows perf to error check * usage, etc. */ -struct mutex { +struct LOCKABLE mutex { pthread_mutex_t lock; }; @@ -25,9 +87,9 @@ struct cond { void mutex_init(struct mutex *mtx, bool pshared); void mutex_destroy(struct mutex *mtx); -void mutex_lock(struct mutex *mtx); -void mutex_unlock(struct mutex *mtx); -bool mutex_trylock(struct mutex *mtx); +void mutex_lock(struct mutex *mtx) EXCLUSIVE_LOCK_FUNCTION(*mtx); +void mutex_unlock(struct mutex *mtx) UNLOCK_FUNCTION(*mtx); +bool mutex_trylock(struct mutex *mtx) EXCLUSIVE_TRYLOCK_FUNCTION(true, *mtx); /* * Initialize the cond struct, if pshared is set then specify the process-shared @@ -36,7 +98,7 @@ bool mutex_trylock(struct mutex *mtx); void cond_init(struct cond *cnd, bool pshared); void cond_destroy(struct cond *cnd); -void cond_wait(struct cond *cnd, struct mutex *mtx); +void cond_wait(struct cond *cnd, struct mutex *mtx) EXCLUSIVE_LOCKS_REQUIRED(mtx); void cond_signal(struct cond *cnd); void cond_broadcast(struct cond *cnd); From patchwork Tue Aug 23 22:09:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952654 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 348C9C32772 for ; Tue, 23 Aug 2022 22:12:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232908AbiHWWMz (ORCPT ); Tue, 23 Aug 2022 18:12:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232965AbiHWWMW (ORCPT ); Tue, 23 Aug 2022 18:12:22 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 618897C192 for ; Tue, 23 Aug 2022 15:11:12 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id q7-20020a63e947000000b004297f1e1f86so6665868pgj.12 for ; Tue, 23 Aug 2022 15:11:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=XPuMrEmpVaX0CO7dyFJBelQFPDb3Vcz70HxeyCf+Aoo=; b=SJ+8921Gu9b3/VmEZj1EADQgc2FMFPsv+iqRwwG5/DLfP5x1WL5CBnZHQEUET2dwDf XCIywGXcF2XdS/+7+k9pj4eDl7frNGPb65KPPdKE101o4g6KTeZpDK6CGNKG00b0ITNl zyDdmGb4gNTgLzAqH4Tjy2oJzOgCcHLXJBlNtbKjB6XEVu49IDYwo+amTX33qly9RRwv rwDvxWdmjvQRM8UsDDWP5uyogTrGHTO2VgNeNSTn06gT7UylBKz+LA4jtwbG1NOPNYJo dzhQ6/kVIZUwVds8WBpBKWKtDjgoVl+bezYgHud5Lxcb045ct0ESDcPS4o48YA9GuyiX cz8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=XPuMrEmpVaX0CO7dyFJBelQFPDb3Vcz70HxeyCf+Aoo=; b=V/uPrBj3uz+wC205v8gYYm1j9wgoJkiHk4D9KOcw8nuXpOqrtRAjNWDeMGvqokT6e1 QM/F5hjb34IZ7BU4hy8TBHYxpTQpbtyLl/uFn/715Ih9Fc7cfNkTB0FKLNDjeYQ1PQjk s12xq4K9DRjC33V8bW/Mwa+yVu7tH2KBl7g0QFoEU3Do7ZBBKZ/lIi7r2aqnlDoyKqY1 g1FZbq1/B4EIHyznBxYzXZ9JMgNZ4zBPHFUmsxoO2BqY+b5tpIO5e0FC4JONAXDDAzKI AxoBel8sXu6c8O66dq1mUQ99+q0GVjtgXXuuq4QtZtDm6pGKRhFp6XB6LlwDozQpenEz c6pg== X-Gm-Message-State: ACgBeo0ZaVZuRVKmOThMCEo8fSRimHcg04bFzDJ8Hsirs6HwhXdc8GrT 6OFHQ/8qS12Fl2SwinD0yUZbD3inEuyt X-Google-Smtp-Source: AA6agR6/lrrbVHDvzAtqYEgzKbOP+OGUUywQemY8hrKQaj9m/YTAfzeBeCgzyMm2qKvm/cIwWjLpEYwnDh/O X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a17:902:d505:b0:173:111a:3023 with SMTP id b5-20020a170902d50500b00173111a3023mr1535558plg.32.1661292670931; Tue, 23 Aug 2022 15:11:10 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:20 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-17-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 16/18] perf sched: Fixes for thread safety analysis From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Add annotations to describe lock behavior. Add unlocks so that mutexes aren't conditionally held on exit from perf_sched__replay. Add an exit variable so that thread_func can terminate, rather than leaving the threads blocked on mutexes. Signed-off-by: Ian Rogers --- tools/perf/builtin-sched.c | 46 ++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index 0f52f73be896..ce8497d39f9c 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -246,6 +246,7 @@ struct perf_sched { const char *time_str; struct perf_time_interval ptime; struct perf_time_interval hist_time; + volatile bool thread_funcs_exit; }; /* per thread run time data */ @@ -633,31 +634,34 @@ static void *thread_func(void *ctx) prctl(PR_SET_NAME, comm2); if (fd < 0) return NULL; -again: - ret = sem_post(&this_task->ready_for_work); - BUG_ON(ret); - mutex_lock(&sched->start_work_mutex); - mutex_unlock(&sched->start_work_mutex); - cpu_usage_0 = get_cpu_usage_nsec_self(fd); + while (!sched->thread_funcs_exit) { + ret = sem_post(&this_task->ready_for_work); + BUG_ON(ret); + mutex_lock(&sched->start_work_mutex); + mutex_unlock(&sched->start_work_mutex); - for (i = 0; i < this_task->nr_events; i++) { - this_task->curr_event = i; - perf_sched__process_event(sched, this_task->atoms[i]); - } + cpu_usage_0 = get_cpu_usage_nsec_self(fd); - cpu_usage_1 = get_cpu_usage_nsec_self(fd); - this_task->cpu_usage = cpu_usage_1 - cpu_usage_0; - ret = sem_post(&this_task->work_done_sem); - BUG_ON(ret); + for (i = 0; i < this_task->nr_events; i++) { + this_task->curr_event = i; + perf_sched__process_event(sched, this_task->atoms[i]); + } - mutex_lock(&sched->work_done_wait_mutex); - mutex_unlock(&sched->work_done_wait_mutex); + cpu_usage_1 = get_cpu_usage_nsec_self(fd); + this_task->cpu_usage = cpu_usage_1 - cpu_usage_0; + ret = sem_post(&this_task->work_done_sem); + BUG_ON(ret); - goto again; + mutex_lock(&sched->work_done_wait_mutex); + mutex_unlock(&sched->work_done_wait_mutex); + } + return NULL; } static void create_tasks(struct perf_sched *sched) + EXCLUSIVE_LOCK_FUNCTION(sched->start_work_mutex) + EXCLUSIVE_LOCK_FUNCTION(sched->work_done_wait_mutex) { struct task_desc *task; pthread_attr_t attr; @@ -687,6 +691,8 @@ static void create_tasks(struct perf_sched *sched) } static void wait_for_tasks(struct perf_sched *sched) + EXCLUSIVE_LOCKS_REQUIRED(sched->work_done_wait_mutex) + EXCLUSIVE_LOCKS_REQUIRED(sched->start_work_mutex) { u64 cpu_usage_0, cpu_usage_1; struct task_desc *task; @@ -738,6 +744,8 @@ static void wait_for_tasks(struct perf_sched *sched) } static void run_one_test(struct perf_sched *sched) + EXCLUSIVE_LOCKS_REQUIRED(sched->work_done_wait_mutex) + EXCLUSIVE_LOCKS_REQUIRED(sched->start_work_mutex) { u64 T0, T1, delta, avg_delta, fluct; @@ -3309,11 +3317,15 @@ static int perf_sched__replay(struct perf_sched *sched) print_task_traces(sched); add_cross_task_wakeups(sched); + sched->thread_funcs_exit = false; create_tasks(sched); printf("------------------------------------------------------------\n"); for (i = 0; i < sched->replay_repeat; i++) run_one_test(sched); + sched->thread_funcs_exit = true; + mutex_unlock(&sched->start_work_mutex); + mutex_unlock(&sched->work_done_wait_mutex); return 0; } From patchwork Tue Aug 23 22:09:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952655 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A3A27C32772 for ; Tue, 23 Aug 2022 22:13:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233210AbiHWWNM (ORCPT ); Tue, 23 Aug 2022 18:13:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233202AbiHWWM2 (ORCPT ); Tue, 23 Aug 2022 18:12:28 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F8C57C771 for ; Tue, 23 Aug 2022 15:11:18 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-336c3b72da5so256065007b3.6 for ; Tue, 23 Aug 2022 15:11:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=K4oRiNn+G2rdtJsE6L2b3oylFpgb/KpBFNxvRzPUh68=; b=mPAPh3cEvyWdIZIF7pmnVaWOxwkdb/agfVPZLOISUSLKAOcBm6GD0GO984MB0BbI3h nLeod1OUEJerQDtGS66iqteAj4Zm/QF4FHJoo6k5jCF7oa7npVly5Z+afdgkK7DojHeE K8XhJDLxpv6UuKYEhvh3OXaCnHSBd81MS4q6MwEPHtSUGiZMhjf9hc0OnS34FCadQuwz 1SoHhjdmqixqY3VKmSmcDFXYPCR4Nxk+7nq3/U2G43pCTfjljlWU06JiNxt6XIhCpDPk pZzBl3Mm1ZUgjxzQpUOIwQluQR+T8AvcaJs7Oz2ymxKbM2XVkwRHeMA2lI1ymtHkqfVg Lvog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=K4oRiNn+G2rdtJsE6L2b3oylFpgb/KpBFNxvRzPUh68=; b=I7NLvWipiUNz7GAX4U1d/9zNgfuzsUpfp/Flf6g8W3hOH+qv1Ws40NjOvPj6DxYAGQ v+F16gWu/Wxdy3Aom3oEB+aqLqdWL0nkX4H5p1SJ0BX8sErUwDV2NkYf741oR7KoNhu/ JRsbqO/pNGDd9AiT21kI/syE9OBunOtfgE03MelMTKJV2V6CJxONdXiHq8D1Hb/idfQ9 BZ+1/MXBkxgGWs7Javq7R5ijIIUJTJK6DTydTTA+KRNBtmNgLiPKS7GECSAOAG1GivZK 92qLo+jZ7upvQxEWBfRFauU28exrGd+bWKDQB1mdTI+ku7dLyMJt+9ixXE3s783T2Y0R 0VmQ== X-Gm-Message-State: ACgBeo17epQLtqYj94+qjK49q7D3fwAm2NS/gV05hiaueg9igbSyhVRz dPeBi32WiFXUBA7SGc4ZtWFDtSpi+40j X-Google-Smtp-Source: AA6agR7GhAN18bhNP7OEUGzMVoWqbLNjvwo0Kz5W4Z6P+DPRLh4sVcdbaQ0WjCi4/p46vI8Uk3tw7Xot0K2W X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a81:124c:0:b0:334:d633:f3c9 with SMTP id 73-20020a81124c000000b00334d633f3c9mr28335053yws.437.1661292677181; Tue, 23 Aug 2022 15:11:17 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:21 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-18-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 17/18] perf top: Fixes for thread safety analysis From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Add annotations to describe lock behavior. Signed-off-by: Ian Rogers --- tools/perf/builtin-top.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 3757292bfe86..e832f04e3076 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -196,6 +196,7 @@ static void perf_top__record_precise_ip(struct perf_top *top, struct hist_entry *he, struct perf_sample *sample, struct evsel *evsel, u64 ip) + EXCLUSIVE_LOCKS_REQUIRED(he->hists->lock) { struct annotation *notes; struct symbol *sym = he->ms.sym; @@ -724,13 +725,13 @@ static void *display_thread(void *arg) static int hist_iter__top_callback(struct hist_entry_iter *iter, struct addr_location *al, bool single, void *arg) + EXCLUSIVE_LOCKS_REQUIRED(iter->he->hists->lock) { struct perf_top *top = arg; - struct hist_entry *he = iter->he; struct evsel *evsel = iter->evsel; if (perf_hpp_list.sym && single) - perf_top__record_precise_ip(top, he, iter->sample, evsel, al->addr); + perf_top__record_precise_ip(top, iter->he, iter->sample, evsel, al->addr); hist__account_cycles(iter->sample->branch_stack, al, iter->sample, !(top->record_opts.branch_stack & PERF_SAMPLE_BRANCH_ANY), From patchwork Tue Aug 23 22:09:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12952656 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5BECCC32789 for ; Tue, 23 Aug 2022 22:13:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232419AbiHWWNY (ORCPT ); Tue, 23 Aug 2022 18:13:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232311AbiHWWMo (ORCPT ); Tue, 23 Aug 2022 18:12:44 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3FBED7D1E3 for ; Tue, 23 Aug 2022 15:11:26 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-334d894afd8so261992967b3.19 for ; Tue, 23 Aug 2022 15:11:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=vpCoIyKRZsG9sSnwmRTlOTB6YRSjIw+Qbk8jvYcuaVE=; b=XvSozB/DWuMKSbNHJ9l9xde2KRhs2B7hPE+zZEBJfJd7Fr6E4lDsopXJvIdm2+oO9j jr31g9cAChKqFWsNNLIHJdF30A0bR3Cv2hneJzi8ReQowFjtktbkfhgyuILUR7cXFQvA PjhFeFqrLVQ6dfX4gTRChIm+bE3ZoENvEXXD1WXoSW40LGANh/e2+thBWHo9naOHrsJq PyBkagsL2dz4ZfnZRB88Ff4UDmD7DseC9LNSz8vpLyrAH8a45cAWOGDS4JmAcqncLx9g k6mivrsUv+Wz/1aLhsPxsuwDEOf0RevYE/lwTzq/W/n8zvM1Bbnbo1lXkXaDP2Gg24/M AFMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=vpCoIyKRZsG9sSnwmRTlOTB6YRSjIw+Qbk8jvYcuaVE=; b=pTA/94CBL8yrQXR+uCrxT786KDKWM6GK6I55bCCxxZLFOlFOaRnzArlzI83mUiAx2s bQop/BWzMEEZK/lcPVW9/ETSg1en1HKoYWvPBkTekdUmap0/tmHAGw/PoulUMHBkguyI JJvRGdYQ7YpvbwY2V8Oh+LKtjg123XKEzciAZS5pwaVi4f6/MQek7jFECruFUyxupNwg VaK7w2yhjU5DnfKrylCEcgm0Xb8O7i3ftIUV/DR6ep59aafe1Qg8QfKG+36zafmvBtHe f0uWsNXPPckW12iYYVmFzkxU67auEu9Vh1P/ZYaJcGYQqKJLlJVl5XmkOULkV6vXiQkl +uqQ== X-Gm-Message-State: ACgBeo1YotQu8sdsFH7/Eh9ei5vlUpjIfM5wOmrOai4TGnOLMEVyzV9f Q3AxK3mezbMPXpAY3OpnNNczy5dYNioX X-Google-Smtp-Source: AA6agR6f5ty3lYMH2Y4qqlqwT9nA65Tc33n67UyVYg+Dgbepp9Cr8MCejsQA/hDfvv7R/YglXQcwvFGYYPpa X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a25:cf4b:0:b0:68e:fea1:9fdc with SMTP id f72-20020a25cf4b000000b0068efea19fdcmr26259610ybg.643.1661292684631; Tue, 23 Aug 2022 15:11:24 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:22 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-19-irogers@google.com> Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 18/18] perf build: Enable -Wthread-safety with clang From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , " =?utf-8?q?Andr=C3=A9_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 , " =?utf-8?q?Martin_Li=C5=A1ka?= " , 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@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org If building with clang then enable -Wthread-safety warnings. Signed-off-by: Ian Rogers --- tools/perf/Makefile.config | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config index 0661a1cf9855..0ef6f572485d 100644 --- a/tools/perf/Makefile.config +++ b/tools/perf/Makefile.config @@ -19,6 +19,11 @@ detected_var = $(shell echo "$(1)=$($(1))" >> $(OUTPUT).config-detected) CFLAGS := $(EXTRA_CFLAGS) $(filter-out -Wnested-externs,$(EXTRA_WARNINGS)) HOSTCFLAGS := $(filter-out -Wnested-externs,$(EXTRA_WARNINGS)) +# Enabled Wthread-safety analysis for clang builds. +ifeq ($(CC_NO_CLANG), 0) + CFLAGS += -Wthread-safety +endif + include $(srctree)/tools/scripts/Makefile.arch $(call detected_var,SRCARCH)