Commit 2e30b973 authored by Linus Torvalds's avatar Linus Torvalds

Merge tag 'trace-v6.4-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace

Pull tracing fixes from Steven Rostedt:

 - Fix MAINTAINERS file to point to proper mailing list for rtla and rv

   The mailing list pointed to linux-trace-devel instead of
   linux-trace-kernel. The former is for the tracing libraries and the
   latter is for anything in the Linux kernel tree. The wrong mailing
   list was used because linux-trace-kernel did not exist when rtla and
   rv were created.

 - User events:

    - Fix matching of dynamic events to their user events

      When user writes to dynamic_events file, a lookup of the
      registered dynamic events is made, but there were some cases that
      a match could be incorrectly made.

    - Add auto cleanup of user events

      Have the user events automatically get removed when the last
      reference (file descriptor) is closed. This was asked for to
      prevent leaks of user events hanging around needing admins to
      clean them up.

    - Add persistent logic (but not let user space use it yet)

      In some cases, having a persistent user event (one that does not
      get cleaned up automatically) is useful. But there's still debates
      about how to expose this to user space. The infrastructure is
      added, but the API is not.

    - Update the selftests

      Update the user event selftests to reflect the above changes"

* tag 'trace-v6.4-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace:
  tracing/user_events: Document auto-cleanup and remove dyn_event refs
  selftests/user_events: Adapt dyn_test to non-persist events
  selftests/user_events: Ensure auto cleanup works as expected
  tracing/user_events: Add auto cleanup and future persist flag
  tracing/user_events: Track refcount consistently via put/get
  tracing/user_events: Store register flags on events
  tracing/user_events: Remove user_ns walk for groups
  selftests/user_events: Add perf self-test for empty arguments events
  selftests/user_events: Clear the events after perf self-test
  selftests/user_events: Add ftrace self-test for empty arguments events
  tracing/user_events: Fix the incorrect trace record for empty arguments events
  tracing: Modify print_fields() for fields output order
  tracing/user_events: Handle matching arguments that is null from dyn_events
  tracing/user_events: Prevent same name but different args event
  tracing/rv/rtla: Update MAINTAINERS file to point to proper mailing list
