linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ian Rogers <irogers@google.com>
Cc: "Peter Zijlstra" <peterz@infradead.org>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Mark Rutland" <mark.rutland@arm.com>,
	"Alexander Shishkin" <alexander.shishkin@linux.intel.com>,
	"Jiri Olsa" <jolsa@kernel.org>,
	"Namhyung Kim" <namhyung@kernel.org>,
	"Thomas Gleixner" <tglx@linutronix.de>,
	"Darren Hart" <dvhart@infradead.org>,
	"Davidlohr Bueso" <dave@stgolabs.net>,
	"André Almeida" <andrealmeid@collabora.com>,
	"James Clark" <james.clark@arm.com>,
	"John Garry" <john.garry@huawei.com>,
	"Riccardo Mancini" <rickyman7@gmail.com>,
	"Yury Norov" <yury.norov@gmail.com>,
	"Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Jin Yao" <yao.jin@linux.intel.com>,
	"Adrian Hunter" <adrian.hunter@intel.com>,
	"Leo Yan" <leo.yan@linaro.org>, "Andi Kleen" <ak@linux.intel.com>,
	"Thomas Richter" <tmricht@linux.ibm.com>,
	"Kan Liang" <kan.liang@linux.intel.com>,
	"Madhavan Srinivasan" <maddy@linux.ibm.com>,
	"Shunsuke Nakamura" <nakamura.shun@fujitsu.com>,
	"Song Liu" <song@kernel.org>,
	"Masami Hiramatsu" <mhiramat@kernel.org>,
	"Steven Rostedt" <rostedt@goodmis.org>,
	"Miaoqian Lin" <linmq006@gmail.com>,
	"Stephen Brennan" <stephen.s.brennan@oracle.com>,
	"Kajol Jain" <kjain@linux.ibm.com>,
	"Alexey Bayduraev" <alexey.v.bayduraev@linux.intel.com>,
	"German Gomez" <german.gomez@arm.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Eric Dumazet" <edumazet@google.com>,
	"Dmitry Vyukov" <dvyukov@google.com>,
	"Hao Luo" <haoluo@google.com>,
	eranian@google.com
Subject: Re: [PATCH v3 03/22] perf dso: Make lock error check and add BUG_ONs
Date: Fri, 11 Feb 2022 16:21:08 -0300	[thread overview]
Message-ID: <Yga3JNhTfSBR3btI@kernel.org> (raw)
In-Reply-To: <CAP-5=fWZTmbQpmoXRKt9HdMU-E_B2uo5b4BHOW6RC6w5VkQ0AQ@mail.gmail.com>

Em Fri, Feb 11, 2022 at 09:43:19AM -0800, Ian Rogers escreveu:
> On Fri, Feb 11, 2022 at 9:13 AM Arnaldo Carvalho de Melo
> <acme@kernel.org> wrote:
> >
> > Em Fri, Feb 11, 2022 at 02:33:56AM -0800, Ian Rogers escreveu:
> > > Make the pthread mutex on dso use the error check type. This allows
> > > deadlock checking via the return type. Assert the returned value from
> > > mutex lock is always 0.
> >
> > I think this is too blunt/pervasive source code wise, perhaps we should
> > wrap this like its done with rwsem in tools/perf/util/rwsem.h to get
> > away from pthreads primitives and make the source code look more like
> > a kernel one and then, taking advantage of the so far ideologic
> > needless indirection, add this BUG_ON if we build with "DEBUG=1" or
> > something, wdyt?
> 

> My concern with semaphores is that they are a concurrency primitive

I'm not suggesting we switch over to semaphores, just to use the same
technique of wrapping pthread_mutex_t with some other API that then
allows us to add these BUG_ON() calls without polluting the source code
in many places.

- Arnaldo

