Skip to content

Commit 78c32f4

Browse files
captain5050namhyung
authored andcommitted
libperf rc_check: Add RC_CHK_EQUAL
Comparing pointers with reference count checking is tricky to avoid a SEGV. Add a convenience macro to simplify and use. Signed-off-by: Ian Rogers <irogers@google.com> Cc: K Prateek Nayak <kprateek.nayak@amd.com> Cc: Ravi Bangoria <ravi.bangoria@amd.com> Cc: Sandipan Das <sandipan.das@amd.com> Cc: Anshuman Khandual <anshuman.khandual@arm.com> Cc: German Gomez <german.gomez@arm.com> Cc: James Clark <james.clark@arm.com> Cc: Nick Terrell <terrelln@fb.com> Cc: Sean Christopherson <seanjc@google.com> Cc: Changbin Du <changbin.du@huawei.com> Cc: liuwenyu <liuwenyu7@huawei.com> Cc: Yang Jihong <yangjihong1@huawei.com> Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Miguel Ojeda <ojeda@kernel.org> Cc: Song Liu <song@kernel.org> Cc: Leo Yan <leo.yan@linaro.org> Cc: Kajol Jain <kjain@linux.ibm.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com> Cc: Yanteng Si <siyanteng@loongson.cn> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Paolo Bonzini <pbonzini@redhat.com> Link: https://lore.kernel.org/r/20231024222353.3024098-5-irogers@google.com Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent 7526532 commit 78c32f4

9 files changed

Lines changed: 21 additions & 15 deletions

File tree

tools/lib/perf/include/internal/rc_check.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
/* A put operation removing the indirection layer. */
5555
#define RC_CHK_PUT(object) {}
5656

57+
/* Pointer equality when the indirection may or may not be there. */
58+
#define RC_CHK_EQUAL(object1, object2) (object1 == object2)
59+
5760
#else
5861

5962
/* Replaces "struct foo" so that the pointer may be interposed. */
@@ -101,6 +104,10 @@
101104
} \
102105
} while(0)
103106

107+
/* Pointer equality when the indirection may or may not be there. */
108+
#define RC_CHK_EQUAL(object1, object2) (object1 == object2 || \
109+
(object1 && object2 && object1->orig == object2->orig))
110+
104111
#endif
105112

106113
#endif /* __LIBPERF_INTERNAL_RC_CHECK_H */

