tracing: Support reading trace event format file larger than PAGE_SIZE
PATCH 1/1 tracing: Support reading trace event format file larger than PAGE_SIZE
From: Shiju Jose <[email protected]>
When userspace reads a trace event format file, the maximum data size that can be read is limited to PAGE_SIZE by the seq_read() and seq_read_iter() functions. This results in userspace receiving partial data if the format file is larger than PAGE_SIZE, requiring a workaround to read the complete data from the format file.
Add support for reading trace event format files larger than PAGE_SIZE when needed by userspace.
--- kernel/trace/trace_events.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-)
kernel/trace/trace_events.c +31 -1
@@ -1801,6 +1801,36 @@ static int trace_format_open(struct inode *inode, struct file *file)return 0 ; } +/** +* trace_format_read - read() method for format file. +* @file: the file to read from +* @buf: the buffer to read to +* @size: the maximum number of bytes to read +* @ppos: the current position in the file +* +* * Return: +* * %0 - No bytes copied (EOF). +* * %>0 - Number of bytes copied. +* * %<0 - Error code. +*/ +static ssize_t trace_format_read (struct file *file,char __user *buf,size_t size,loff_t *ppos) +{ +ssize_t copied =0 ; +ssize_t ret; + +do { + ret =seq_read (file, buf + copied, size - copied, ppos); +if (ret <0 ) +return ret; + + copied += ret; +if (copied >= size) +break ; + }while (ret); + +return copied; +} +#ifdef CONFIG_PERF_EVENTS static ssize_t event_id_read (struct file *filp,char __user *ubuf,size_t cnt,loff_t *ppos)
@@ -2284,7 +2314,7 @@ static const struct file_operations ftrace_enable_fops = {static const struct file_operations ftrace_event_format_fops = { .open = trace_format_open, - .read = seq_read, + .read = trace_format_read, .llseek = seq_lseek, .release = seq_release, };
Signature · 1 line
2.43.0
Quoted 8 lines · On Thu, 2 Jan 2025 17:43:17 +0000 <[email protected]> wrote:
From: Shiju Jose <[email protected]>
When userspace reads a trace event format file, the maximum data size that can be read is limited to
PAGE_SIZEby theseq_read()andseq_read_iter()functions. This results in userspace receiving partial data if the format file is larger thanPAGE_SIZE, requiring a workaround to read the complete data from the format file.
Quoted 4 lines
Add support for reading trace event format files larger than
PAGE_SIZEwhen needed by userspace.Signed-off-by Shiju Jose <[email protected]>
How is this an issue? This is common for all pseudo files and can be handled properly from user space.
Here, with this program:
read.c:
-------------------------8<------------------------- #include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
int main (int argc, char **argv)
{
char *buf;
int fd;
off_t size;
int r, s;
if (argc < 2) {
printf("usage: %s file\n", argv[0]);
exit(-1);
}
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
perror(argv[1]);
exit(-1);
}
size = BUFSIZ * 10;
buf = malloc(size);
for (s = 0, r = 1; r > 0; s += r) {
r = read(fd, buf, size);
if (r < 0) {
perror(argv[1]);
exit(-1);
}
printf("Read %d bytes from %s\n", r, argv[1]);
}
free(buf);
close(fd);
return 0;
}------------------------->8-------------------------
$ read /proc/kallsyms Read 4091 bytes from /proc/kallsyms Read 4075 bytes from /proc/kallsyms Read 4078 bytes from /proc/kallsyms Read 4083 bytes from /proc/kallsyms Read 4093 bytes from /proc/kallsyms Read 4076 bytes from /proc/kallsyms Read 4080 bytes from /proc/kallsyms Read 4086 bytes from /proc/kallsyms Read 4080 bytes from /proc/kallsyms Read 4064 bytes from /proc/kallsyms Read 4071 bytes from /proc/kallsyms Read 4063 bytes from /proc/kallsyms Read 4069 bytes from /proc/kallsyms Read 4079 bytes from /proc/kallsyms Read 4063 bytes from /proc/kallsyms Read 4072 bytes from /proc/kallsyms Read 4046 bytes from /proc/kallsyms Read 4091 bytes from /proc/kallsyms Read 4090 bytes from /proc/kallsyms Read 4067 bytes from /proc/kallsyms Read 4080 bytes from /proc/kallsyms Read 4066 bytes from /proc/kallsyms Read 4085 bytes from /proc/kallsyms Read 4095 bytes from /proc/kallsyms Read 4076 bytes from /proc/kallsyms Read 4090 bytes from /proc/kallsyms Read 4066 bytes from /proc/kallsyms Read 4073 bytes from /proc/kallsyms Read 4091 bytes from /proc/kallsyms Read 4075 bytes from /proc/kallsyms Read 4076 bytes from /proc/kallsyms Read 4048 bytes from /proc/kallsyms Read 4074 bytes from /proc/kallsyms Read 4058 bytes from /proc/kallsyms Read 4074 bytes from /proc/kallsyms [..] Read 4052 bytes from /proc/kallsyms Read 4061 bytes from /proc/kallsyms Read 4061 bytes from /proc/kallsyms Read 4053 bytes from /proc/kallsyms Read 4083 bytes from /proc/kallsyms Read 4066 bytes from /proc/kallsyms Read 4093 bytes from /proc/kallsyms Read 4072 bytes from /proc/kallsyms Read 1982 bytes from /proc/kallsyms Read 0 bytes from /proc/kallsyms
You see, it requires multiple reads to pull in an entire kernel pseudo file. None of those reads are greater than PAGE_SIZE. Why should trace format files be any different?
libtracefs handles this perfectly fine:
https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/tree/src/tracefs-utils.c#n343
Looks like you are trying to change the kernel to fix a user space bug :-/
NAK!
-- Steve
Quoted 126 lines
-----Original Message-----
From: Steven Rostedt <[email protected]>
Sent: 06 January 2025 17:22
To: Shiju Jose <[email protected]>
Cc: [email protected]; [email protected]; linux-trace- [email protected]; [email protected]; Linuxarm <[email protected]>; Jonathan Cameron <[email protected]>; tanxiaofei <[email protected]>; Zengtao (B) <[email protected]>
Subject: Re: [PATCH 1/1] tracing: Support reading trace event format file larger thanPAGE_SIZEQuoted 8 lines · On Thu, 2 Jan 2025 17:43:17 +0000 <[email protected]> wrote:
From: Shiju Jose <[email protected]>
When userspace reads a trace event format file, the maximum data size that can be read is limited to
PAGE_SIZEby theseq_read()andseq_read_iter()functions. This results in userspace receiving partial data if the format file is larger thanPAGE_SIZE, requiring a workaround to read the complete data from the format file.Quoted 4 lines
Add support for reading trace event format files larger than
PAGE_SIZEwhen needed by userspace.Signed-off-by Shiju Jose <[email protected]>How is this an issue? This is common for all pseudo files and can be handled properly from user space.
Here, with this program:
read.c:
-------------------------8<------------------------- #include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>int main (int argc, char **argv)
{ char *buf; int fd; off_t size; int r, s; if (argc < 2) { printf("usage: %s file\n", argv[0]); exit(-1); } fd = open(argv[1], O_RDONLY); if (fd < 0) { perror(argv[1]); exit(-1); } size = BUFSIZ * 10; buf = malloc(size); for (s = 0, r = 1; r > 0; s += r) { r = read(fd, buf, size); if (r < 0) { perror(argv[1]); exit(-1); } printf("Read %d bytes from %s\n", r, argv[1]); } free(buf); close(fd); return 0; }------------------------->8-------------------------
$ read /proc/kallsyms Read 4091 bytes from /proc/kallsyms Read 4075 bytes from /proc/kallsyms Read 4078 bytes from /proc/kallsyms Read 4083 bytes from /proc/kallsyms Read 4093 bytes from /proc/kallsyms Read 4076 bytes from /proc/kallsyms Read 4080 bytes from /proc/kallsyms Read 4086 bytes from /proc/kallsyms Read 4080 bytes from /proc/kallsyms Read 4064 bytes from /proc/kallsyms Read 4071 bytes from /proc/kallsyms Read 4063 bytes from /proc/kallsyms Read 4069 bytes from /proc/kallsyms Read 4079 bytes from /proc/kallsyms Read 4063 bytes from /proc/kallsyms Read 4072 bytes from /proc/kallsyms Read 4046 bytes from /proc/kallsyms Read 4091 bytes from /proc/kallsyms Read 4090 bytes from /proc/kallsyms Read 4067 bytes from /proc/kallsyms Read 4080 bytes from /proc/kallsyms Read 4066 bytes from /proc/kallsyms Read 4085 bytes from /proc/kallsyms Read 4095 bytes from /proc/kallsyms Read 4076 bytes from /proc/kallsyms Read 4090 bytes from /proc/kallsyms Read 4066 bytes from /proc/kallsyms Read 4073 bytes from /proc/kallsyms Read 4091 bytes from /proc/kallsyms Read 4075 bytes from /proc/kallsyms Read 4076 bytes from /proc/kallsyms Read 4048 bytes from /proc/kallsyms Read 4074 bytes from /proc/kallsyms Read 4058 bytes from /proc/kallsyms Read 4074 bytes from /proc/kallsyms [..] Read 4052 bytes from /proc/kallsyms Read 4061 bytes from /proc/kallsyms Read 4061 bytes from /proc/kallsyms Read 4053 bytes from /proc/kallsyms Read 4083 bytes from /proc/kallsyms Read 4066 bytes from /proc/kallsyms Read 4093 bytes from /proc/kallsyms Read 4072 bytes from /proc/kallsyms Read 1982 bytes from /proc/kallsyms Read 0 bytes from /proc/kallsyms
Quoted 3 lines
You see, it requires multiple reads to pull in an entire kernel pseudo file. None of those reads are greater than
PAGE_SIZE. Why should trace format files be any different?
Thanks for the reply.
Yes. I had a fix/workaround in the userspace rasdaemon with multiple reads like above as reported previously in the following thread.
https://lore.kernel.org/lkml/[email protected]/
However thought a solution in the common kernel code for the format file may be better. I will go ahead with the user space solution.
Also shared an information in the above thread about libtraceevent __parse_event() does not return error when parse_format() fail with incomplete formt data, which resulted initialization for the trace event does not fail in the user space tool.
Quoted 10 lines
libtracefs handles this perfectly fine:
https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/tree/src/tracefs-
utils.c#n343Looks like you are trying to change the kernel to fix a user space bug :-/
NAK!
-- Steve
Thanks,
Shiju
Steven Rostedt
maintainerQuoted 9 lines · On Mon, 6 Jan 2025 18:15:36 +0000 Shiju Jose <[email protected]> wrote:
Quoted 3 lines
You see, it requires multiple reads to pull in an entire kernel pseudo file. None of those reads are greater than
PAGE_SIZE. Why should trace format files be any different?Thanks for the reply.
Yes. I had a fix/workaround in the userspace rasdaemon with multiple reads like above as reported previously in the following thread.
https://lore.kernel.org/lkml/[email protected]/
However thought a solution in the common kernel code for the format file may be better. I will go ahead with the user space solution.
That would make it different than every other pseudo file in the kernel. I rather not do that.
Quoted 4 lines
Also shared an information in the above thread about libtraceevent
__parse_event()does not return error whenparse_format()fail with incomplete formt data, which resulted initialization for the trace event does not fail in the user space tool.
Hmm, can you send me the the format file that failed?
Just the output of "cat /sys/kernel/tracing/events/<system>/<event>/format" will do.
-- Steve
Quoted 33 lines
-----Original Message-----
From: Steven Rostedt <[email protected]>
Sent: 06 January 2025 22:12
To: Shiju Jose <[email protected]>
Cc: [email protected]; [email protected]; linux-trace- [email protected]; [email protected]; Linuxarm <[email protected]>; Jonathan Cameron <[email protected]>; tanxiaofei <[email protected]>; Zengtao (B) <[email protected]>
Subject: Re: [PATCH 1/1] tracing: Support reading trace event format file larger thanPAGE_SIZEQuoted 10 lines · On Mon, 6 Jan 2025 18:15:36 +0000 Shiju Jose <[email protected]> wrote:
Quoted 3 lines
You see, it requires multiple reads to pull in an entire kernel pseudo file. None of those reads are greater than
PAGE_SIZE. Why should trace format files be any different?Thanks for the reply.
Yes. I had a fix/workaround in the userspace rasdaemon with multiple reads like above as reported previously in the following thread.
https://lore.kernel.org/lkml/[email protected]
om/ However thought a solution in the common kernel code for the format file may be better. I will go ahead with the user space solution.That would make it different than every other pseudo file in the kernel. I rather not do that.
Quoted 4 lines
Also shared an information in the above thread about libtraceevent
__parse_event()does not return error whenparse_format()fail with incomplete formt data, which resulted initialization for the trace event does
Quoted 6 lines
not fail in the user space tool.
Hmm, can you send me the the format file that failed?
Just the output of "cat /sys/kernel/tracing/events/<system>/<event>/format" will do.
Please find attached format file, which might have failed to parse because of the unsupported formats in the libtraceevent you mentioned in the following thread.
https://lore.kernel.org/lkml/[email protected]/#t
Please find the improvement in the libraceevent which I mentioned for not returning error when parsing failed,
===========================================
src/event-parse.c +7 -2
@@ -7905,9 +7905,14 @@ __parse_event(struct tep_handle *tep, const char *buf, unsigned long size, const char *sys) { - int ret = parse_format(eventp, tep, buf, size, sys); -struct tep_event *event = *eventp; + int ret; +struct tep_event *event; + + ret =parse_format (eventp, tep, buf, size, sys); +if (ret) +return ret; + event = *eventp;if (event ==NULL )return ret; =========================================
Quoted 2 lines
-- Steve
Thanks,
Shiju
Steven Rostedt
maintainerQuoted 16 lines · On Tue, 7 Jan 2025 11:04:57 +0000 Shiju Jose <[email protected]> wrote:
Please find attached format file, which might have failed to parse because of the unsupported formats in the libtraceevent you mentioned in the following thread.
https://lore.kernel.org/lkml/[email protected]/#tPlease find the improvement in the libraceevent which I mentioned for not returning error when parsing failed,
===========================================src/event-parse.c +0
-2@@ -7905,9 +7905,14 @@ __parse_event(struct tep_handle *tep, const char *buf, unsigned long size, const char *sys) { - int ret = parse_format(eventp, tep, buf, size, sys); -struct tep_event *event = *eventp;
src/event-parse.c __parse_event(struct tep_handle *tep, · line 7917
Quoted 10 lines
src/event-parse.c +7
-0+int ret; +struct tep_event *event; + + ret =parse_format (eventp, tep, buf, size, sys); +if (ret) +return ret; + event = *eventp;if (event ==NULL )return ret;
Actually, this is the proper patch:
src/event-parse.c +1 -1
@@ -7841,7 +7841,7 @@ static enum tep_errno parse_format(struct tep_event **eventp, ret =event_read_format (event);if (ret <0 ) { ret =TEP_ERRNO__READ_FORMAT_FAILED ; - goto event_parse_failed; + goto event_alloc_failed; }/*
As it's OK if it returns that it failed to parse the print format, as it can still read the event. But if it fails to read the fields, then it is basically useless.
-- Steve
Steven Rostedt
maintainerQuoted 15 lines · On Tue, 7 Jan 2025 18:02:24 -0500 Steven Rostedt <[email protected]> wrote:
src/event-parse.c +1
-1@@ -7841,7 +7841,7 @@ static enum tep_errno parse_format(struct tep_event **eventp, ret =event_read_format (event);if (ret <0 ) { ret =TEP_ERRNO__READ_FORMAT_FAILED ; - goto event_parse_failed; + goto event_alloc_failed; }/*
Quoted 3 lines
As it's OK if it returns that it failed to parse the print format, as it can still read the event. But if it fails to read the fields, then it is basically useless.
And the event you posted would still succeed. And it should succeed actually. It just failed to parse the print format. There's a lot of events that do as the print format is pretty much full C code, and libtraceveent is not a full C parser. It can also call functions that it doesn't know about which means even if it were a full C parser it would still fail to parse.
But it does flag the event as "TEP_EVENT_FL_FAILED", and should display "[FAILED_TO_PARSE]" when printing the generic output.
I still think failing to parse the fields is more of a critical error though. That shouldn't happen, and if it does, it should be corrected.
-- Steve
Quoted 41 lines
-----Original Message-----
From: Steven Rostedt <[email protected]>
Sent: 07 January 2025 23:19
To: Shiju Jose <[email protected]>
Cc: [email protected]; [email protected]; linux-trace- [email protected]; [email protected]; Linuxarm <[email protected]>; Jonathan Cameron <[email protected]>; tanxiaofei <[email protected]>; Zengtao (B) <[email protected]>
Subject: Re: [PATCH 1/1] tracing: Support reading trace event format file larger thanPAGE_SIZEsrc/event-parse.c static enum tep_errno parse_format(struct
Quoted 5 lines · On Tue, 7 Jan 2025 18:02:24 -0500 Steven Rostedt <[email protected]> wrote:
src/event-parse.c index renamed from src/event-parse.c +0
-0
33ed7fb47fff..f2e50b0e8992100644src/event-parse.c +0
-0@@ -7841,7 +7841,7 @@ static enum tep_errno parse_format(struct
tep_event**eventp,Quoted 10 lines
src/event-parse.c +1
-1ret =event_read_format (event);if (ret <0 ) { ret =TEP_ERRNO__READ_FORMAT_FAILED ; - goto event_parse_failed; + goto event_alloc_failed; }/* Quoted 3 lines
As it's OK if it returns that it failed to parse the print format, as it can still read the event. But if it fails to read the fields, then it is basically useless.
And the event you posted would still succeed. And it should succeed actually. It just failed to parse the print format. There's a lot of events that do as the print format is pretty much full C code, and libtraceveent is not a full C parser. It can also call functions that it doesn't know about which means even if it were a full C parser it would still fail to parse.
Quoted 3 lines
But it does flag the event as "
TEP_EVENT_FL_FAILED", and should display "[FAILED_TO_PARSE]" when printing the generic output.
Ok.
Quoted 2 lines
I still think failing to parse the fields is more of a critical error though. That shouldn't happen, and if it does, it should be corrected.
As reported previously, parsing fields succeed in the rasdaemon for CXL events, when format file is larger than PAGE_SIZE, with the rasdaemon change for reading complete format file.
Quoted 2 lines
-- Steve
Thanks,
Shiju
Quoted 63 lines
-----Original Message-----
From: Steven Rostedt <[email protected]>
Sent: 07 January 2025 23:02
To: Shiju Jose <[email protected]>
Cc: [email protected]; [email protected]; linux-trace- [email protected]; [email protected]; Linuxarm <[email protected]>; Jonathan Cameron <[email protected]>; tanxiaofei <[email protected]>; Zengtao (B) <[email protected]>
Subject: Re: [PATCH 1/1] tracing: Support reading trace event format file larger thanPAGE_SIZEQuoted 2 lines · On Tue, 7 Jan 2025 11:04:57 +0000 Shiju Jose <[email protected]> wrote:
Please find attached format file, which might have failed to parse because of the unsupported formats in the libtraceevent you mentioned in the
following thread.
Quoted 1 line
https://lore.kernel.org/lkml/[email protected]/#t
Quoted 14 lines
Please find the improvement in the libraceevent which I mentioned for not returning error when parsing failed,
===========================================src/event-parse.c index renamed from src/event-parse.c +0
-00427061..b9264cb 100644
src/event-parse.c +0
-2@@ -7905,9 +7905,14 @@ __parse_event(struct tep_handle *tep, const char *buf, unsigned long size, const char *sys) { - int ret = parse_format(eventp, tep, buf, size, sys); -struct tep_event *event = *eventp;src/event-parse.c __parse_event(struct tep_handle *tep, · line 7917
Quoted 10 lines
src/event-parse.c +7
-0+int ret; +struct tep_event *event; + + ret =parse_format (eventp, tep, buf, size, sys); +if (ret) +return ret; + event = *eventp;if (event ==NULL )return ret;Actually, this is the proper patch:
src/event-parse.c index renamed from src/event-parse.c +0
-0
33ed7fb47fff..f2e50b0e8992100644src/event-parse.c +1
-1@@ -7841,7 +7841,7 @@ static enum tep_errno parse_format(struct tep_event **eventp, ret =event_read_format (event);if (ret <0 ) { ret =TEP_ERRNO__READ_FORMAT_FAILED ; - goto event_parse_failed; + goto event_alloc_failed; }/*
Quoted 2 lines
As it's OK if it returns that it failed to parse the print format, as it can still read the event. But if it fails to read the fields, then it is basically useless.
Thanks.
Your patch worked fine for returning error to the user space tool for the incomplete format data case as the following check become true in the __parse_event() after freeing 'event' in the goto event_alloc_failed.
if (event == NULL)
return ret;
Quoted 2 lines
-- Steve
Thanks,
Shiju