> that has more flexibility and power than a mutex. I like a mutex as it
> is quite obvious what is going on and that is good from a tooling
> point of view. A deadlock with two mutexes is easy to understand. On a
> semaphore, were we using it like a condition variable? There's more to
> figure out. I also like the idea of compiling the perf command with
> emscripten, we could then generate say perf annotate output in your
> web browser. Emscripten has implementations of standard posix
> libraries including pthreads, but we may need to have two approaches
> in the perf code if we want to compile with emscripten and use
> semaphores when targeting linux.
> 
> Where this change comes from is that I worried that extending the
> locked regions to cover the race that'd been found would then expose
> the kind of recursive deadlock that pthread mutexes all too willingly
> allow. With this code we at least see the bug and don't just hang. I
> don't think we need the change to the mutexes for this change, but we
> do need to extend the regions to fix the data race.
> 
> Let me know how you prefer it and I can roll it into a v4 version.
> 
> Thanks,
> Ian
> 
> > - Arnaldo
> >
> > > Signed-off-by: Ian Rogers <irogers@google.com>
> > > ---
> > >  tools/perf/util/dso.c    | 12 +++++++++---
> > >  tools/perf/util/symbol.c |  2 +-
> > >  2 files changed, 10 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
> > > index 9cc8a1772b4b..6beccffeef7b 100644
> > > --- a/tools/perf/util/dso.c
> > > +++ b/tools/perf/util/dso.c
> > > @@ -784,7 +784,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);
> > > +     BUG_ON(pthread_mutex_lock(&dso->lock) != 0);
> > >       while (next) {
> > >               struct dso_cache *cache;
> > >
> > > @@ -830,7 +830,7 @@ dso_cache__insert(struct dso *dso, struct dso_cache *new)
> > >       struct dso_cache *cache;
> > >       u64 offset = new->offset;
> > >
> > > -     pthread_mutex_lock(&dso->lock);
> > > +     BUG_ON(pthread_mutex_lock(&dso->lock) != 0);
> > >       while (*p != NULL) {
> > >               u64 end;
> > >
> > > @@ -1259,6 +1259,8 @@ struct dso *dso__new_id(const char *name, struct dso_id *id)
> > >       struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
> > >
> > >       if (dso != NULL) {
> > > +             pthread_mutexattr_t lock_attr;
> > > +
> > >               strcpy(dso->name, name);
> > >               if (id)
> > >                       dso->id = *id;
> > > @@ -1286,8 +1288,12 @@ 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);
> > > +             pthread_mutexattr_init(&lock_attr);
> > > +             pthread_mutexattr_settype(&lock_attr, PTHREAD_MUTEX_ERRORCHECK);
> > > +             pthread_mutex_init(&dso->lock, &lock_attr);
> > > +             pthread_mutexattr_destroy(&lock_attr);
> > >               refcount_set(&dso->refcnt, 1);
> > > +
> > >       }
> > >
> > >       return dso;
> > > diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
> > > index b2ed3140a1fa..43f47532696f 100644
> > > --- a/tools/perf/util/symbol.c
> > > +++ b/tools/perf/util/symbol.c
> > > @@ -1783,7 +1783,7 @@ int dso__load(struct dso *dso, struct map *map)
> > >       }
> > >
> > >       nsinfo__mountns_enter(dso->nsinfo, &nsc);
> > > -     pthread_mutex_lock(&dso->lock);
> > > +     BUG_ON(pthread_mutex_lock(&dso->lock) != 0);
> > >
> > >       /* check again under the dso->lock */
> > >       if (dso__loaded(dso)) {
> > > --
> > > 2.35.1.265.g69c8d7142f-goog
> >
> > --
> >
> > - Arnaldo

-- 

- Arnaldo

  reply	other threads:[~2022-02-11 19:21 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-11 10:33 [PATCH v3 00/22] Reference count checker and related fixes Ian Rogers
2022-02-11 10:33 ` [PATCH v3 01/22] perf cpumap: Migrate to libperf cpumap api Ian Rogers
2022-02-11 17:02   ` Arnaldo Carvalho de Melo
2022-02-11 10:33 ` [PATCH v3 02/22] perf cpumap: Use for each loop Ian Rogers
2022-02-11 17:04   ` Arnaldo Carvalho de Melo
2022-02-11 10:33 ` [PATCH v3 03/22] perf dso: Make lock error check and add BUG_ONs Ian Rogers
2022-02-11 17:13   ` Arnaldo Carvalho de Melo
2022-02-11 17:43     ` Ian Rogers
2022-02-11 19:21       ` Arnaldo Carvalho de Melo [this message]
2022-02-11 19:35         ` Ian Rogers
2022-02-12 15:48           ` Arnaldo Carvalho de Melo
2022-02-12 15:49             ` Arnaldo Carvalho de Melo
2022-02-12 20:59               ` Ian Rogers
2022-02-11 10:33 ` [PATCH v3 04/22] perf dso: Hold lock when accessing nsinfo Ian Rogers
2022-02-11 17:14   ` Arnaldo Carvalho de Melo
2022-02-12 11:30   ` Jiri Olsa
2022-02-11 10:33 ` [PATCH v3 05/22] perf maps: Use a pointer for kmaps Ian Rogers
2022-02-11 17:23   ` Arnaldo Carvalho de Melo
2022-02-14 19:45     ` Arnaldo Carvalho de Melo
2022-02-11 10:33 ` [PATCH v3 06/22] perf test: Use pointer for maps Ian Rogers
2022-02-11 17:24   ` Arnaldo Carvalho de Melo
2022-02-14 19:48   ` Arnaldo Carvalho de Melo
2022-02-14 19:50     ` Arnaldo Carvalho de Melo
2022-02-11 10:34 ` [PATCH v3 07/22] perf maps: Reduce scope of init and exit Ian Rogers
2022-02-11 17:26   ` Arnaldo Carvalho de Melo
2022-02-11 10:34 ` [PATCH v3 08/22] perf maps: Move maps code to own C file Ian Rogers
2022-02-11 17:27   ` Arnaldo Carvalho de Melo
2022-02-14 19:58   ` Arnaldo Carvalho de Melo
2022-02-11 10:34 ` [PATCH v3 09/22] perf map: Add const to map_ip and unmap_ip Ian Rogers
2022-02-11 17:28   ` Arnaldo Carvalho de Melo
2022-02-11 10:34 ` [PATCH v3 10/22] perf map: Make map__contains_symbol args const Ian Rogers
2022-02-11 17:28   ` Arnaldo Carvalho de Melo
2022-02-11 10:34 ` [PATCH v3 11/22] perf map: Move map list node into symbol Ian Rogers
2022-02-11 10:34 ` [PATCH v3 12/22] perf maps: Remove rb_node from struct map Ian Rogers
2022-02-16 14:08   ` Arnaldo Carvalho de Melo
2022-02-16 17:36     ` Ian Rogers
2022-02-16 20:12       ` Arnaldo Carvalho de Melo
2022-02-16 22:07         ` Ian Rogers
2022-02-11 10:34 ` [PATCH v3 13/22] perf namespaces: Add functions to access nsinfo Ian Rogers
2022-02-11 17:31   ` Arnaldo Carvalho de Melo
2022-02-11 10:34 ` [PATCH v3 14/22] perf maps: Add functions to access maps Ian Rogers
2022-02-11 17:33   ` Arnaldo Carvalho de Melo
2022-02-11 10:34 ` [PATCH v3 16/22] perf test: Add extra diagnostics to maps test Ian Rogers
2022-02-11 10:34 ` [PATCH v3 17/22] perf map: Changes to reference counting Ian Rogers
2022-02-12  8:45   ` Masami Hiramatsu
2022-02-12 20:48     ` Ian Rogers
2022-02-14  2:00       ` Masami Hiramatsu
2022-02-14 18:56       ` Arnaldo Carvalho de Melo
2022-02-11 10:34 ` [PATCH v3 18/22] libperf: Add reference count checking macros Ian Rogers
2022-02-11 10:34 ` [PATCH v3 19/22] perf cpumap: Add reference count checking Ian Rogers
2022-02-11 10:34 ` [PATCH v3 20/22] perf namespaces: " Ian Rogers
2022-02-11 10:34 ` [PATCH v3 21/22] perf maps: " Ian Rogers
2022-02-11 10:34 ` [PATCH v3 22/22] perf map: " Ian Rogers

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=Yga3JNhTfSBR3btI@kernel.org \
    --to=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=ak@linux.intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=alexey.v.bayduraev@linux.intel.com \
    --cc=andrealmeid@collabora.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=dave@stgolabs.net \
    --cc=dvhart@infradead.org \
    --cc=dvyukov@google.com \
    --cc=edumazet@google.com \
    --cc=eranian@google.com \
    --cc=german.gomez@arm.com \
    --cc=haoluo@google.com \
    --cc=irogers@google.com \
    --cc=james.clark@arm.com \
    --cc=john.garry@huawei.com \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=kjain@linux.ibm.com \
    --cc=leo.yan@linaro.org \
    --cc=linmq006@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=maddy@linux.ibm.com \
    --cc=mark.rutland@arm.com \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=nakamura.shun@fujitsu.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rickyman7@gmail.com \
    --cc=rostedt@goodmis.org \
    --cc=song@kernel.org \
    --cc=stephen.s.brennan@oracle.com \
    --cc=tglx@linutronix.de \
    --cc=tmricht@linux.ibm.com \
    --cc=yao.jin@linux.intel.com \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

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

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