Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b91fc39f authored by Arnaldo Carvalho de Melo's avatar Arnaldo Carvalho de Melo
Browse files

perf machine: Protect the machine->threads with a rwlock



In addition to using refcounts for the struct thread lifetime
management, we need to protect access to machine->threads from
concurrent access.

That happens in 'perf top', where a thread processes events, inserting
and deleting entries from that rb_tree while another thread decays
hist_entries, that end up dropping references and ultimately deleting
threads from the rb_tree and releasing its resources when no further
hist_entry (or other data structures, like in 'perf sched') references
it.

So the rule is the same for refcounts + protected trees in the kernel,
get the tree lock, find object, bump the refcount, drop the tree lock,
return, use object, drop the refcount if no more use of it is needed,
keep it if storing it in some other data structure, drop when releasing
that data structure.

I.e. pair "t = machine__find(new)_thread()" with a "thread__put(t)", and
"perf_event__preprocess_sample(&al)" with "addr_location__put(&al)".

The addr_location__put() one is because as we return references to
several data structures, we may end up adding more reference counting
for the other data structures and then we'll drop it at
addr_location__put() time.

Acked-by: default avatarDavid Ahern <dsahern@gmail.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-bs9rt4n0jw3hi9f3zxyy3xln@git.kernel.org


Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent e1ed3a5b
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -84,6 +84,7 @@ static int process_sample_event(struct perf_tool *tool,
{
	struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool);
	struct addr_location al;
	int ret = 0;

	if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
		pr_warning("problem processing %d event, skipping it.\n",
@@ -92,15 +93,16 @@ static int process_sample_event(struct perf_tool *tool,
	}

	if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
		return 0;
		goto out_put;

	if (!al.filtered && perf_evsel__add_sample(evsel, sample, &al, ann)) {
		pr_warning("problem incrementing symbol count, "
			   "skipping event\n");
		return -1;
		ret = -1;
	}

	return 0;
out_put:
	addr_location__put(&al);
	return ret;
}

static int hist_entry__tty_annotate(struct hist_entry *he,
+6 −3
Original line number Diff line number Diff line
@@ -328,6 +328,7 @@ static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
{
	struct addr_location al;
	struct hists *hists = evsel__hists(evsel);
	int ret = -1;

	if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
		pr_warning("problem processing %d event, skipping it.\n",
@@ -338,7 +339,7 @@ static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
	if (hists__add_entry(hists, &al, sample->period,
			     sample->weight, sample->transaction)) {
		pr_warning("problem incrementing symbol period, skipping event\n");
		return -1;
		goto out_put;
	}

	/*
@@ -350,8 +351,10 @@ static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
	hists->stats.total_period += sample->period;
	if (!al.filtered)
		hists->stats.total_non_filtered_period += sample->period;

	return 0;
	ret = 0;
out_put:
	addr_location__put(&al);
	return ret;
}

static struct perf_tool tool = {
+1 −0
Original line number Diff line number Diff line
@@ -365,6 +365,7 @@ static int perf_event__inject_buildid(struct perf_tool *tool,
		}
	}

	thread__put(thread);
repipe:
	perf_event__repipe(tool, event, sample, machine);
	return 0;
+5 −2
Original line number Diff line number Diff line
@@ -906,6 +906,7 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused,
				struct perf_evsel *evsel,
				struct machine *machine)
{
	int err = 0;
	struct thread *thread = machine__findnew_thread(machine, sample->pid,
							sample->tid);

@@ -919,10 +920,12 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused,

	if (evsel->handler != NULL) {
		tracepoint_handler f = evsel->handler;
		return f(evsel, sample);
		err = f(evsel, sample);
	}

	return 0;
	thread__put(thread);

	return err;
}

static struct perf_tool perf_kmem = {
+4 −2
Original line number Diff line number Diff line
@@ -651,6 +651,7 @@ static int process_sample_event(struct perf_tool *tool,
				struct perf_evsel *evsel,
				struct machine *machine)
{
	int err = 0;
	struct thread *thread;
	struct perf_kvm_stat *kvm = container_of(tool, struct perf_kvm_stat,
						 tool);
@@ -666,9 +667,10 @@ static int process_sample_event(struct perf_tool *tool,
	}

	if (!handle_kvm_event(kvm, thread, evsel, sample))
		return -1;
		err = -1;

	return 0;
	thread__put(thread);
	return err;
}

static int cpu_isa_config(struct perf_kvm_stat *kvm)
Loading