All of lore.kernel.org
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ian Rogers <irogers@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
	Nathan Chancellor <nathan@kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Tom Rix <trix@redhat.com>, Andrii Nakryiko <andrii@kernel.org>,
	Christy Lee <christylee@fb.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Miaoqian Lin <linmq006@gmail.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	bpf@vger.kernel.org, llvm@lists.linux.dev,
	Stephane Eranian <eranian@google.com>
Subject: Re: [PATCH] perf bpf: Remove undefined behavior from bpf_perf_object__next
Date: Wed, 27 Jul 2022 11:16:53 -0300	[thread overview]
Message-ID: <YuFI1Thhls+dYE2I@kernel.org> (raw)
In-Reply-To: <20220726220921.2567761-1-irogers@google.com>

Em Tue, Jul 26, 2022 at 03:09:21PM -0700, Ian Rogers escreveu:
> 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.
> in 
> Remove the undefined behav(ior 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.

Nicely spotted!

In some places people solve this with list_first_entry_or_null(), like
in cs_etm__queue_aux_records().

Applied.

- Arnado
 
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/perf/util/bpf-loader.c | 18 +++++++-----------
>  1 file changed, 7 insertions(+), 11 deletions(-)
> 
> diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
> indelx f8ad581ea247..cdd6463a5b68 100644
> --- a/tools/perf/util/bpf-loader.c
> +++ b/tools/perf/util/bpf-loader.c
> @@ -63,20 +63,16 @@ static struct hashmap *bpf_map_hash;
>  static struct bpf_perf_object *
>  bpf_perf_object__next(struct bpf_perf_object *prev)
>  {
> -	struct bpf_perf_object *next;
> -
> -	if (!prev)
> -		next = list_first_entry(&bpf_objects_list,
> -					struct bpf_perf_object,
> -					list);
> -	else
> -		next = list_next_entry(prev, list);
> +	if (!prev) {
> +		if (list_empty(&bpf_objects_list))
> +			return NULL;
>  
> -	/* Empty list is noticed here so don't need checking on entry. */
> -	if (&next->list == &bpf_objects_list)
> +		return list_first_entry(&bpf_objects_list, struct bpf_perf_object, list);
> +	}
> +	if (list_is_last(&prev->list, &bpf_objects_list))
>  		return NULL;
>  
> -	return next;
> +	return list_next_entry(prev, list);
>  }
>  
>  #define bpf_perf_object__for_each(perf_obj, tmp)	\
> -- 
> 2.37.1.359.gd136c6c3e2-goog

-- 

- Arnaldo

      reply	other threads:[~2022-07-27 14:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-26 22:09 [PATCH] perf bpf: Remove undefined behavior from bpf_perf_object__next Ian Rogers
2022-07-27 14:16 ` 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=YuFI1Thhls+dYE2I@kernel.org \
    --to=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=christylee@fb.com \
    --cc=eranian@google.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=linmq006@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=peterz@infradead.org \
    --cc=trix@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.