Skip to contentSkip to conversation

perf trace: Fix potential u64 underflow in duration calculation

PATCH Superseded Michael Petlan 3 participants 7 messages Mar 31 – Apr 4 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf trace: Fix potential u64 underflow in duration calculation

Although it happens very rarely, in case of out-of-order events (i.e. due to CPU migration when a syscall is executed), the calculation of event duration might underflow and thus a bogus value is printed:

    2.804 ( 0.001 ms): :49553/49553 rt_sigaction(sig: QUIT, act: 0x7fff403ed6e0, oact: 0x7fff403ed780, sigsetsize: 8) = 0
    2.807 ( 0.001 ms): :49553/49553 rt_sigaction(sig: CHLD, act: 0x7fff403ed6e0, oact: 0x7fff403ed780, sigsetsize: 8) = 0
    2.815 (18446744073709.438 ms): :49553/49553 execve(filename: 0xbb173a30, argv: 0x55aabb171930, envp: 0x55aabb171120) = 0
    2.815 ( 0.534 ms): pwd/49553  ... [continued]: execve())                                           = 0

Check for possible underflow first and in case of a bogus value, do not print it.

---
 tools/perf/builtin-trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-trace.c +1 -1
index f487fbaa0ad6..c9de6bb9b0ea 100644
@@ -2956,7 +2956,7 @@ static int trace__sys_exit(struct trace *trace, struct evsel *evsel,
 		++trace->stats.vfs_getname;
 	}
 