tools/perf/builtin-sched.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,7 @@ static int pid_cmp(struct work_atoms *l, struct work_atoms *r)
13851385
{
13861386
pid_t l_tid, r_tid;
13871387

1388-
if (RC_CHK_ACCESS(l->thread) == RC_CHK_ACCESS(r->thread))
1388+
if (RC_CHK_EQUAL(l->thread, r->thread))
13891389
return 0;
13901390
l_tid = thread__tid(l->thread);
13911391
r_tid = thread__tid(r->thread);

tools/perf/tests/hists_link.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ static int find_sample(struct sample *samples, size_t nr_samples,
148148
struct thread *t, struct map *m, struct symbol *s)
149149
{
150150
while (nr_samples--) {
151-
if (RC_CHK_ACCESS(samples->thread) == RC_CHK_ACCESS(t) &&
152-
RC_CHK_ACCESS(samples->map) == RC_CHK_ACCESS(m) &&
151+
if (RC_CHK_EQUAL(samples->thread, t) &&
152+
RC_CHK_EQUAL(samples->map, m) &&
153153
samples->sym == s)
154154
return 1;
155155
samples++;

tools/perf/tests/thread-maps-share.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ static int test__thread_maps_share(struct test_suite *test __maybe_unused, int s
4646
TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(maps__refcnt(maps)), 4);
4747

4848
/* test the maps pointer is shared */
49-
TEST_ASSERT_VAL("maps don't match", RC_CHK_ACCESS(maps) == RC_CHK_ACCESS(thread__maps(t1)));
50-
TEST_ASSERT_VAL("maps don't match", RC_CHK_ACCESS(maps) == RC_CHK_ACCESS(thread__maps(t2)));
51-
TEST_ASSERT_VAL("maps don't match", RC_CHK_ACCESS(maps) == RC_CHK_ACCESS(thread__maps(t3)));
49+
TEST_ASSERT_VAL("maps don't match", RC_CHK_EQUAL(maps, thread__maps(t1)));
50+
TEST_ASSERT_VAL("maps don't match", RC_CHK_EQUAL(maps, thread__maps(t2)));
51+
TEST_ASSERT_VAL("maps don't match", RC_CHK_EQUAL(maps, thread__maps(t3)));
5252

5353
/*
5454
* Verify the other leader was created by previous call.
@@ -73,8 +73,7 @@ static int test__thread_maps_share(struct test_suite *test __maybe_unused, int s
7373
other_maps = thread__maps(other);
7474
TEST_ASSERT_EQUAL("wrong refcnt", refcount_read(maps__refcnt(other_maps)), 2);
7575

76-
TEST_ASSERT_VAL("maps don't match", RC_CHK_ACCESS(other_maps) ==
77-
RC_CHK_ACCESS(thread__maps(other_leader)));
76+
TEST_ASSERT_VAL("maps don't match", RC_CHK_EQUAL(other_maps, thread__maps(other_leader)));
7877

7978
/* release thread group */
8079
thread__put(t3);

tools/perf/util/callchain.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *
11421142
if (al->map == NULL)
11431143
goto out;
11441144
}
1145-
if (RC_CHK_ACCESS(al->maps) == RC_CHK_ACCESS(machine__kernel_maps(machine))) {
1145+
if (RC_CHK_EQUAL(al->maps, machine__kernel_maps(machine))) {
11461146
if (machine__is_host(machine)) {
11471147
al->cpumode = PERF_RECORD_MISC_KERNEL;
11481148
al->level = 'k';

tools/perf/util/hist.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2142,7 +2142,7 @@ static bool hists__filter_entry_by_thread(struct hists *hists,
21422142
struct hist_entry *he)
21432143
{
21442144
if (hists->thread_filter != NULL &&
2145-
RC_CHK_ACCESS(he->thread) != RC_CHK_ACCESS(hists->thread_filter)) {
2145+
!RC_CHK_EQUAL(he->thread, hists->thread_filter)) {
21462146
he->filtered |= (1 << HIST_FILTER__THREAD);
21472147
return true;
21482148
}

tools/perf/util/machine.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ static int machine__process_ksymbol_unregister(struct machine *machine,
969969
if (!map)
970970
return 0;
971971

972-
if (RC_CHK_ACCESS(map) != RC_CHK_ACCESS(machine->vmlinux_map))
972+
if (!RC_CHK_EQUAL(map, machine->vmlinux_map))
973973
maps__remove(machine__kernel_maps(machine), map);
974974
else {
975975
struct dso *dso = map__dso(map);
@@ -2058,7 +2058,7 @@ static void __machine__remove_thread(struct machine *machine, struct thread_rb_n
20582058
if (!nd)
20592059
nd = thread_rb_node__find(th, &threads->entries.rb_root);
20602060

2061-
if (threads->last_match && RC_CHK_ACCESS(threads->last_match) == RC_CHK_ACCESS(th))
2061+
if (threads->last_match && RC_CHK_EQUAL(threads->last_match, th))
20622062
threads__set_last_match(threads, NULL);
20632063

20642064
if (lock)

tools/perf/util/sort.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static int hist_entry__thread_filter(struct hist_entry *he, int type, const void
128128
if (type != HIST_FILTER__THREAD)
129129
return -1;
130130

131-
return th && RC_CHK_ACCESS(he->thread) != RC_CHK_ACCESS(th);
131+
return th && !RC_CHK_EQUAL(he->thread, th);
132132
}
133133

134134
struct sort_entry sort_thread = {

tools/perf/util/symbol.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
877877
*module++ = '\0';
878878
curr_map_dso = map__dso(curr_map);
879879
if (strcmp(curr_map_dso->short_name, module)) {
880-
if (RC_CHK_ACCESS(curr_map) != RC_CHK_ACCESS(initial_map) &&
880+
if (!RC_CHK_EQUAL(curr_map, initial_map) &&
881881
dso->kernel == DSO_SPACE__KERNEL_GUEST &&
882882
machine__is_default_guest(machine)) {
883883
/*
@@ -1469,7 +1469,7 @@ static int dso__load_kcore(struct dso *dso, struct map *map,
14691469

14701470
list_del_init(&new_node->node);
14711471

1472-
if (RC_CHK_ACCESS(new_map) == RC_CHK_ACCESS(replacement_map)) {
1472+
if (RC_CHK_EQUAL(new_map, replacement_map)) {
14731473
struct map *map_ref;
14741474

14751475
map__set_start(map, map__start(new_map));

0 commit comments

Comments
 (0)