Commit ec417ad4 authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo
Browse files

perf map: Changes to reference counting



When a pointer to a map exists do a get, when that pointer is
overwritten or freed, put the map. This avoids issues with gets and
puts being inconsistently used causing, use after puts, etc. For
example, the map in struct addr_location is changed to hold a
reference count. Reference count checking and address sanitizer were
used to identify issues.

Signed-off-by: default avatarIan Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexey Bayduraev <alexey.v.bayduraev@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Darren Hart <dvhart@infradead.org>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Dmitriy Vyukov <dvyukov@google.com>
Cc: Eric Dumazet <edumazet@google.com>
Cc: German Gomez <german.gomez@arm.com>
Cc: Hao Luo <haoluo@google.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Miaoqian Lin <linmq006@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Riccardo Mancini <rickyman7@gmail.com>
Cc: Shunsuke Nakamura <nakamura.shun@fujitsu.com>
Cc: Song Liu <song@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Stephen Brennan <stephen.s.brennan@oracle.com>
Cc: Steven Rostedt (VMware) <rostedt@goodmis.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Yury Norov <yury.norov@gmail.com>
Link: https://lore.kernel.org/r/20230404205954.2245628-2-irogers@google.com


Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 392cf49e
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -366,6 +366,7 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,
	}
	pr_debug("Bytes read match those read by objdump\n");
out:
	map__put(al.map);
	return err;
}

+10 −0
Original line number Diff line number Diff line
@@ -112,6 +112,7 @@ static int add_hist_entries(struct hists *hists, struct machine *machine)
		}

		fake_samples[i].thread = al.thread;
		map__put(fake_samples[i].map);
		fake_samples[i].map = al.map;
		fake_samples[i].sym = al.sym;
	}
@@ -147,6 +148,14 @@ static void del_hist_entries(struct hists *hists)
	}
}

static void put_fake_samples(void)
{
	size_t i;

	for (i = 0; i < ARRAY_SIZE(fake_samples); i++)
		map__put(fake_samples[i].map);
}

typedef int (*test_fn_t)(struct evsel *, struct machine *);

#define COMM(he)  (thread__comm_str(he->thread))
@@ -733,6 +742,7 @@ static int test__hists_cumulate(struct test_suite *test __maybe_unused, int subt
	/* tear down everything */
	evlist__delete(evlist);
	machines__exit(&machines);
	put_fake_samples();

	return err;
}
+10 −0
Original line number Diff line number Diff line
@@ -89,6 +89,7 @@ static int add_hist_entries(struct evlist *evlist,
			}

			fake_samples[i].thread = al.thread;
			map__put(fake_samples[i].map);
			fake_samples[i].map = al.map;
			fake_samples[i].sym = al.sym;
		}
@@ -101,6 +102,14 @@ static int add_hist_entries(struct evlist *evlist,
	return TEST_FAIL;
}

static void put_fake_samples(void)
{
	size_t i;

	for (i = 0; i < ARRAY_SIZE(fake_samples); i++)
		map__put(fake_samples[i].map);
}

static int test__hists_filter(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
{
	int err = TEST_FAIL;
@@ -322,6 +331,7 @@ static int test__hists_filter(struct test_suite *test __maybe_unused, int subtes
	evlist__delete(evlist);
	reset_output_field();
	machines__exit(&machines);
	put_fake_samples();

	return err;
}
+17 −1
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
#include "evsel.h"
#include "evlist.h"
#include "machine.h"
#include "map.h"
#include "parse-events.h"
#include "hists_common.h"
#include "util/mmap.h"
@@ -94,6 +95,7 @@ static int add_hist_entries(struct evlist *evlist, struct machine *machine)
			}

			fake_common_samples[k].thread = al.thread;
			map__put(fake_common_samples[k].map);
			fake_common_samples[k].map = al.map;
			fake_common_samples[k].sym = al.sym;
		}
@@ -126,11 +128,24 @@ static int add_hist_entries(struct evlist *evlist, struct machine *machine)
	return -1;
}

static void put_fake_samples(void)
{
	size_t i, j;

	for (i = 0; i < ARRAY_SIZE(fake_common_samples); i++)
		map__put(fake_common_samples[i].map);
	for (i = 0; i < ARRAY_SIZE(fake_samples); i++) {
		for (j = 0; j < ARRAY_SIZE(fake_samples[0]); j++)
			map__put(fake_samples[i][j].map);
	}
}

static int find_sample(struct sample *samples, size_t nr_samples,
		       struct thread *t, struct map *m, struct symbol *s)
{
	while (nr_samples--) {
		if (samples->thread == t && samples->map == m &&
		if (samples->thread == t &&
		    samples->map == m &&
		    samples->sym == s)
			return 1;
		samples++;
@@ -336,6 +351,7 @@ static int test__hists_link(struct test_suite *test __maybe_unused, int subtest
	evlist__delete(evlist);
	reset_output_field();
	machines__exit(&machines);
	put_fake_samples();

	return err;
}
+10 −0
Original line number Diff line number Diff line
@@ -78,6 +78,7 @@ static int add_hist_entries(struct hists *hists, struct machine *machine)
		}

		fake_samples[i].thread = al.thread;
		map__put(fake_samples[i].map);
		fake_samples[i].map = al.map;
		fake_samples[i].sym = al.sym;
	}
@@ -113,6 +114,14 @@ static void del_hist_entries(struct hists *hists)
	}
}

static void put_fake_samples(void)
{
	size_t i;

	for (i = 0; i < ARRAY_SIZE(fake_samples); i++)
		map__put(fake_samples[i].map);
}

typedef int (*test_fn_t)(struct evsel *, struct machine *);

#define COMM(he)  (thread__comm_str(he->thread))
@@ -620,6 +629,7 @@ static int test__hists_output(struct test_suite *test __maybe_unused, int subtes
	/* tear down everything */
	evlist__delete(evlist);
	machines__exit(&machines);
	put_fake_samples();

	return err;
}
Loading