linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] perf tools: Allow build-id with trailing zeros
@ 2021-09-10 16:41 Namhyung Kim
  2021-09-10 19:41 ` Jiri Olsa
  0 siblings, 1 reply; 3+ messages in thread
From: Namhyung Kim @ 2021-09-10 16:41 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, LKML, Andi Kleen, Ian Rogers

Current perf saves a build-id with size but old versions assumes the
size of 20.  In case the build-id is less than 20 (like for MD5), it'd
fill the rest with 0s.

I saw a problem when old version of perf record saved binary in the
build-id cache and new version of perf reads the data.  The symbols
should be read from the build-id cache (as the path no longer has the
same binary) but it failed due to mismatch in the build-id.

  symsrc__init: build id mismatch for /home/namhyung/.debug/.build-id/53/e4c2f42a4c61a2d632d92a72afa08f00000000/elf.

The build-id event in the data has 20 byte build-ids, but it saw a
different size (16) when it reads the build-id of the elf file in the
build-id cache.

  $ readelf -n ~/.debug/.build-id/53/e4c2f42a4c61a2d632d92a72afa08f00000000/elf

  Displaying notes found in: .note.gnu.build-id
    Owner                Data size 	Description
    GNU                  0x00000010	NT_GNU_BUILD_ID (unique build ID bitstring)
      Build ID: 53e4c2f42a4c61a2d632d92a72afa08f

Let's fix this by allowing trailing zeros if the size is different.

Fixes: 39be8d0115b3 ("perf tools: Pass build_id object to dso__build_id_equal()")

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/dso.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index ee15db2be2f4..0c0dd877d4e9 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -1349,6 +1349,18 @@ void dso__set_build_id(struct dso *dso, struct build_id *bid)
 
 bool dso__build_id_equal(const struct dso *dso, struct build_id *bid)
 {
+	static const char zeros[BUILD_ID_SIZE];
+
+	if (dso->bid.size > bid->size && dso->bid.size == BUILD_ID_SIZE) {
+		/*
+		 * For the backward compatibility, it allows a build-id has
+		 * trailing zeros.
+		 */
+		return !memcmp(dso->bid.data, bid->data, bid->size) &&
+			!memcmp(&dso->bid.data[bid->size], zeros,
+				dso->bid.size - bid->size);
+	}
+
 	return dso->bid.size == bid->size &&
 	       memcmp(dso->bid.data, bid->data, dso->bid.size) == 0;
 }
-- 
2.33.0.309.g3052b89438-goog


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] perf tools: Allow build-id with trailing zeros
  2021-09-10 16:41 [PATCH] perf tools: Allow build-id with trailing zeros Namhyung Kim
@ 2021-09-10 19:41 ` Jiri Olsa
  2021-09-10 22:38   ` Namhyung Kim
  0 siblings, 1 reply; 3+ messages in thread
From: Jiri Olsa @ 2021-09-10 19:41 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra, LKML,
	Andi Kleen, Ian Rogers

On Fri, Sep 10, 2021 at 09:41:50AM -0700, Namhyung Kim wrote:
> Current perf saves a build-id with size but old versions assumes the
> size of 20.  In case the build-id is less than 20 (like for MD5), it'd
> fill the rest with 0s.
> 
> I saw a problem when old version of perf record saved binary in the
> build-id cache and new version of perf reads the data.  The symbols
> should be read from the build-id cache (as the path no longer has the
> same binary) but it failed due to mismatch in the build-id.
> 
>   symsrc__init: build id mismatch for /home/namhyung/.debug/.build-id/53/e4c2f42a4c61a2d632d92a72afa08f00000000/elf.
> 
> The build-id event in the data has 20 byte build-ids, but it saw a
> different size (16) when it reads the build-id of the elf file in the
> build-id cache.
> 
>   $ readelf -n ~/.debug/.build-id/53/e4c2f42a4c61a2d632d92a72afa08f00000000/elf
> 
>   Displaying notes found in: .note.gnu.build-id
>     Owner                Data size 	Description
>     GNU                  0x00000010	NT_GNU_BUILD_ID (unique build ID bitstring)
>       Build ID: 53e4c2f42a4c61a2d632d92a72afa08f
> 
> Let's fix this by allowing trailing zeros if the size is different.
> 
> Fixes: 39be8d0115b3 ("perf tools: Pass build_id object to dso__build_id_equal()")
> 
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> ---
>  tools/perf/util/dso.c | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
> 
> diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
> index ee15db2be2f4..0c0dd877d4e9 100644
> --- a/tools/perf/util/dso.c
> +++ b/tools/perf/util/dso.c
> @@ -1349,6 +1349,18 @@ void dso__set_build_id(struct dso *dso, struct build_id *bid)
>  
>  bool dso__build_id_equal(const struct dso *dso, struct build_id *bid)
>  {
> +	static const char zeros[BUILD_ID_SIZE];
> +
> +	if (dso->bid.size > bid->size && dso->bid.size == BUILD_ID_SIZE) {
> +		/*
> +		 * For the backward compatibility, it allows a build-id has
> +		 * trailing zeros.
> +		 */
> +		return !memcmp(dso->bid.data, bid->data, bid->size) &&
> +			!memcmp(&dso->bid.data[bid->size], zeros,
> +				dso->bid.size - bid->size);

we now have memchr_inv in tools, so you could use:
  memchr_inv(&dso->bid.data[bid->size], 0, dso->bid.size - bid->size);

and save 20 bytes in bss ;-) other than that, nice catch

Acked-by: Jiri Olsa <jolsa@redhat.com>

thanks,
jirka

> +	}
> +
>  	return dso->bid.size == bid->size &&
>  	       memcmp(dso->bid.data, bid->data, dso->bid.size) == 0;
>  }
> -- 
> 2.33.0.309.g3052b89438-goog
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] perf tools: Allow build-id with trailing zeros
  2021-09-10 19:41 ` Jiri Olsa
