Commit 82b0a103 authored by Adrian Hunter's avatar Adrian Hunter Committed by Arnaldo Carvalho de Melo

perf dlfilter: Add al_cleanup()

Add perf_dlfilter_fns.al_cleanup() to do addr_location__exit() on data
passed via perf_dlfilter_fns.resolve_address().

Add dlfilter-test-api-v2 to the "dlfilter C API" test to test it.

Update documentation, clarifying that data returned by APIs should not
be dereferenced after filter_event() and filter_event_early() return.

Fixes: 0dd5041c ("perf addr_location: Add init/exit/copy functions")
Reviewed-by: default avatarIan Rogers <irogers@google.com>
Signed-off-by: default avatarAdrian Hunter <adrian.hunter@intel.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20230731091857.10681-3-adrian.hunter@intel.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 42c6dd9d
...@@ -64,6 +64,12 @@ internal filtering. ...@@ -64,6 +64,12 @@ internal filtering.
If implemented, 'filter_description' should return a one-line description If implemented, 'filter_description' should return a one-line description
of the filter, and optionally a longer description. of the filter, and optionally a longer description.
Do not assume the 'sample' argument is valid (dereferenceable)
after 'filter_event' and 'filter_event_early' return.
Do not assume data referenced by pointers in struct perf_dlfilter_sample
is valid (dereferenceable) after 'filter_event' and 'filter_event_early' return.
The perf_dlfilter_sample structure The perf_dlfilter_sample structure
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...@@ -150,7 +156,8 @@ struct perf_dlfilter_fns { ...@@ -150,7 +156,8 @@ struct perf_dlfilter_fns {
const char *(*srcline)(void *ctx, __u32 *line_number); const char *(*srcline)(void *ctx, __u32 *line_number);
struct perf_event_attr *(*attr)(void *ctx); struct perf_event_attr *(*attr)(void *ctx);
__s32 (*object_code)(void *ctx, __u64 ip, void *buf, __u32 len); __s32 (*object_code)(void *ctx, __u64 ip, void *buf, __u32 len);
void *(*reserved[120])(void *); void (*al_cleanup)(void *ctx, struct perf_dlfilter_al *al);
void *(*reserved[119])(void *);
}; };
---- ----
...@@ -161,7 +168,8 @@ struct perf_dlfilter_fns { ...@@ -161,7 +168,8 @@ struct perf_dlfilter_fns {
'args' returns arguments from --dlarg options. 'args' returns arguments from --dlarg options.
'resolve_address' provides information about 'address'. al->size must be set 'resolve_address' provides information about 'address'. al->size must be set
before calling. Returns 0 on success, -1 otherwise. before calling. Returns 0 on success, -1 otherwise. Call al_cleanup() (if present,
see below) when 'al' data is no longer needed.
'insn' returns instruction bytes and length. 'insn' returns instruction bytes and length.
...@@ -171,6 +179,12 @@ before calling. Returns 0 on success, -1 otherwise. ...@@ -171,6 +179,12 @@ before calling. Returns 0 on success, -1 otherwise.
'object_code' reads object code and returns the number of bytes read. 'object_code' reads object code and returns the number of bytes read.
'al_cleanup' must be called (if present, so check perf_dlfilter_fns.al_cleanup != NULL)
after resolve_address() to free any associated resources.
Do not assume pointers obtained via perf_dlfilter_fns are valid (dereferenceable)
after 'filter_event' and 'filter_event_early' return.
The perf_dlfilter_al structure The perf_dlfilter_al structure
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...@@ -197,9 +211,13 @@ struct perf_dlfilter_al { ...@@ -197,9 +211,13 @@ struct perf_dlfilter_al {
/* Below members are only populated by resolve_ip() */ /* Below members are only populated by resolve_ip() */
__u8 filtered; /* true if this sample event will be filtered out */ __u8 filtered; /* true if this sample event will be filtered out */
const char *comm; const char *comm;
void *priv; /* Private data. Do not change */
}; };
---- ----
Do not assume data referenced by pointers in struct perf_dlfilter_al
is valid (dereferenceable) after 'filter_event' and 'filter_event_early' return.
perf_dlfilter_sample flags perf_dlfilter_sample flags
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~
......
...@@ -377,7 +377,7 @@ ifndef NO_JVMTI ...@@ -377,7 +377,7 @@ ifndef NO_JVMTI
PROGRAMS += $(OUTPUT)$(LIBJVMTI) PROGRAMS += $(OUTPUT)$(LIBJVMTI)
endif endif
DLFILTERS := dlfilter-test-api-v0.so dlfilter-show-cycles.so DLFILTERS := dlfilter-test-api-v0.so dlfilter-test-api-v2.so dlfilter-show-cycles.so
DLFILTERS := $(patsubst %,$(OUTPUT)dlfilters/%,$(DLFILTERS)) DLFILTERS := $(patsubst %,$(OUTPUT)dlfilters/%,$(DLFILTERS))
# what 'all' will build and 'install' will install, in perfexecdir # what 'all' will build and 'install' will install, in perfexecdir
......
This diff is collapsed.
...@@ -91,6 +91,7 @@ struct perf_dlfilter_al { ...@@ -91,6 +91,7 @@ struct perf_dlfilter_al {
/* Below members are only populated by resolve_ip() */ /* Below members are only populated by resolve_ip() */
__u8 filtered; /* True if this sample event will be filtered out */ __u8 filtered; /* True if this sample event will be filtered out */
const char *comm; const char *comm;
void *priv; /* Private data. Do not change */
}; };
struct perf_dlfilter_fns { struct perf_dlfilter_fns {
...@@ -102,7 +103,8 @@ struct perf_dlfilter_fns { ...@@ -102,7 +103,8 @@ struct perf_dlfilter_fns {
char **(*args)(void *ctx, int *dlargc); char **(*args)(void *ctx, int *dlargc);
/* /*
* Return information about address (al->size must be set before * Return information about address (al->size must be set before
* calling). Returns 0 on success, -1 otherwise. * calling). Returns 0 on success, -1 otherwise. Call al_cleanup()
* when 'al' data is no longer needed.
*/ */
__s32 (*resolve_address)(void *ctx, __u64 address, struct perf_dlfilter_al *al); __s32 (*resolve_address)(void *ctx, __u64 address, struct perf_dlfilter_al *al);
/* Return instruction bytes and length */ /* Return instruction bytes and length */
...@@ -113,8 +115,13 @@ struct perf_dlfilter_fns { ...@@ -113,8 +115,13 @@ struct perf_dlfilter_fns {
struct perf_event_attr *(*attr)(void *ctx); struct perf_event_attr *(*attr)(void *ctx);
/* Read object code, return numbers of bytes read */ /* Read object code, return numbers of bytes read */
__s32 (*object_code)(void *ctx, __u64 ip, void *buf, __u32 len); __s32 (*object_code)(void *ctx, __u64 ip, void *buf, __u32 len);
/*
* If present (i.e. must check al_cleanup != NULL), call after
* resolve_address() to free any associated resources.
*/
void (*al_cleanup)(void *ctx, struct perf_dlfilter_al *al);
/* Reserved */ /* Reserved */
void *(*reserved[120])(void *); void *(*reserved[119])(void *);
}; };
/* /*
......
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
/* /*
* Test dlfilter C API. A perf.data file is synthesized and then processed * Test dlfilter C API. A perf.data file is synthesized and then processed
* by perf script with a dlfilter named dlfilter-test-api-v0.so. Also a C file * by perf script with dlfilters named dlfilter-test-api-v*.so. Also a C file
* is compiled to provide a dso to match the synthesized perf.data file. * is compiled to provide a dso to match the synthesized perf.data file.
*/ */
...@@ -37,6 +37,8 @@ ...@@ -37,6 +37,8 @@
#define MAP_START 0x400000 #define MAP_START 0x400000
#define DLFILTER_TEST_NAME_MAX 128
struct test_data { struct test_data {
struct perf_tool tool; struct perf_tool tool;
struct machine *machine; struct machine *machine;
...@@ -45,6 +47,8 @@ struct test_data { ...@@ -45,6 +47,8 @@ struct test_data {
u64 bar; u64 bar;
u64 ip; u64 ip;
u64 addr; u64 addr;
char name[DLFILTER_TEST_NAME_MAX];
char desc[DLFILTER_TEST_NAME_MAX];
char perf[PATH_MAX]; char perf[PATH_MAX];
char perf_data_file_name[PATH_MAX]; char perf_data_file_name[PATH_MAX];
char c_file_name[PATH_MAX]; char c_file_name[PATH_MAX];
...@@ -215,7 +219,7 @@ static int write_prog(char *file_name) ...@@ -215,7 +219,7 @@ static int write_prog(char *file_name)
return err ? -1 : 0; return err ? -1 : 0;
} }
static int get_dlfilters_path(char *buf, size_t sz) static int get_dlfilters_path(const char *name, char *buf, size_t sz)
{ {
char perf[PATH_MAX]; char perf[PATH_MAX];
char path[PATH_MAX]; char path[PATH_MAX];
...@@ -224,12 +228,12 @@ static int get_dlfilters_path(char *buf, size_t sz) ...@@ -224,12 +228,12 @@ static int get_dlfilters_path(char *buf, size_t sz)
perf_exe(perf, sizeof(perf)); perf_exe(perf, sizeof(perf));
perf_path = dirname(perf); perf_path = dirname(perf);
snprintf(path, sizeof(path), "%s/dlfilters/dlfilter-test-api-v0.so", perf_path); snprintf(path, sizeof(path), "%s/dlfilters/%s", perf_path, name);
if (access(path, R_OK)) { if (access(path, R_OK)) {
exec_path = get_argv_exec_path(); exec_path = get_argv_exec_path();
if (!exec_path) if (!exec_path)
return -1; return -1;
snprintf(path, sizeof(path), "%s/dlfilters/dlfilter-test-api-v0.so", exec_path); snprintf(path, sizeof(path), "%s/dlfilters/%s", exec_path, name);
free(exec_path); free(exec_path);
if (access(path, R_OK)) if (access(path, R_OK))
return -1; return -1;
...@@ -244,9 +248,9 @@ static int check_filter_desc(struct test_data *td) ...@@ -244,9 +248,9 @@ static int check_filter_desc(struct test_data *td)
char *desc = NULL; char *desc = NULL;
int ret; int ret;
if (get_filter_desc(td->dlfilters, "dlfilter-test-api-v0.so", &desc, &long_desc) && if (get_filter_desc(td->dlfilters, td->name, &desc, &long_desc) &&
long_desc && !strcmp(long_desc, "Filter used by the 'dlfilter C API' perf test") && long_desc && !strcmp(long_desc, "Filter used by the 'dlfilter C API' perf test") &&
desc && !strcmp(desc, "dlfilter to test v0 C API")) desc && !strcmp(desc, td->desc))
ret = 0; ret = 0;
else else
ret = -1; ret = -1;
...@@ -284,7 +288,7 @@ static int get_ip_addr(struct test_data *td) ...@@ -284,7 +288,7 @@ static int get_ip_addr(struct test_data *td)
static int do_run_perf_script(struct test_data *td, int do_early) static int do_run_perf_script(struct test_data *td, int do_early)
{ {
return system_cmd("%s script -i %s " return system_cmd("%s script -i %s "
"--dlfilter %s/dlfilter-test-api-v0.so " "--dlfilter %s/%s "
"--dlarg first " "--dlarg first "
"--dlarg %d " "--dlarg %d "
"--dlarg %" PRIu64 " " "--dlarg %" PRIu64 " "
...@@ -292,7 +296,7 @@ static int do_run_perf_script(struct test_data *td, int do_early) ...@@ -292,7 +296,7 @@ static int do_run_perf_script(struct test_data *td, int do_early)
"--dlarg %d " "--dlarg %d "
"--dlarg last", "--dlarg last",
td->perf, td->perf_data_file_name, td->dlfilters, td->perf, td->perf_data_file_name, td->dlfilters,
verbose, td->ip, td->addr, do_early); td->name, verbose, td->ip, td->addr, do_early);
} }
static int run_perf_script(struct test_data *td) static int run_perf_script(struct test_data *td)
...@@ -321,7 +325,7 @@ static int test__dlfilter_test(struct test_data *td) ...@@ -321,7 +325,7 @@ static int test__dlfilter_test(struct test_data *td)
u64 id = 99; u64 id = 99;
int err; int err;
if (get_dlfilters_path(td->dlfilters, PATH_MAX)) if (get_dlfilters_path(td->name, td->dlfilters, PATH_MAX))
return test_result("dlfilters not found", TEST_SKIP); return test_result("dlfilters not found", TEST_SKIP);
if (check_filter_desc(td)) if (check_filter_desc(td))
...@@ -399,14 +403,18 @@ static void test_data__free(struct test_data *td) ...@@ -399,14 +403,18 @@ static void test_data__free(struct test_data *td)
} }
} }
static int test__dlfilter(struct test_suite *test __maybe_unused, int subtest __maybe_unused) static int test__dlfilter_ver(int ver)
{ {
struct test_data td = {.fd = -1}; struct test_data td = {.fd = -1};
int pid = getpid(); int pid = getpid();
int err; int err;
pr_debug("\n-- Testing version %d API --\n", ver);
perf_exe(td.perf, sizeof(td.perf)); perf_exe(td.perf, sizeof(td.perf));
snprintf(td.name, sizeof(td.name), "dlfilter-test-api-v%d.so", ver);
snprintf(td.desc, sizeof(td.desc), "dlfilter to test v%d C API", ver);
snprintf(td.perf_data_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-perf-data", pid); snprintf(td.perf_data_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-perf-data", pid);
snprintf(td.c_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-prog.c", pid); snprintf(td.c_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-prog.c", pid);
snprintf(td.prog_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-prog", pid); snprintf(td.prog_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-prog", pid);
...@@ -416,4 +424,14 @@ static int test__dlfilter(struct test_suite *test __maybe_unused, int subtest __ ...@@ -416,4 +424,14 @@ static int test__dlfilter(struct test_suite *test __maybe_unused, int subtest __
return err; return err;
} }
static int test__dlfilter(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
{
int err = test__dlfilter_ver(0);
if (err)
return err;
/* No test for version 1 */
return test__dlfilter_ver(2);
}
DEFINE_SUITE("dlfilter C API", dlfilter); DEFINE_SUITE("dlfilter C API", dlfilter);
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include <subcmd/exec-cmd.h> #include <subcmd/exec-cmd.h>
#include <linux/zalloc.h> #include <linux/zalloc.h>
#include <linux/build_bug.h> #include <linux/build_bug.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include "debug.h" #include "debug.h"
#include "event.h" #include "event.h"
...@@ -63,6 +65,7 @@ static void al_to_d_al(struct addr_location *al, struct perf_dlfilter_al *d_al) ...@@ -63,6 +65,7 @@ static void al_to_d_al(struct addr_location *al, struct perf_dlfilter_al *d_al)
d_al->addr = al->addr; d_al->addr = al->addr;
d_al->comm = NULL; d_al->comm = NULL;
d_al->filtered = 0; d_al->filtered = 0;
d_al->priv = NULL;
} }
static struct addr_location *get_al(struct dlfilter *d) static struct addr_location *get_al(struct dlfilter *d)
...@@ -151,6 +154,11 @@ static char **dlfilter__args(void *ctx, int *dlargc) ...@@ -151,6 +154,11 @@ static char **dlfilter__args(void *ctx, int *dlargc)
return d->dlargv; return d->dlargv;
} }
static bool has_priv(struct perf_dlfilter_al *d_al_p)
{
return d_al_p->size >= offsetof(struct perf_dlfilter_al, priv) + sizeof(d_al_p->priv);
}
static __s32 dlfilter__resolve_address(void *ctx, __u64 address, struct perf_dlfilter_al *d_al_p) static __s32 dlfilter__resolve_address(void *ctx, __u64 address, struct perf_dlfilter_al *d_al_p)
{ {
struct dlfilter *d = (struct dlfilter *)ctx; struct dlfilter *d = (struct dlfilter *)ctx;
...@@ -177,9 +185,29 @@ static __s32 dlfilter__resolve_address(void *ctx, __u64 address, struct perf_dlf ...@@ -177,9 +185,29 @@ static __s32 dlfilter__resolve_address(void *ctx, __u64 address, struct perf_dlf
memcpy(d_al_p, &d_al, min((size_t)sz, sizeof(d_al))); memcpy(d_al_p, &d_al, min((size_t)sz, sizeof(d_al)));
d_al_p->size = sz; d_al_p->size = sz;
if (has_priv(d_al_p))
d_al_p->priv = memdup(&al, sizeof(al));
return 0; return 0;
} }
static void dlfilter__al_cleanup(void *ctx __maybe_unused, struct perf_dlfilter_al *d_al_p)
{
struct addr_location *al;
/* Ensure backward compatibility */
if (!has_priv(d_al_p) || !d_al_p->priv)
return;
al = d_al_p->priv;
d_al_p->priv = NULL;
addr_location__exit(al);
free(al);
}
static const __u8 *dlfilter__insn(void *ctx, __u32 *len) static const __u8 *dlfilter__insn(void *ctx, __u32 *len)
{ {
struct dlfilter *d = (struct dlfilter *)ctx; struct dlfilter *d = (struct dlfilter *)ctx;
...@@ -297,6 +325,7 @@ static const struct perf_dlfilter_fns perf_dlfilter_fns = { ...@@ -297,6 +325,7 @@ static const struct perf_dlfilter_fns perf_dlfilter_fns = {
.resolve_addr = dlfilter__resolve_addr, .resolve_addr = dlfilter__resolve_addr,
.args = dlfilter__args, .args = dlfilter__args,
.resolve_address = dlfilter__resolve_address, .resolve_address = dlfilter__resolve_address,
.al_cleanup = dlfilter__al_cleanup,
.insn = dlfilter__insn, .insn = dlfilter__insn,
.srcline = dlfilter__srcline, .srcline = dlfilter__srcline,
.attr = dlfilter__attr, .attr = dlfilter__attr,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment