Skip to contentSkip to conversation

posix-cpu-timers: Unbreak interval timers

PATCH In review Thomas Gleixner 3 participants 6 messages Jan 11, 2019 – Jan 15, 2019 Posix clocks and timers

lore
Share & tools
lore
#0 lore raw

cover posix-cpu-timers: Unbreak interval timers

The recent commit which prevented a division by 0 issue in the alarm timer code broke posix CPU timers as an unwanted side effect.

The following series unbreaks them and removes the duplicated storage of the interval which is not longer required.

Thanks,

tglx

#1 lore raw

PATCH 1/2 posix-cpu-timers: Unbreak timer rearming

The recent commit which prevented a division by 0 issue in the alarm timer code broke posix CPU timers as an unwanted side effect.

The reason is that the common rearm code checks for timer->it_interval being 0 now. What went unnoticed is that the posix cpu timer setup does not initialize timer->it_interval as it stores the interval in CPU timer specific storage. The reason for the separate storage is historical as the posix CPU timers always had a 64bit nanoseconds representation internally while timer->it_interval is type ktime_t which used to be a modified timespec representation on 32bit machines.

Instead of reverting the offending commit and fixing the alarmtimer issue in the alarmtimer code, store the interval in timer->it_interval at CPU timer setup time so the common code check works. This also repairs the existing inconistency of the posix CPU timer code which kept a single shot timer armed despite of the interval being 0.

The separate storage can be removed in mainline, but that needs to be a separate commit as the current one has to be backported to stable kernels.

Fixes 0e334db6bb4b ("posix-timers: Fix division by zero bug")
Cc Peter Zijlstra <[email protected]>
Cc John Stultz <[email protected]>
---
 kernel/time/posix-cpu-timers.c |    1 +
 1 file changed, 1 insertion(+)