parents 4b0c7a1b 0113d461
...@@ -14,10 +14,6 @@ Programs can view status of the events via ...@@ -14,10 +14,6 @@ Programs can view status of the events via
/sys/kernel/tracing/user_events_status and can both register and write /sys/kernel/tracing/user_events_status and can both register and write
data out via /sys/kernel/tracing/user_events_data. data out via /sys/kernel/tracing/user_events_data.
Programs can also use /sys/kernel/tracing/dynamic_events to register and
delete user based events via the u: prefix. The format of the command to
dynamic_events is the same as the ioctl with the u: prefix applied.
Typically programs will register a set of events that they wish to expose to Typically programs will register a set of events that they wish to expose to
tools that can read trace_events (such as ftrace and perf). The registration tools that can read trace_events (such as ftrace and perf). The registration
process tells the kernel which address and bit to reflect if any tool has process tells the kernel which address and bit to reflect if any tool has
...@@ -144,6 +140,9 @@ its name. Delete will only succeed if there are no references left to the ...@@ -144,6 +140,9 @@ its name. Delete will only succeed if there are no references left to the
event (in both user and kernel space). User programs should use a separate file event (in both user and kernel space). User programs should use a separate file
to request deletes than the one used for registration due to this. to request deletes than the one used for registration due to this.
**NOTE:** By default events will auto-delete when there are no references left
to the event. Flags in the future may change this logic.
Unregistering Unregistering
------------- -------------
If after registering an event it is no longer wanted to be updated then it can If after registering an event it is no longer wanted to be updated then it can
......
...@@ -17827,7 +17827,7 @@ F: tools/testing/selftests/rtc/ ...@@ -17827,7 +17827,7 @@ F: tools/testing/selftests/rtc/
Real-time Linux Analysis (RTLA) tools Real-time Linux Analysis (RTLA) tools
M: Daniel Bristot de Oliveira <bristot@kernel.org> M: Daniel Bristot de Oliveira <bristot@kernel.org>
M: Steven Rostedt <rostedt@goodmis.org> M: Steven Rostedt <rostedt@goodmis.org>
L: linux-trace-devel@vger.kernel.org L: linux-trace-kernel@vger.kernel.org
S: Maintained S: Maintained
F: Documentation/tools/rtla/ F: Documentation/tools/rtla/
F: tools/tracing/rtla/ F: tools/tracing/rtla/
...@@ -18397,7 +18397,7 @@ F: drivers/infiniband/ulp/rtrs/ ...@@ -18397,7 +18397,7 @@ F: drivers/infiniband/ulp/rtrs/
RUNTIME VERIFICATION (RV) RUNTIME VERIFICATION (RV)
M: Daniel Bristot de Oliveira <bristot@kernel.org> M: Daniel Bristot de Oliveira <bristot@kernel.org>
M: Steven Rostedt <rostedt@goodmis.org> M: Steven Rostedt <rostedt@goodmis.org>
L: linux-trace-devel@vger.kernel.org L: linux-trace-kernel@vger.kernel.org
S: Maintained S: Maintained
F: Documentation/trace/rv/ F: Documentation/trace/rv/
F: include/linux/rv.h F: include/linux/rv.h
......
This diff is collapsed.
...@@ -847,7 +847,7 @@ static void print_fields(struct trace_iterator *iter, struct trace_event_call *c ...@@ -847,7 +847,7 @@ static void print_fields(struct trace_iterator *iter, struct trace_event_call *c
int ret; int ret;
void *pos; void *pos;
list_for_each_entry(field, head, link) { list_for_each_entry_reverse(field, head, link) {
trace_seq_printf(&iter->seq, " %s=", field->name); trace_seq_printf(&iter->seq, " %s=", field->name);
if (field->offset + field->size > iter->ent_size) { if (field->offset + field->size > iter->ent_size) {
trace_seq_puts(&iter->seq, "<OVERFLOW>"); trace_seq_puts(&iter->seq, "<OVERFLOW>");
......
...@@ -16,42 +16,140 @@ ...@@ -16,42 +16,140 @@
#include "../kselftest_harness.h" #include "../kselftest_harness.h"
const char *dyn_file = "/sys/kernel/tracing/dynamic_events"; const char *abi_file = "/sys/kernel/tracing/user_events_data";
const char *clear = "!u:__test_event"; const char *enable_file = "/sys/kernel/tracing/events/user_events/__test_event/enable";
static int Append(const char *value) static bool wait_for_delete(void)
{ {
int fd = open(dyn_file, O_RDWR | O_APPEND); int i;
int ret = write(fd, value, strlen(value));
for (i = 0; i < 1000; ++i) {
int fd = open(enable_file, O_RDONLY);
if (fd == -1)
return true;
close(fd);
usleep(1000);
}
return false;
}
static int reg_event(int fd, int *check, int bit, const char *value)
{
struct user_reg reg = {0};
reg.size = sizeof(reg);
reg.name_args = (__u64)value;
reg.enable_bit = bit;
reg.enable_addr = (__u64)check;
reg.enable_size = sizeof(*check);
if (ioctl(fd, DIAG_IOCSREG, &reg) == -1)
return -1;
return 0;
}
static int unreg_event(int fd, int *check, int bit)
{
struct user_unreg unreg = {0};
unreg.size = sizeof(unreg);
unreg.disable_bit = bit;
unreg.disable_addr = (__u64)check;
return ioctl(fd, DIAG_IOCSUNREG, &unreg);
}
static int parse(int *check, const char *value)
{
int fd = open(abi_file, O_RDWR);
int ret;
if (fd == -1)
return -1;
/* Until we have persist flags via dynamic events, use the base name */
if (value[0] != 'u' || value[1] != ':') {
close(fd);
return -1;
}
ret = reg_event(fd, check, 31, value + 2);
if (ret != -1) {
if (unreg_event(fd, check, 31) == -1)
printf("WARN: Couldn't unreg event\n");
}
close(fd); close(fd);
return ret; return ret;
} }
#define CLEAR() \ static int check_match(int *check, const char *first, const char *second, bool *match)
{
int fd = open(abi_file, O_RDWR);
int ret = -1;
if (fd == -1)
return -1;
if (reg_event(fd, check, 31, first) == -1)
goto cleanup;
if (reg_event(fd, check, 30, second) == -1) {
if (errno == EADDRINUSE) {
/* Name is in use, with different fields */
*match = false;
ret = 0;
}
goto cleanup;
}
*match = true;
ret = 0;
cleanup:
unreg_event(fd, check, 31);
unreg_event(fd, check, 30);
close(fd);
wait_for_delete();
return ret;
}
#define TEST_MATCH(x, y) \
do { \ do { \
int ret = Append(clear); \ bool match; \
if (ret == -1) \ ASSERT_NE(-1, check_match(&self->check, x, y, &match)); \
ASSERT_EQ(ENOENT, errno); \ ASSERT_EQ(true, match); \
} while (0) } while (0)
#define TEST_PARSE(x) \ #define TEST_NMATCH(x, y) \
do { \ do { \
ASSERT_NE(-1, Append(x)); \ bool match; \
CLEAR(); \ ASSERT_NE(-1, check_match(&self->check, x, y, &match)); \
ASSERT_EQ(false, match); \
} while (0) } while (0)
#define TEST_NPARSE(x) ASSERT_EQ(-1, Append(x)) #define TEST_PARSE(x) ASSERT_NE(-1, parse(&self->check, x))
#define TEST_NPARSE(x) ASSERT_EQ(-1, parse(&self->check, x))
FIXTURE(user) { FIXTURE(user) {
int check;
}; };
FIXTURE_SETUP(user) { FIXTURE_SETUP(user) {
CLEAR();
} }
FIXTURE_TEARDOWN(user) { FIXTURE_TEARDOWN(user) {
CLEAR(); wait_for_delete();
} }
TEST_F(user, basic_types) { TEST_F(user, basic_types) {
...@@ -95,33 +193,30 @@ TEST_F(user, size_types) { ...@@ -95,33 +193,30 @@ TEST_F(user, size_types) {
TEST_NPARSE("u:__test_event char a 20"); TEST_NPARSE("u:__test_event char a 20");
} }
TEST_F(user, flags) {
/* Should work */
TEST_PARSE("u:__test_event:BPF_ITER u32 a");
/* Forward compat */
TEST_PARSE("u:__test_event:BPF_ITER,FLAG_FUTURE u32 a");
}
TEST_F(user, matching) { TEST_F(user, matching) {
/* Register */ /* Single name matches */
ASSERT_NE(-1, Append("u:__test_event struct custom a 20")); TEST_MATCH("__test_event u32 a",
/* Should not match */ "__test_event u32 a");
TEST_NPARSE("!u:__test_event struct custom b");
/* Should match */ /* Multiple names match */
TEST_PARSE("!u:__test_event struct custom a"); TEST_MATCH("__test_event u32 a; u32 b",
/* Multi field reg */ "__test_event u32 a; u32 b");
ASSERT_NE(-1, Append("u:__test_event u32 a; u32 b"));
/* Non matching cases */ /* Multiple names match with dangling ; */
TEST_NPARSE("!u:__test_event u32 a"); TEST_MATCH("__test_event u32 a; u32 b",
TEST_NPARSE("!u:__test_event u32 b"); "__test_event u32 a; u32 b;");
TEST_NPARSE("!u:__test_event u32 a; u32 ");
TEST_NPARSE("!u:__test_event u32 a; u32 a"); /* Single name doesn't match */
/* Matching case */ TEST_NMATCH("__test_event u32 a",
TEST_PARSE("!u:__test_event u32 a; u32 b"); "__test_event u32 b");
/* Register */
ASSERT_NE(-1, Append("u:__test_event u32 a; u32 b")); /* Multiple names don't match */
/* Ensure trailing semi-colon case */ TEST_NMATCH("__test_event u32 a; u32 b",
TEST_PARSE("!u:__test_event u32 a; u32 b;"); "__test_event u32 b; u32 a");
/* Types don't match */
TEST_NMATCH("__test_event u64 a; u64 b",
"__test_event u32 a; u32 b");
} }
int main(int argc, char **argv) int main(int argc, char **argv)
......
...@@ -102,30 +102,56 @@ static int get_print_fmt(char *buffer, int len) ...@@ -102,30 +102,56 @@ static int get_print_fmt(char *buffer, int len)
return -1; return -1;
} }
static bool wait_for_delete(void)
{
int i;
for (i = 0; i < 1000; ++i) {
int fd = open(enable_file, O_RDONLY);
if (fd == -1)
return true;
close(fd);
usleep(1000);
}
return false;
}
static int clear(int *check) static int clear(int *check)
{ {
struct user_unreg unreg = {0}; struct user_unreg unreg = {0};
int fd;
unreg.size = sizeof(unreg); unreg.size = sizeof(unreg);
unreg.disable_bit = 31; unreg.disable_bit = 31;
unreg.disable_addr = (__u64)check; unreg.disable_addr = (__u64)check;
int fd = open(data_file, O_RDWR); fd = open(data_file, O_RDWR);
if (fd == -1) if (fd == -1)
return -1; return -1;
if (ioctl(fd, DIAG_IOCSUNREG, &unreg) == -1) if (ioctl(fd, DIAG_IOCSUNREG, &unreg) == -1)
if (errno != ENOENT) if (errno != ENOENT)
return -1; goto fail;
if (ioctl(fd, DIAG_IOCSDEL, "__test_event") == -1) if (ioctl(fd, DIAG_IOCSDEL, "__test_event") == -1) {
if (errno != ENOENT) if (errno == EBUSY) {
return -1; if (!wait_for_delete())
goto fail;
} else if (errno != ENOENT)
goto fail;
}
close(fd); close(fd);
return 0; return 0;
fail:
close(fd);
return -1;
} }
static int check_print_fmt(const char *event, const char *expected, int *check) static int check_print_fmt(const char *event, const char *expected, int *check)
...@@ -155,9 +181,8 @@ static int check_print_fmt(const char *event, const char *expected, int *check) ...@@ -155,9 +181,8 @@ static int check_print_fmt(const char *event, const char *expected, int *check)
/* Register should work */ /* Register should work */
ret = ioctl(fd, DIAG_IOCSREG, &reg); ret = ioctl(fd, DIAG_IOCSREG, &reg);
close(fd);
if (ret != 0) { if (ret != 0) {
close(fd);
printf("Reg failed in fmt\n"); printf("Reg failed in fmt\n");
return ret; return ret;
} }
...@@ -165,6 +190,8 @@ static int check_print_fmt(const char *event, const char *expected, int *check) ...@@ -165,6 +190,8 @@ static int check_print_fmt(const char *event, const char *expected, int *check)
/* Ensure correct print_fmt */ /* Ensure correct print_fmt */
ret = get_print_fmt(print_fmt, sizeof(print_fmt)); ret = get_print_fmt(print_fmt, sizeof(print_fmt));
close(fd);
if (ret != 0) if (ret != 0)
return ret; return ret;
...@@ -228,6 +255,12 @@ TEST_F(user, register_events) { ...@@ -228,6 +255,12 @@ TEST_F(user, register_events) {
ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, &reg)); ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, &reg));
ASSERT_EQ(0, reg.write_index); ASSERT_EQ(0, reg.write_index);
/* Multiple registers to same name but different args should fail */
reg.enable_bit = 29;
reg.name_args = (__u64)"__test_event u32 field1;";
ASSERT_EQ(-1, ioctl(self->data_fd, DIAG_IOCSREG, &reg));
ASSERT_EQ(EADDRINUSE, errno);
/* Ensure disabled */ /* Ensure disabled */
self->enable_fd = open(enable_file, O_RDWR); self->enable_fd = open(enable_file, O_RDWR);
ASSERT_NE(-1, self->enable_fd); ASSERT_NE(-1, self->enable_fd);
...@@ -250,10 +283,10 @@ TEST_F(user, register_events) { ...@@ -250,10 +283,10 @@ TEST_F(user, register_events) {
unreg.disable_bit = 30; unreg.disable_bit = 30;
ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSUNREG, &unreg)); ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSUNREG, &unreg));
/* Delete should work only after close and unregister */ /* Delete should have been auto-done after close and unregister */
close(self->data_fd); close(self->data_fd);
self->data_fd = open(data_file, O_RDWR);
ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSDEL, "__test_event")); ASSERT_EQ(true, wait_for_delete());
} }
TEST_F(user, write_events) { TEST_F(user, write_events) {
...@@ -310,6 +343,39 @@ TEST_F(user, write_events) { ...@@ -310,6 +343,39 @@ TEST_F(user, write_events) {
ASSERT_EQ(EINVAL, errno); ASSERT_EQ(EINVAL, errno);
} }
TEST_F(user, write_empty_events) {
struct user_reg reg = {0};
struct iovec io[1];
int before = 0, after = 0;
reg.size = sizeof(reg);
reg.name_args = (__u64)"__test_event";
reg.enable_bit = 31;
reg.enable_addr = (__u64)&self->check;
reg.enable_size = sizeof(self->check);
io[0].iov_base = &reg.write_index;
io[0].iov_len = sizeof(reg.write_index);
/* Register should work */
ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, &reg));
ASSERT_EQ(0, reg.write_index);
ASSERT_EQ(0, self->check);
/* Enable event */
self->enable_fd = open(enable_file, O_RDWR);
ASSERT_NE(-1, write(self->enable_fd, "1", sizeof("1")))
/* Event should now be enabled */
ASSERT_EQ(1 << reg.enable_bit, self->check);
/* Write should make it out to ftrace buffers */
before = trace_bytes();
ASSERT_NE(-1, writev(self->data_fd, (const struct iovec *)io, 1));
after = trace_bytes();
ASSERT_GT(after, before);
}
TEST_F(user, write_fault) { TEST_F(user, write_fault) {
struct user_reg reg = {0}; struct user_reg reg = {0};
struct iovec io[2]; struct iovec io[2];
......
...@@ -81,6 +81,32 @@ static int get_offset(void) ...@@ -81,6 +81,32 @@ static int get_offset(void)
return offset; return offset;
} }
static int clear(int *check)
{
struct user_unreg unreg = {0};
unreg.size = sizeof(unreg);
unreg.disable_bit = 31;
unreg.disable_addr = (__u64)check;
int fd = open(data_file, O_RDWR);
if (fd == -1)
return -1;
if (ioctl(fd, DIAG_IOCSUNREG, &unreg) == -1)
if (errno != ENOENT)
return -1;
if (ioctl(fd, DIAG_IOCSDEL, "__test_event") == -1)
if (errno != ENOENT)
return -1;
close(fd);
return 0;
}
FIXTURE(user) { FIXTURE(user) {
int data_fd; int data_fd;
int check; int check;
...@@ -93,6 +119,9 @@ FIXTURE_SETUP(user) { ...@@ -93,6 +119,9 @@ FIXTURE_SETUP(user) {
FIXTURE_TEARDOWN(user) { FIXTURE_TEARDOWN(user) {
close(self->data_fd); close(self->data_fd);
if (clear(&self->check) != 0)
printf("WARNING: Clear didn't work!\n");
} }
TEST_F(user, perf_write) { TEST_F(user, perf_write) {
...@@ -160,6 +189,59 @@ TEST_F(user, perf_write) { ...@@ -160,6 +189,59 @@ TEST_F(user, perf_write) {
ASSERT_EQ(0, self->check); ASSERT_EQ(0, self->check);
} }
TEST_F(user, perf_empty_events) {
struct perf_event_attr pe = {0};
struct user_reg reg = {0};
struct perf_event_mmap_page *perf_page;
int page_size = sysconf(_SC_PAGESIZE);
int id, fd;
__u32 *val;
reg.size = sizeof(reg);
reg.name_args = (__u64)"__test_event";
reg.enable_bit = 31;
reg.enable_addr = (__u64)&self->check;
reg.enable_size = sizeof(self->check);
/* Register should work */
ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, &reg));
ASSERT_EQ(0, reg.write_index);
ASSERT_EQ(0, self->check);
/* Id should be there */
id = get_id();
ASSERT_NE(-1, id);
pe.type = PERF_TYPE_TRACEPOINT;
pe.size = sizeof(pe);
pe.config = id;
pe.sample_type = PERF_SAMPLE_RAW;
pe.sample_period = 1;
pe.wakeup_events = 1;
/* Tracepoint attach should work */
fd = perf_event_open(&pe, 0, -1, -1, 0);
ASSERT_NE(-1, fd);
perf_page = mmap(NULL, page_size * 2, PROT_READ, MAP_SHARED, fd, 0);
ASSERT_NE(MAP_FAILED, perf_page);
/* Status should be updated */
ASSERT_EQ(1 << reg.enable_bit, self->check);
/* Ensure write shows up at correct offset */
ASSERT_NE(-1, write(self->data_fd, &reg.write_index,
sizeof(reg.write_index)));
val = (void *)(((char *)perf_page) + perf_page->data_offset);
ASSERT_EQ(PERF_RECORD_SAMPLE, *val);
munmap(perf_page, page_size * 2);
close(fd);
/* Status should be updated */
ASSERT_EQ(0, self->check);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
return test_harness_run(argc, argv); return test_harness_run(argc, argv);
......
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