@ 2021-09-10 22:38   ` Namhyung Kim
  0 siblings, 0 replies; 3+ messages in thread
From: Namhyung Kim @ 2021-09-10 22:38 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra, LKML,
	Andi Kleen, Ian Rogers

Hi Jiri,

On Fri, Sep 10, 2021 at 12:41 PM Jiri Olsa <jolsa@redhat.com> wrote:
>
> On Fri, Sep 10, 2021 at 09:41:50AM -0700, Namhyung Kim wrote:
> > Current perf saves a build-id with size but old versions assumes the
> > size of 20.  In case the build-id is less than 20 (like for MD5), it'd
> > fill the rest with 0s.
> >
> > I saw a problem when old version of perf record saved binary in the
> > build-id cache and new version of perf reads the data.  The symbols
> > should be read from the build-id cache (as the path no longer has the
> > same binary) but it failed due to mismatch in the build-id.
> >
> >   symsrc__init: build id mismatch for /home/namhyung/.debug/.build-id/53/e4c2f42a4c61a2d632d92a72afa08f00000000/elf.
> >
> > The build-id event in the data has 20 byte build-ids, but it saw a
> > different size (16) when it reads the build-id of the elf file in the
> > build-id cache.
> >
> >   $ readelf -n ~/.debug/.build-id/53/e4c2f42a4c61a2d632d92a72afa08f00000000/elf
> >
> >   Displaying notes found in: .note.gnu.build-id
> >     Owner                Data size    Description
> >     GNU                  0x00000010   NT_GNU_BUILD_ID (unique build ID bitstring)
> >       Build ID: 53e4c2f42a4c61a2d632d92a72afa08f
> >
> > Let's fix this by allowing trailing zeros if the size is different.
> >
> > Fixes: 39be8d0115b3 ("perf tools: Pass build_id object to dso__build_id_equal()")
> >
> > Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> > ---
> >  tools/perf/util/dso.c | 12 ++++++++++++
> >  1 file changed, 12 insertions(+)
> >
> > diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
> > index ee15db2be2f4..0c0dd877d4e9 100644
> > --- a/tools/perf/util/dso.c
> > +++ b/tools/perf/util/dso.c
> > @@ -1349,6 +1349,18 @@ void dso__set_build_id(struct dso *dso, struct build_id *bid)
> >
> >  bool dso__build_id_equal(const struct dso *dso, struct build_id *bid)
> >  {
> > +     static const char zeros[BUILD_ID_SIZE];
> > +
> > +     if (dso->bid.size > bid->size && dso->bid.size == BUILD_ID_SIZE) {
> > +             /*
> > +              * For the backward compatibility, it allows a build-id has
> > +              * trailing zeros.
> > +              */
> > +             return !memcmp(dso->bid.data, bid->data, bid->size) &&
> > +                     !memcmp(&dso->bid.data[bid->size], zeros,
> > +                             dso->bid.size - bid->size);
>
> we now have memchr_inv in tools, so you could use:
>   memchr_inv(&dso->bid.data[bid->size], 0, dso->bid.size - bid->size);
>
> and save 20 bytes in bss ;-) other than that, nice catch
>
> Acked-by: Jiri Olsa <jolsa@redhat.com>

right, will update!

Thanks,
Namhyung

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-09-10 22:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-10 16:41 [PATCH] perf tools: Allow build-id with trailing zeros Namhyung Kim
2021-09-10 19:41 ` Jiri Olsa
2021-09-10 22:38   ` Namhyung Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).