perf dlfilter: Fix use of addr_location__exit() in dlfilter__object_code()

Message ID 20230928071605.17624-1-adrian.hunter@intel.com
State New
Headers
Series perf dlfilter: Fix use of addr_location__exit() in dlfilter__object_code() |

Commit Message

Adrian Hunter Sept. 28, 2023, 7:16 a.m. UTC
  Stop calling addr_location__exit() when addr_location__init() was not
called.

Fixes: 0dd5041c9a0e ("perf addr_location: Add init/exit/copy functions")
Cc: stable@vger.kernel.org
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
---
 tools/perf/util/dlfilter.c | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
  

Comments

Namhyung Kim Sept. 30, 2023, 6:40 a.m. UTC | #1
Hi Adrian,

On Thu, Sep 28, 2023 at 12:16 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
>
> Stop calling addr_location__exit() when addr_location__init() was not
> called.
>
> Fixes: 0dd5041c9a0e ("perf addr_location: Add init/exit/copy functions")
> Cc: stable@vger.kernel.org
> Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>

Acked-by: Namhyung Kim <namhyung@kernel.org>

Thanks,
Namhyung


> ---
>  tools/perf/util/dlfilter.c | 32 +++++++++++++++-----------------
>  1 file changed, 15 insertions(+), 17 deletions(-)
>
> diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c
> index 1dbf27822ee2..4a1dc21b0450 100644
> --- a/tools/perf/util/dlfilter.c
> +++ b/tools/perf/util/dlfilter.c
> @@ -282,13 +282,21 @@ static struct perf_event_attr *dlfilter__attr(void *ctx)
>         return &d->evsel->core.attr;
>  }
>
> +static __s32 code_read(__u64 ip, struct map *map, struct machine *machine, void *buf, __u32 len)
> +{
> +       u64 offset = map__map_ip(map, ip);
> +
> +       if (ip + len >= map__end(map))
> +               len = map__end(map) - ip;
> +
> +       return dso__data_read_offset(map__dso(map), machine, offset, buf, len);
> +}
> +
>  static __s32 dlfilter__object_code(void *ctx, __u64 ip, void *buf, __u32 len)
>  {
>         struct dlfilter *d = (struct dlfilter *)ctx;
>         struct addr_location *al;
>         struct addr_location a;
> -       struct map *map;
> -       u64 offset;
>         __s32 ret;
>
>         if (!d->ctx_valid)
> @@ -298,27 +306,17 @@ static __s32 dlfilter__object_code(void *ctx, __u64 ip, void *buf, __u32 len)
>         if (!al)
>                 return -1;
>
> -       map = al->map;
> -
> -       if (map && ip >= map__start(map) && ip < map__end(map) &&
> +       if (al->map && ip >= map__start(al->map) && ip < map__end(al->map) &&
>             machine__kernel_ip(d->machine, ip) == machine__kernel_ip(d->machine, d->sample->ip))
> -               goto have_map;
> +               return code_read(ip, al->map, d->machine, buf, len);
>
>         addr_location__init(&a);
> +
>         thread__find_map_fb(al->thread, d->sample->cpumode, ip, &a);
> -       if (!a.map) {
> -               ret = -1;
> -               goto out;
> -       }
> +       ret = a.map ? code_read(ip, a.map, d->machine, buf, len) : -1;
>
> -       map = a.map;
> -have_map:
> -       offset = map__map_ip(map, ip);
> -       if (ip + len >= map__end(map))
> -               len = map__end(map) - ip;
> -       ret = dso__data_read_offset(map__dso(map), d->machine, offset, buf, len);
> -out:
>         addr_location__exit(&a);
> +
>         return ret;
>  }
>
> --
> 2.34.1
>
  
Namhyung Kim Oct. 3, 2023, 4:08 a.m. UTC | #2
On Fri, Sep 29, 2023 at 11:40 PM Namhyung Kim <namhyung@kernel.org> wrote:
>
> Hi Adrian,
>
> On Thu, Sep 28, 2023 at 12:16 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
> >
> > Stop calling addr_location__exit() when addr_location__init() was not
> > called.
> >
> > Fixes: 0dd5041c9a0e ("perf addr_location: Add init/exit/copy functions")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
>
> Acked-by: Namhyung Kim <namhyung@kernel.org>

Applied this and the test code to perf-tools, thanks!
  

Patch

diff --git a/tools/perf/util/dlfilter.c b/tools/perf/util/dlfilter.c
index 1dbf27822ee2..4a1dc21b0450 100644
--- a/tools/perf/util/dlfilter.c
+++ b/tools/perf/util/dlfilter.c
@@ -282,13 +282,21 @@  static struct perf_event_attr *dlfilter__attr(void *ctx)
 	return &d->evsel->core.attr;
 }
 
+static __s32 code_read(__u64 ip, struct map *map, struct machine *machine, void *buf, __u32 len)
+{
+	u64 offset = map__map_ip(map, ip);
+
+	if (ip + len >= map__end(map))
+		len = map__end(map) - ip;
+
+	return dso__data_read_offset(map__dso(map), machine, offset, buf, len);
+}
+
 static __s32 dlfilter__object_code(void *ctx, __u64 ip, void *buf, __u32 len)
 {
 	struct dlfilter *d = (struct dlfilter *)ctx;
 	struct addr_location *al;
 	struct addr_location a;
-	struct map *map;
-	u64 offset;
 	__s32 ret;
 
 	if (!d->ctx_valid)
@@ -298,27 +306,17 @@  static __s32 dlfilter__object_code(void *ctx, __u64 ip, void *buf, __u32 len)
 	if (!al)
 		return -1;
 
-	map = al->map;
-
-	if (map && ip >= map__start(map) && ip < map__end(map) &&
+	if (al->map && ip >= map__start(al->map) && ip < map__end(al->map) &&
 	    machine__kernel_ip(d->machine, ip) == machine__kernel_ip(d->machine, d->sample->ip))
-		goto have_map;
+		return code_read(ip, al->map, d->machine, buf, len);
 
 	addr_location__init(&a);
+
 	thread__find_map_fb(al->thread, d->sample->cpumode, ip, &a);
-	if (!a.map) {
-		ret = -1;
-		goto out;
-	}
+	ret = a.map ? code_read(ip, a.map, d->machine, buf, len) : -1;
 
-	map = a.map;
-have_map:
-	offset = map__map_ip(map, ip);
-	if (ip + len >= map__end(map))
-		len = map__end(map) - ip;
-	ret = dso__data_read_offset(map__dso(map), d->machine, offset, buf, len);
-out:
 	addr_location__exit(&a);
+
 	return ret;
 }