Skip to content

Commit 9a24180

Browse files
captain5050acmel
authored andcommitted
perf bpf: Remove undefined behavior from bpf_perf_object__next()
bpf_perf_object__next() folded the last element in the list test with the empty list test. However, this meant that offsets were computed against null and that a struct list_head was compared against a 'struct bpf_perf_object'. Working around this with clang's undefined behavior sanitizer required -fno-sanitize=null and -fno-sanitize=object-size. Remove the undefined behavior by using the regular Linux list APIs and handling the starting case separately from the end testing case. Looking at uses like bpf_perf_object__for_each(), as the constant NULL or non-NULL argument can be constant propagated, the code is no less efficient. Signed-off-by: Ian Rogers <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Andrii Nakryiko <[email protected]> Cc: Christy Lee <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Miaoqian Lin <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Nathan Chancellor <[email protected]> Cc: Nick Desaulniers <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Tom Rix <[email protected]> Cc: [email protected] Cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 882528d commit 9a24180

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

tools/perf/util/bpf-loader.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,16 @@ static struct hashmap *bpf_map_hash;
6363
static struct bpf_perf_object *
6464
bpf_perf_object__next(struct bpf_perf_object *prev)
6565
{
66-
struct bpf_perf_object *next;
67-
68-
if (!prev)
69-
next = list_first_entry(&bpf_objects_list,
70-
struct bpf_perf_object,
71-
list);
72-
else
73-
next = list_next_entry(prev, list);
66+
if (!prev) {
67+
if (list_empty(&bpf_objects_list))
68+
return NULL;
7469

75-
/* Empty list is noticed here so don't need checking on entry. */
76-
if (&next->list == &bpf_objects_list)
70+
return list_first_entry(&bpf_objects_list, struct bpf_perf_object, list);
71+
}
72+
if (list_is_last(&prev->list, &bpf_objects_list))
7773
return NULL;
7874

79-
return next;
75+
return list_next_entry(prev, list);
8076
}
8177

8278
#define bpf_perf_object__for_each(perf_obj, tmp) \

0 commit comments

Comments
 (0)