From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>,
Adrian Hunter <adrian.hunter@intel.com>,
linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
Ingo Molnar <mingo@redhat.com>,
Namhyung Kim <namhyung@kernel.org>, Jiri Olsa <jolsa@redhat.com>,
Wang Nan <wangnan0@huawei.com>,
Alexei Starovoitov <ast@plumgrid.com>
Subject: Re: [PATCH perf/core 00/22] perf refcnt debugger API and fixes
Date: Fri, 11 Dec 2015 19:26:21 -0300 [thread overview]
Message-ID: <20151211222621.GE6843@kernel.org> (raw)
In-Reply-To: <20151209134138.GB15864@kernel.org>
Em Wed, Dec 09, 2015 at 10:41:38AM -0300, Arnaldo Carvalho de Melo escreveu:
> Em Wed, Dec 09, 2015 at 11:10:48AM +0900, Masami Hiramatsu escreveu:
> > General refcnt miscodings
> > =========================
> >
> > BTW, while applying this change, I've found that there are refcnt
> > coding mismatches in those code and most of the bugs come from those
> > mismatches.
> >
> > - The reference counter can be initialized by 0 or 1.
> > - If 0 is chosen, caller have to get it and free it if failed to
> > register appropriately.
> > - If 1 is chosen, caller doesn't need to get it, but when exits the
> > caller, it has to put it. (except for returning the object itself)
> > - The application should choose either one as its policy, to avoid
> > confusion.
> >
> > perf tools mixes it up (moreover, it initializes 2 in a case) and
> > caller usually forgets to put it (it is not surprising, because too
> > many "put" usually cause SEGV by accessing freed object.)
>
> Well, we should fix the bugs and document why some initialization is
> deemed better than a single initialization style.
> For instance, if we know that we will keep multiple references straight
> away, then we could init it with some value different that preferred,
> that I aggee, is 1, i.e. the usual way for some constructor is:
>
>
> struct foo *foo__new()
> {
> struct foo *f = malloc(sizeof (*f));
>
> if (f) {
> atomic_set(&f->refcnt, 1);
> }
>
> return f;
> }
>
> void *foo__delete(struct foo *f)
> {
> free(f);
> }
>
> void foo__put(struct foo *f)
> {
> if (f && atomic_dec_and_test(f->refcnt))
> foo__delete(f);
> }
>
>
> Then, when using if, and before adding it to any other tree, list, i.e.
> a container, we do:
>
> struct foo *f = foo__new();
>
> /*
> * assume f was allocated and then the function allocating it
> * failed, when it bails out it should just do:
> */
> out_error:
> foo__put(f);
>
> And that will make it hit zero, which will call foo__delete(), case
> closed.
>
> > As far as I can see, cgroup_sel, perf_mmap(this is initialized 0 or 2...),
> > thread, and comm_str are initialized by 0. Others are initialized by 1.
> >
> > So, I'd like to suggest that we choose one policy and cleanup the code.
> > I recommend to use init by 1 policy, because anyway caller has to get
>
> See above, if nothing else recommends using a different value, use 1.
So, I think I fixed the thread->refcnt case, please take a look at my
perf/core branch, more specifically this patch:
From a9a8442c64a86599600ecb2ae0f296b5c93e2a98 Mon Sep 17 00:00:00 2001
From: Arnaldo Carvalho de Melo <acme@redhat.com>
Date: Fri, 11 Dec 2015 19:11:23 -0300
Subject: [PATCH 1/1] perf thread: Fix reference count initial state
We should always return from thread__new(), the constructor, with the
object with a reference count of one, so that:
struct thread *thread = thread__new();
thread__put(thread);
Will call thread__delete().
If any reference is made to that 'thread' variable, it better use
thread__get(thread) to hold a reference.
We were returning with thread->refcnt set to zero, fix it and some cases
where thread__delete() was being called, which were not a problem
because just one reference was being used, now that we set it to 1, use
thread__put() instead.
Reported-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-4b9mkuk66to4ecckpmpvqx6s@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
tools/perf/util/intel-pt.c | 4 ++--
tools/perf/util/machine.c | 19 ++++++++++++-------
tools/perf/util/thread.c | 10 ++++++++--
3 files changed, 22 insertions(+), 11 deletions(-)
diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
index 97f963a3dcb9..81a2eb77ba7f 100644
--- a/tools/perf/util/intel-pt.c
+++ b/tools/perf/util/intel-pt.c
@@ -1744,7 +1744,7 @@ static void intel_pt_free(struct perf_session *session)
auxtrace_heap__free(&pt->heap);
intel_pt_free_events(session);
session->auxtrace = NULL;
- thread__delete(pt->unknown_thread);
+ thread__put(pt->unknown_thread);
free(pt);
}
@@ -2153,7 +2153,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,
return 0;
err_delete_thread:
- thread__delete(pt->unknown_thread);
+ thread__zput(pt->unknown_thread);
err_free_queues:
intel_pt_log_disable();
auxtrace_queues__free(&pt->queues);
diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index 1407d5107480..ad79297c76c8 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -352,13 +352,18 @@ static void machine__update_thread_pid(struct machine *machine,
}
th->mg = map_groups__get(leader->mg);
-
+out_put:
+ thread__put(leader);
return;
-
out_err:
pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
+ goto out_put;
}
+/*
+ * Caller must eventually drop thread->refcnt returned with a successfull
+ * lookup/new thread inserted.
+ */
static struct thread *____machine__findnew_thread(struct machine *machine,
pid_t pid, pid_t tid,
bool create)
@@ -376,7 +381,7 @@ static struct thread *____machine__findnew_thread(struct machine *machine,
if (th != NULL) {
if (th->tid == tid) {
machine__update_thread_pid(machine, th, pid);
- return th;
+ return thread__get(th);
}
machine->last_match = NULL;
@@ -389,7 +394,7 @@ static struct thread *____machine__findnew_thread(struct machine *machine,
if (th->tid == tid) {
machine->last_match = th;
machine__update_thread_pid(machine, th, pid);
- return th;
+ return thread__get(th);
}
if (tid < th->tid)
@@ -417,7 +422,7 @@ static struct thread *____machine__findnew_thread(struct machine *machine,
if (thread__init_map_groups(th, machine)) {
rb_erase_init(&th->rb_node, &machine->threads);
RB_CLEAR_NODE(&th->rb_node);
- thread__delete(th);
+ thread__put(th);
return NULL;
}
/*
@@ -441,7 +446,7 @@ struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
struct thread *th;
pthread_rwlock_wrlock(&machine->threads_lock);
- th = thread__get(__machine__findnew_thread(machine, pid, tid));
+ th = __machine__findnew_thread(machine, pid, tid);
pthread_rwlock_unlock(&machine->threads_lock);
return th;
}
@@ -451,7 +456,7 @@ struct thread *machine__find_thread(struct machine *machine, pid_t pid,
{
struct thread *th;
pthread_rwlock_rdlock(&machine->threads_lock);
- th = thread__get(____machine__findnew_thread(machine, pid, tid, false));
+ th = ____machine__findnew_thread(machine, pid, tid, false);
pthread_rwlock_unlock(&machine->threads_lock);
return th;
}
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 0a9ae8014729..dfd00c6dad6e 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -19,8 +19,10 @@ int thread__init_map_groups(struct thread *thread, struct machine *machine)
thread->mg = map_groups__new(machine);
} else {
leader = __machine__findnew_thread(machine, pid, pid);
- if (leader)
+ if (leader) {
thread->mg = map_groups__get(leader->mg);
+ thread__put(leader);
+ }
}
return thread->mg ? 0 : -1;
@@ -53,7 +55,7 @@ struct thread *thread__new(pid_t pid, pid_t tid)
goto err_thread;
list_add(&comm->list, &thread->comm_list);
- atomic_set(&thread->refcnt, 0);
+ atomic_set(&thread->refcnt, 1);
RB_CLEAR_NODE(&thread->rb_node);
}
@@ -95,6 +97,10 @@ struct thread *thread__get(struct thread *thread)
void thread__put(struct thread *thread)
{
if (thread && atomic_dec_and_test(&thread->refcnt)) {
+ /*
+ * Remove it from the dead_threads list, as last reference
+ * is gone.
+ */
list_del_init(&thread->node);
thread__delete(thread);
}
--
2.1.0
prev parent reply other threads:[~2015-12-11 22:26 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-12-09 2:10 [PATCH perf/core 00/22] perf refcnt debugger API and fixes Masami Hiramatsu
2015-12-09 2:10 ` [PATCH perf/core 01/22] [v2] perf refcnt: Introduce generic refcount APIs with debug feature Masami Hiramatsu
2015-12-09 2:10 ` [PATCH perf/core 02/22] perf refcnt: Use a hash for refcnt_root Masami Hiramatsu
2015-12-09 2:10 ` [PATCH perf/core 03/22] perf refcnt: Add refcnt debug filter Masami Hiramatsu
2015-12-09 2:10 ` [PATCH perf/core 04/22] perf refcnt: refcnt shows summary per object Masami Hiramatsu
2015-12-09 2:10 ` [PATCH perf/core 05/22] perf: make map to use refcnt Masami Hiramatsu
2015-12-09 2:11 ` [PATCH perf/core 06/22] perf: Make dso to use refcnt for debug Masami Hiramatsu
2015-12-09 2:11 ` [PATCH perf/core 07/22] perf: Make map_groups to use refcnt Masami Hiramatsu
2015-12-09 2:11 ` [PATCH perf/core 08/22] perf: Make thread uses refcnt for debug Masami Hiramatsu
2015-12-09 2:11 ` [PATCH perf/core 09/22] perf: Make cpu_map to use " Masami Hiramatsu
2015-12-09 2:11 ` [PATCH perf/core 10/22] perf: Make comm_str " Masami Hiramatsu
2015-12-09 2:11 ` [PATCH perf/core 11/22] perf: Make cgroup_sel " Masami Hiramatsu
2015-12-09 2:11 ` [PATCH perf/core 12/22] perf: Make thread_map " Masami Hiramatsu
2015-12-09 2:11 ` [PATCH perf/core 13/22] perf: Make perf_mmap " Masami Hiramatsu
2015-12-09 2:11 ` [PATCH perf/core 14/22] perf: Fix dso__load_sym to put dso Masami Hiramatsu
2015-12-09 14:16 ` Arnaldo Carvalho de Melo
2015-12-10 8:52 ` 平松雅巳 / HIRAMATU,MASAMI
2015-12-10 19:26 ` 'Arnaldo Carvalho de Melo'
2015-12-09 2:11 ` [PATCH perf/core 15/22] perf: Fix map_groups__clone to put cloned map Masami Hiramatsu
2015-12-09 14:17 ` Arnaldo Carvalho de Melo
2015-12-09 2:11 ` [PATCH perf/core 16/22] perf: Fix __cmd_top and perf_session__process_events to put the idle thread Masami Hiramatsu
2015-12-09 14:30 ` Arnaldo Carvalho de Melo
2015-12-10 10:07 ` 平松雅巳 / HIRAMATU,MASAMI
2015-12-09 2:11 ` [PATCH perf/core 17/22] perf: Fix __machine__addnew_vdso to put dso after add to dsos Masami Hiramatsu
2015-12-09 14:38 ` Arnaldo Carvalho de Melo
2015-12-09 2:11 ` [PATCH perf/core 18/22] perf stat: Fix cmd_stat to release cpu_map Masami Hiramatsu
2015-12-09 15:05 ` Arnaldo Carvalho de Melo
2015-12-09 2:11 ` [PATCH perf/core 19/22] perf: fix hists_evsel to release hists Masami Hiramatsu
2015-12-09 15:12 ` Arnaldo Carvalho de Melo
2015-12-09 2:11 ` [PATCH perf/core 20/22] perf: Fix maps__fixup_overlappings to put used maps Masami Hiramatsu
2015-12-09 15:15 ` Arnaldo Carvalho de Melo
2015-12-09 2:11 ` [PATCH perf/core 21/22] perf: Fix machine.vmlinux_maps to make sure to clear the old one Masami Hiramatsu
2015-12-09 15:22 ` Arnaldo Carvalho de Melo
2015-12-10 2:52 ` Wangnan (F)
2015-12-09 2:11 ` [PATCH perf/core 22/22] perf: Fix write_numa_topology to put cpu_map instead of free Masami Hiramatsu
2015-12-09 15:26 ` Arnaldo Carvalho de Melo
2015-12-09 13:41 ` [PATCH perf/core 00/22] perf refcnt debugger API and fixes Arnaldo Carvalho de Melo
2015-12-10 3:31 ` Alexei Starovoitov
2015-12-10 4:49 ` Namhyung Kim
2015-12-10 8:39 ` 平松雅巳 / HIRAMATU,MASAMI
2015-12-10 11:04 ` 平松雅巳 / HIRAMATU,MASAMI
2015-12-10 12:52 ` Wangnan (F)
2015-12-10 15:12 ` 'Arnaldo Carvalho de Melo'
2015-12-11 1:53 ` Wangnan (F)
2015-12-11 2:08 ` 平松雅巳 / HIRAMATU,MASAMI
2015-12-11 2:22 ` Wangnan (F)
2015-12-11 2:15 ` 平松雅巳 / HIRAMATU,MASAMI
2015-12-11 2:42 ` Wangnan (F)
2015-12-11 2:53 ` Wangnan (F)
2015-12-11 3:59 ` 平松雅巳 / HIRAMATU,MASAMI
2015-12-11 22:26 ` Arnaldo Carvalho de Melo [this message]
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=20151211222621.GE6843@kernel.org \
--to=acme@kernel.org \
--cc=a.p.zijlstra@chello.nl \
--cc=adrian.hunter@intel.com \
--cc=ast@plumgrid.com \
--cc=jolsa@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=masami.hiramatsu.pt@hitachi.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=wangnan0@huawei.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).