Skip to contentSkip to conversation

posix-cpu-timers: Fix spuriously armed 0-value itimer

PATCH Applied Frederic Weisbecker 3 participants 3 messages Sep 13, 2021 – Sep 26, 2021 Posix clocks and timers

lore
Share & tools
lore

Frederic Weisbecker

author maintainer
#0 lore raw

PATCH 1/1 posix-cpu-timers: Fix spuriously armed 0-value itimer

Resetting/stopping an itimer eventually leads to it being reprogrammed with an actual "0" value. As a result the itimer expires on the next tick, triggering an unexpected signal.

To fix this, make sure that
struct signal_struct::it[CPUCLOCK_PROF/VIRT]::expires is set to 0 when setitimer() passes a 0 it_value, indicating that the timer must stop.

Fixes 406dd42bd1ba ("posix-cpu-timers: Force next expiration recalc after itimer reset")
Cc Peter Zijlstra (Intel) <[email protected]>
---
 kernel/time/posix-cpu-timers.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/time/posix-cpu-timers.c +2 -1
index ee736861b18f..643d412ac623 100644
@@ -1404,7 +1404,8 @@ void set_process_cpu_timer(struct task_struct *tsk, unsigned int clkid,
 			}
 		}
 
-		*newval += now;
+		if (*newval)
+			*newval += now;
 	}
 
 	/*
Signature · 1 line
2.25.1
#1 lore raw

[tip: timers/urgent] posix-cpu-timers: Prevent spuriously armed 0-value itimer

The following commit has been merged into the timers/urgent branch of tip:

The following commit has been merged into the timers/urgent branch of tip:

Commit-ID:     8cd9da85d2bd87ce889043e7b1735723dd10eb89
Gitweb:        https://git.kernel.org/tip/8cd9da85d2bd87ce889043e7b1735723dd10eb89
Author:        Frederic Weisbecker <[email protected]>
AuthorDate:    Mon, 13 Sep 2021 16:53:32 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Thu, 23 Sep 2021 11:53:51 +02:00

posix-cpu-timers: Prevent spuriously armed 0-value itimer

Resetting/stopping an itimer eventually leads to it being reprogrammed with an actual "0" value. As a result the itimer expires on the next tick, triggering an unexpected signal.

To fix this, make sure that
struct signal_struct::it[CPUCLOCK_PROF/VIRT]::expires is set to 0 when setitimer() passes a 0 it_value, indicating that the timer must stop.

Fixes 406dd42bd1ba ("posix-cpu-timers: Force next expiration recalc after itimer reset")
---
 kernel/time/posix-cpu-timers.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/time/posix-cpu-timers.c +2 -1
index ee73686..643d412 100644
@@ -1404,7 +1404,8 @@ void set_process_cpu_timer(struct task_struct *tsk, unsigned int clkid,
 			}
 		}
 
-		*newval += now;
+		if (*newval)
+			*newval += now;
 	}
 
 	/*
#2 lore raw
In reply to Frederic Weisbecker

Hello,

Ccing libc-alpha,

Quoted 3 lines · Frederic Weisbecker

Resetting/stopping an itimer eventually leads to it being reprogrammed with an actual "0" value. As a result the itimer expires on the next tick, triggering an unexpected signal.

Thanks for this I just tracked down the same issue.

I just wanted to give an FYI in case anyone else notices this.

This was causing gprof (gcc -pg) to fail. I noticed this when running the glibc test suite, tests: gmon/tst-gmon-static, gmon/tst-gmon

Gprof uses ITIMER_PROF, as per:

- https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/posix/profil.c;h=fbf1184df1ccc6e1d2098f92c7d87fc28f5a8f09;hb=HEAD

I was seeing the extra SIGPROF signal after profil de-registered the timer and signal handler. The spurious signal causes all profiled programs to be killed and collect no gprof data.

-Stafford

Quoted 29 lines

To fix this, make sure that
struct signal_struct::it[CPUCLOCK_PROF/VIRT]::expires is set to 0 when setitimer() passes a 0 it_value, indicating that the timer must stop.

Fixes 406dd42bd1ba ("posix-cpu-timers: Force next expiration recalc after itimer reset")
Cc Peter Zijlstra (Intel) <[email protected]>
---
 kernel/time/posix-cpu-timers.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/time/posix-cpu-timers.c +2 -1
index ee736861b18f..643d412ac623 100644
@@ -1404,7 +1404,8 @@ void set_process_cpu_timer(struct task_struct *tsk, unsigned int clkid,
 			}
 		}
 
-		*newval += now;
+		if (*newval)
+			*newval += now;
 	}
 
 	/*
Signature · 1 line
2.25.1