kernel/time/posix-cpu-timers.c +1 -0
@@ -685,6 +685,7 @@ static int posix_cpu_timer_set(struct k_
 	 * set up the signal and overrun bookkeeping.
 	 */
 	timer->it.cpu.incr = timespec64_to_ns(&new->it_interval);
+	timer->it_interval = ns_to_ktime(timer->it.cpu.incr);
 
 	/*
 	 * This acts as a modification timestamp for the timer,
#2 lore raw

Re: [patch 1/2] posix-cpu-timers: Unbreak timer rearming

Quoted 36 lines · Thomas Gleixner

The recent commit which prevented a division by 0 issue in the alarm timer code broke posix CPU timers as an unwanted side effect.

The reason is that the common rearm code checks for timer->it_interval being 0 now. What went unnoticed is that the posix cpu timer setup does not initialize timer->it_interval as it stores the interval in CPU timer specific storage. The reason for the separate storage is historical as the posix CPU timers always had a 64bit nanoseconds representation internally while timer->it_interval is type ktime_t which used to be a modified timespec representation on 32bit machines.

Instead of reverting the offending commit and fixing the alarmtimer issue in the alarmtimer code, store the interval in timer->it_interval at CPU timer setup time so the common code check works. This also repairs the existing inconistency of the posix CPU timer code which kept a single shot timer armed despite of the interval being 0.

The separate storage can be removed in mainline, but that needs to be a separate commit as the current one has to be backported to stable kernels.

Fixes 0e334db6bb4b ("posix-timers: Fix division by zero bug")
Cc Peter Zijlstra <[email protected]>
Cc John Stultz <[email protected]>
---
 kernel/time/posix-cpu-timers.c |    1 +
 1 file changed, 1 insertion(+)

kernel/time/posix-cpu-timers.c static int posix_cpu_timer_set(struct k_ · line 691

Quoted 7 lines
kernel/time/posix-cpu-timers.c +1 -0
@@ -685,6 +685,7 @@ static int posix_cpu_timer_set(struct k_
         * set up the signal and overrun bookkeeping.
         */
        timer->it.cpu.incr = timespec64_to_ns(&new->it_interval);
+       timer->it_interval = ns_to_ktime(timer->it.cpu.incr);
 
        /*
         * This acts as a modification timestamp for the timer,

I verified that this patch works on 4.19.14 kernel.

Thanks.

Signature · 1 line
H.J.
#3 lore raw

[tip:timers/urgent] posix-cpu-timers: Unbreak timer rearming

In reply to Thomas Gleixner
posix-cpu-timers: Unbreak timer rearming
Commit-ID:  93ad0fc088c5b4631f796c995bdd27a082ef33a6
Gitweb:     https://git.kernel.org/tip/93ad0fc088c5b4631f796c995bdd27a082ef33a6
Author:     Thomas Gleixner <[email protected]>
AuthorDate: Fri, 11 Jan 2019 14:33:16 +0100
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Tue, 15 Jan 2019 16:34:37 +0100

posix-cpu-timers: Unbreak timer rearming

The recent commit which prevented a division by 0 issue in the alarm timer code broke posix CPU timers as an unwanted side effect.

The reason is that the common rearm code checks for timer->it_interval being 0 now. What went unnoticed is that the posix cpu timer setup does not initialize timer->it_interval as it stores the interval in CPU timer specific storage. The reason for the separate storage is historical as the posix CPU timers always had a 64bit nanoseconds representation internally while timer->it_interval is type ktime_t which used to be a modified timespec representation on 32bit machines.

Instead of reverting the offending commit and fixing the alarmtimer issue in the alarmtimer code, store the interval in timer->it_interval at CPU timer setup time so the common code check works. This also repairs the existing inconistency of the posix CPU timer code which kept a single shot timer armed despite of the interval being 0.

The separate storage can be removed in mainline, but that needs to be a separate commit as the current one has to be backported to stable kernels.

Fixes 0e334db6bb4b ("posix-timers: Fix division by zero bug")
Cc John Stultz <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 kernel/time/posix-cpu-timers.c | 1 +
 1 file changed, 1 insertion(+)
kernel/time/posix-cpu-timers.c +1 -0
index 8f0644af40be..80f955210861 100644
@@ -685,6 +685,7 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
 	 * set up the signal and overrun bookkeeping.
 	 */
 	timer->it.cpu.incr = timespec64_to_ns(&new->it_interval);
+	timer->it_interval = ns_to_ktime(timer->it.cpu.incr);
 
 	/*
 	 * This acts as a modification timestamp for the timer,
#4 lore raw

PATCH 2/2 posix-cpu-timers: Remove private interval storage

In reply to Thomas Gleixner

Posix CPU timers store the interval in private storage for historical reasons (it_interval used to be a non scalar representation on 32bit systems). This is gone and there is no reason for duplicated storage anymore.

Use it_interval everywhere.

---
 include/linux/posix-timers.h   |    2 +-
 kernel/time/posix-cpu-timers.c |   13 ++++++-------
 2 files changed, 7 insertions(+), 8 deletions(-)
include/linux/posix-timers.h +1 -1
@@ -12,7 +12,7 @@ struct siginfo;
 
 struct cpu_timer_list {
 	struct list_head entry;
-	u64 expires, incr;
+	u64 expires;
 	struct task_struct *task;
 	int firing;
 };
kernel/time/posix-cpu-timers.c +6 -7
@@ -67,13 +67,13 @@ static void bump_cpu_timer(struct k_itim
 	int i;
 	u64 delta, incr;
 
-	if (timer->it.cpu.incr == 0)
+	if (!timer->it_interval)
 		return;
 
 	if (now < timer->it.cpu.expires)
 		return;
 
-	incr = timer->it.cpu.incr;
+	incr = timer->it_interval;
 	delta = now + incr - timer->it.cpu.expires;
 
 	/* Don't use (incr*2 < delta), incr*2 might overflow. */
@@ -520,7 +520,7 @@ static void cpu_timer_fire(struct k_itim
 		 */
 		wake_up_process(timer->it_process);
 		timer->it.cpu.expires = 0;
-	} else if (timer->it.cpu.incr == 0) {
+	} else if (!timer->it_interval) {
 		/*
 		 * One-shot timer.  Clear it as soon as it's fired.
 		 */
@@ -606,7 +606,7 @@ static int posix_cpu_timer_set(struct k_
 	 */
 
 	ret = 0;
-	old_incr = timer->it.cpu.incr;
+	old_incr = timer->it_interval;
 	old_expires = timer->it.cpu.expires;
 	if (unlikely(timer->it.cpu.firing)) {
 		timer->it.cpu.firing = -1;
@@ -684,8 +684,7 @@ static int posix_cpu_timer_set(struct k_
 	 * Install the new reload setting, and
 	 * set up the signal and overrun bookkeeping.
 	 */
-	timer->it.cpu.incr = timespec64_to_ns(&new->it_interval);
-	timer->it_interval = ns_to_ktime(timer->it.cpu.incr);
+	timer->it_interval = timespec64_to_ktime(new->it_interval);
 
 	/*
 	 * This acts as a modification timestamp for the timer,
@@ -724,7 +723,7 @@ static void posix_cpu_timer_get(struct k
 	/*
 	 * Easy part: convert the reload time.
 	 */
-	itp->it_interval = ns_to_timespec64(timer->it.cpu.incr);
+	itp->it_interval = ktime_to_timespec64(timer->it_interval);
 
 	if (!timer->it.cpu.expires)
 		return;
#5 lore raw

[tip:timers/core] posix-cpu-timers: Remove private interval storage

posix-cpu-timers: Remove private interval storage
Commit-ID:  16118794ede91aac1a73abe15de22d3de9d2b775
Gitweb:     https://git.kernel.org/tip/16118794ede91aac1a73abe15de22d3de9d2b775
Author:     Thomas Gleixner <[email protected]>
AuthorDate: Fri, 11 Jan 2019 14:33:17 +0100
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Tue, 15 Jan 2019 16:36:13 +0100

posix-cpu-timers: Remove private interval storage

Posix CPU timers store the interval in private storage for historical reasons (it_interval used to be a non scalar representation on 32bit systems). This is gone and there is no reason for duplicated storage anymore.

Use it_interval everywhere.

---
 include/linux/posix-timers.h   |  2 +-
 kernel/time/posix-cpu-timers.c | 13 ++++++-------
 2 files changed, 7 insertions(+), 8 deletions(-)
include/linux/posix-timers.h +1 -1
index e96581ca7c9d..b20798fc5191 100644
@@ -12,7 +12,7 @@ struct siginfo;
 
 struct cpu_timer_list {
 	struct list_head entry;
-	u64 expires, incr;
+	u64 expires;
 	struct task_struct *task;
 	int firing;
 };
kernel/time/posix-cpu-timers.c +6 -7
index 80f955210861..0a426f4e3125 100644
@@ -67,13 +67,13 @@ static void bump_cpu_timer(struct k_itimer *timer, u64 now)
 	int i;
 	u64 delta, incr;
 
-	if (timer->it.cpu.incr == 0)
+	if (!timer->it_interval)
 		return;
 
 	if (now < timer->it.cpu.expires)
 		return;
 
-	incr = timer->it.cpu.incr;
+	incr = timer->it_interval;
 	delta = now + incr - timer->it.cpu.expires;
 
 	/* Don't use (incr*2 < delta), incr*2 might overflow. */
@@ -520,7 +520,7 @@ static void cpu_timer_fire(struct k_itimer *timer)
 		 */
 		wake_up_process(timer->it_process);
 		timer->it.cpu.expires = 0;
-	} else if (timer->it.cpu.incr == 0) {
+	} else if (!timer->it_interval) {
 		/*
 		 * One-shot timer.  Clear it as soon as it's fired.
 		 */
@@ -606,7 +606,7 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
 	 */
 
 	ret = 0;
-	old_incr = timer->it.cpu.incr;
+	old_incr = timer->it_interval;
 	old_expires = timer->it.cpu.expires;
 	if (unlikely(timer->it.cpu.firing)) {
 		timer->it.cpu.firing = -1;
@@ -684,8 +684,7 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
 	 * Install the new reload setting, and
 	 * set up the signal and overrun bookkeeping.
 	 */
-	timer->it.cpu.incr = timespec64_to_ns(&new->it_interval);
-	timer->it_interval = ns_to_ktime(timer->it.cpu.incr);
+	timer->it_interval = timespec64_to_ktime(new->it_interval);
 
 	/*
 	 * This acts as a modification timestamp for the timer,
@@ -724,7 +723,7 @@ static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp
 	/*
 	 * Easy part: convert the reload time.
 	 */
-	itp->it_interval = ns_to_timespec64(timer->it.cpu.incr);
+	itp->it_interval = ktime_to_timespec64(timer->it_interval);
 
 	if (!timer->it.cpu.expires)
 		return;