-	if (ttrace->entry_time) {
+	if (ttrace->entry_time && sample->time > ttrace->entry_time) {
 		duration = sample->time - ttrace->entry_time;
 		if (trace__filter_duration(trace, duration))
 			goto out;
Signature · 1 line
2.47.3
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Michael Petlan

Although it happens very rarely, in case of out-of-order events (i.e. due to CPU migration when a syscall is executed), the calculation of event duration might underflow and thus a bogus value is printed:

    2.804 ( 0.001 ms): :49553/49553 rt_sigaction(sig: QUIT, act: 0x7fff403ed6e0, oact: 0x7fff403ed780, sigsetsize: 8) = 0
    2.807 ( 0.001 ms): :49553/49553 rt_sigaction(sig: CHLD, act: 0x7fff403ed6e0, oact: 0x7fff403ed780, sigsetsize: 8) = 0
    2.815 (18446744073709.438 ms): :49553/49553 execve(filename: 0xbb173a30, argv: 0x55aabb171930, envp: 0x55aabb171120) = 0
    2.815 ( 0.534 ms): pwd/49553  ... [continued]: execve())                                           = 0
Quoted 4 lines

Check for possible underflow first and in case of a bogus value, do not print it.

Sashiko notes [1] that maybe this should be >= so that 0ns system calls still have a duration. I'm not sure how sensible that is.

Thanks,
Ian

[1] https://sashiko.dev/#/patchset/20260331101020.1270363-1-mpetlan%40redhat.com

Quoted 20 lines
---
 tools/perf/builtin-trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-trace.c +1 -2
index f487fbaa0ad6..c9de6bb9b0ea 100644
@@ -2956,7 +2956,7 @@ static int trace__sys_exit(struct trace *trace, struct evsel *evsel,
                ++trace->stats.vfs_getname;
        }
 
-       if (ttrace->entry_time) {
+       if (ttrace->entry_time && sample->time > ttrace->entry_time) {
                duration = sample->time - ttrace->entry_time;
                if (trace__filter_duration(trace, duration))
                        goto out;
--

2.47.3

#2 lore raw
Quoted 14 lines · Ian Rogers
Quoted 13 lines · Michael Petlan

Although it happens very rarely, in case of out-of-order events (i.e. due to CPU migration when a syscall is executed), the calculation of event duration might underflow and thus a bogus value is printed:

    2.804 ( 0.001 ms): :49553/49553 rt_sigaction(sig: QUIT, act: 0x7fff403ed6e0, oact: 0x7fff403ed780, sigsetsize: 8) = 0
    2.807 ( 0.001 ms): :49553/49553 rt_sigaction(sig: CHLD, act: 0x7fff403ed6e0, oact: 0x7fff403ed780, sigsetsize: 8) = 0
    2.815 (18446744073709.438 ms): :49553/49553 execve(filename: 0xbb173a30, argv: 0x55aabb171930, envp: 0x55aabb171120) = 0
    2.815 ( 0.534 ms): pwd/49553  ... [continued]: execve())                                           = 0

Check for possible underflow first and in case of a bogus value, do not print it.

Quoted 7 lines
Quoted 1 line

Sashiko notes [1] that maybe this should be >= so that 0ns system calls still have a duration. I'm not sure how sensible that is.

OK, although there is nothing of zero duration, coarse clock resolution is a reasonable case. Sending v2.
Thanks.
Michael

Quoted 28 lines

Thanks,
Ian

[1] https://sashiko.dev/#/patchset/20260331101020.1270363-1-mpetlan%40redhat.com

Quoted 20 lines
---
 tools/perf/builtin-trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-trace.c +1 -2
index f487fbaa0ad6..c9de6bb9b0ea 100644
@@ -2956,7 +2956,7 @@ static int trace__sys_exit(struct trace *trace, struct evsel *evsel,
                ++trace->stats.vfs_getname;
        }
 
-       if (ttrace->entry_time) {
+       if (ttrace->entry_time && sample->time > ttrace->entry_time) {
                duration = sample->time - ttrace->entry_time;
                if (trace__filter_duration(trace, duration))
                        goto out;
--

2.47.3

#3 lore raw

PATCH v2 1/1 perf trace: Fix potential u64 underflow in duration calculation

In reply to Michael Petlan

Although it happens very rarely, in case of out-of-order events (i.e. due to CPU migration when a syscall is executed), the calculation of event duration might underflow and thus a bogus value is printed:

    2.804 ( 0.001 ms): :49553/49553 rt_sigaction(sig: QUIT, act: 0x7fff403ed6e0, oact: 0x7fff403ed780, sigsetsize: 8) = 0
    2.807 ( 0.001 ms): :49553/49553 rt_sigaction(sig: CHLD, act: 0x7fff403ed6e0, oact: 0x7fff403ed780, sigsetsize: 8) = 0
    2.815 (18446744073709.438 ms): :49553/49553 execve(filename: 0xbb173a30, argv: 0x55aabb171930, envp: 0x55aabb171120) = 0
    2.815 ( 0.534 ms): pwd/49553  ... [continued]: execve())                                           = 0

Check for possible underflow first and in case of a bogus value, do not print it.

v2: Use greater-or-equal to handle "zero" duration calls correctly.

---
 tools/perf/builtin-trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-trace.c +1 -1
index f487fbaa0ad6..9c59969de2af 100644
@@ -2956,7 +2956,7 @@ static int trace__sys_exit(struct trace *trace, struct evsel *evsel,
 		++trace->stats.vfs_getname;
 	}
 
-	if (ttrace->entry_time) {
+	if (ttrace->entry_time && sample->time >= ttrace->entry_time) {
 		duration = sample->time - ttrace->entry_time;
 		if (trace__filter_duration(trace, duration))
 			goto out;
Signature · 1 line
2.47.3
reviewer Reviewed-by on v2 1/1
#4 lore raw
Quoted 10 lines · Michael Petlan

Although it happens very rarely, in case of out-of-order events (i.e. due to CPU migration when a syscall is executed), the calculation of event duration might underflow and thus a bogus value is printed:

    2.804 ( 0.001 ms): :49553/49553 rt_sigaction(sig: QUIT, act: 0x7fff403ed6e0, oact: 0x7fff403ed780, sigsetsize: 8) = 0
    2.807 ( 0.001 ms): :49553/49553 rt_sigaction(sig: CHLD, act: 0x7fff403ed6e0, oact: 0x7fff403ed780, sigsetsize: 8) = 0
    2.815 (18446744073709.438 ms): :49553/49553 execve(filename: 0xbb173a30, argv: 0x55aabb171930, envp: 0x55aabb171120) = 0
    2.815 ( 0.534 ms): pwd/49553  ... [continued]: execve())                                           = 0
Quoted 4 lines

Check for possible underflow first and in case of a bogus value, do not print it.

v2: Use greater-or-equal to handle "zero" duration calls correctly.

Nit: This normally comes after the signed-off-by past a "---" to indicate it should not be included in the commit log.

Quoted 1 line

Thanks,
Ian

Quoted 20 lines
---
 tools/perf/builtin-trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-trace.c +1 -2
index f487fbaa0ad6..9c59969de2af 100644
@@ -2956,7 +2956,7 @@ static int trace__sys_exit(struct trace *trace, struct evsel *evsel,
                ++trace->stats.vfs_getname;
        }
 
-       if (ttrace->entry_time) {
+       if (ttrace->entry_time && sample->time >= ttrace->entry_time) {
                duration = sample->time - ttrace->entry_time;
                if (trace__filter_duration(trace, duration))
                        goto out;
--

2.47.3

maintainer
#5 lore raw
Quoted 6 lines · Ian Rogers
Quoted 5 lines · Michael Petlan

Although it happens very rarely, in case of out-of-order events (i.e. due to CPU migration when a syscall is executed), the calculation of event duration might underflow and thus a bogus value is printed:

Quoted 7 lines
Quoted 7 lines
    2.804 ( 0.001 ms): :49553/49553 rt_sigaction(sig: QUIT, act: 0x7fff403ed6e0, oact: 0x7fff403ed780, sigsetsize: 8) = 0
    2.807 ( 0.001 ms): :49553/49553 rt_sigaction(sig: CHLD, act: 0x7fff403ed6e0, oact: 0x7fff403ed780, sigsetsize: 8) = 0
    2.815 (18446744073709.438 ms): :49553/49553 execve(filename: 0xbb173a30, argv: 0x55aabb171930, envp: 0x55aabb171120) = 0
    2.815 ( 0.534 ms): pwd/49553  ... [continued]: execve())                                           = 0

Check for possible underflow first and in case of a bogus value, do not print it.

I guess this means to leave the duration empty and print the rest. For clear communication, I think it's better to put examples before and after for user-visible changes.

Quoted 5 lines
Quoted 2 lines

v2: Use greater-or-equal to handle "zero" duration calls correctly.

Nit: This normally comes after the signed-off-by past a "---" to indicate it should not be included in the commit log.

+1. I can handle that this time but please do later.

Quoted 4 lines
Quoted 2 lines

Thanks,
Namhyung

Quoted 21 lines
Quoted 21 lines
---
 tools/perf/builtin-trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-trace.c +1 -2
index f487fbaa0ad6..9c59969de2af 100644
@@ -2956,7 +2956,7 @@ static int trace__sys_exit(struct trace *trace, struct evsel *evsel,
                ++trace->stats.vfs_getname;
        }
 
-       if (ttrace->entry_time) {
+       if (ttrace->entry_time && sample->time >= ttrace->entry_time) {
                duration = sample->time - ttrace->entry_time;
                if (trace__filter_duration(trace, duration))
                        goto out;
--

2.47.3

maintainer Applied
#6 lore raw
In reply to Michael Petlan
Quoted 10 lines · Michael Petlan

Although it happens very rarely, in case of out-of-order events (i.e. due to CPU migration when a syscall is executed), the calculation of event duration might underflow and thus a bogus value is printed:

    2.804 ( 0.001 ms): :49553/49553 rt_sigaction(sig: QUIT, act: 0x7fff403ed6e0, oact: 0x7fff403ed780, sigsetsize: 8) = 0
    2.807 ( 0.001 ms): :49553/49553 rt_sigaction(sig: CHLD, act: 0x7fff403ed6e0, oact: 0x7fff403ed780, sigsetsize: 8) = 0
    2.815 (18446744073709.438 ms): :49553/49553 execve(filename: 0xbb173a30, argv: 0x55aabb171930, envp: 0x55aabb171120) = 0
    2.815 ( 0.534 ms): pwd/49553  ... [continued]: execve())                                           = 0

[...]

Applied to perf-tools-next, thanks!

Best regards,
Namhyung