Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1653571 > unrolled thread

[patch 00/26] alarmtimers/posixtimers: Bug fixes and spec conformity changes

Started byThomas Gleixner <tglx@linutronix.de>
First post2017-05-30 23:50 +0200
Last post2017-06-05 10:30 +0200
Articles 20 on this page of 57 — 3 participants

Back to article view | Back to linux.kernel


Contents

  [patch 00/26] alarmtimers/posixtimers: Bug fixes and spec conformity  changes Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
    [patch 09/26] posix-timers: Unify overrun/requeue_pending handling Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] posix-timers: Unify overrun/requeue_pending  handling tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200
    [patch 19/26] posix-timers: Add cancel/arm callbacks Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] posix-timers: Add cancel/arm callbacks tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200
    [patch 11/26] posix-timers: Store k_clock pointer in k_itimer Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] posix-timers: Store k_clock pointer in k_itimer tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200
    [patch 24/26] alarmtimer: Implement try_to_cancel callback Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] alarmtimer: Implement try_to_cancel callback tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200
    [patch 08/26] posix-timers: Move posix-timer internals to core Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      Re: [patch 08/26] posix-timers: Move posix-timer internals to core Christoph Hellwig <hch@infradead.org> - 2017-05-31 17:40 +0200
      [tip:timers/core] posix-timers: Move posix-timer internals to core tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:20 +0200
    [patch 17/26] posix-timers: Make use of forward/remaining callbacks Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] posix-timers: Make use of forward/remaining  callbacks tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200
    [patch 07/26] posix-timers: Cleanup struct k_itimer Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] posix-timers: Cleanup struct k_itimer tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:20 +0200
    [patch 02/26] alarmtimer: Rate limit periodic intervals Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/urgent] alarmtimer: Rate limit periodic intervals tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-04 15:30 +0200
      [tip:timers/urgent] alarmtimer: Rate limit periodic intervals tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-04 15:40 +0200
    [patch 14/26] posix-timers: Use timer_rearm() callback in  posixtimer_rearm() Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] posix-timers: Use timer_rearm() callback in  posixtimer_rearm() tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200
    [patch 04/26] posix-timers: Remove unused export of  posix_timer_event() Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] posix-timers: Remove unused export of  posix_timer_event() tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:20 +0200
    [patch 23/26] alarmtimer: Implement remaining callback Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] alarmtimer: Implement remaining callback tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200
    [patch 13/26] posix-timers: Rename do_schedule_next_timer Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      Re: [patch 13/26] posix-timers: Rename do_schedule_next_timer Christoph Hellwig <hch@infradead.org> - 2017-05-31 17:40 +0200
        Re: [patch 13/26] posix-timers: Rename do_schedule_next_timer Thomas Gleixner <tglx@linutronix.de> - 2017-06-01 23:00 +0200
          Re: [patch 13/26] posix-timers: Rename do_schedule_next_timer Christoph Hellwig <hch@infradead.org> - 2017-06-02 09:10 +0200
      [tip:timers/core] posix-timers: Rename do_schedule_next_timer tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200
    [patch 22/26] alarmtimer: Implement forward callback Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] alarmtimer: Implement forward callback tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200
    [patch 03/26] alarmtimer: Remove pointless config conditional Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] alarmtimer: Remove pointless config conditional tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:20 +0200
    [patch 26/26] alarmtimer: Switch over to generic set/get/rearm routine Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] alarmtimer: Switch over to generic set/get/rearm  routine tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200
    [patch 06/26] posix-timers: Avoid gazillions of forward declarations Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] posix-timers: Avoid gazillions of forward  declarations tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:20 +0200
    [patch 12/26] posix-timers: Add timer_rearm() callback Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] posix-timers: Add timer_rearm() callback tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200
    [patch 01/26] alarmtimer: Prevent overflow of relative timers Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/urgent] alarmtimer: Prevent overflow of relative timers tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-04 15:30 +0200
      [tip:timers/urgent] alarmtimer: Prevent overflow of relative timers tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-04 15:40 +0200
    [patch 15/26] posix-timers: Add active flag to k_itimer Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] posix-timers: Add active flag to k_itimer tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200
    [patch 16/26] posix-timers: Add forward/remaining callbacks Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] posix-timers: Add forward/remaining callbacks tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200
    [patch 25/26] alarmtimer: Implement arm callback Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] alarmtimer: Implement arm callback tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200
    [patch 21/26] alarmtimer: Implement timer_rearm() callback Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] alarmtimer: Implement timer_rearm() callback tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200
    [patch 10/26] posix-timers: Move interval out of the union Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] posix-timers: Move interval out of the union tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200
    [patch 20/26] posix-timers: Make use of cancel/arm callbacks Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] posix-timers: Make use of cancel/arm callbacks tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200
    [patch 18/26] posix-timers: Zero settings value in common code Thomas Gleixner <tglx@linutronix.de> - 2017-05-30 23:50 +0200
      [tip:timers/core] posix-timers: Zero settings value in common code tip-bot for Thomas Gleixner <tipbot@zytor.com> - 2017-06-05 10:30 +0200

Page 1 of 3  [1] 2 3  Next page →


#1653571 — [patch 00/26] alarmtimers/posixtimers: Bug fixes and spec conformity changes

FromThomas Gleixner <tglx@linutronix.de>
Date2017-05-30 23:50 +0200
Subject[patch 00/26] alarmtimers/posixtimers: Bug fixes and spec conformity changes
Message-ID<tMXbz-4e6-5@gated-at.bofh.it>
The syzkaller folks reported an RCU stall while fuzzing the posix timer
interface with CLOCK_ALARMTIMER_*.

While fixing this issue, I noticed that the posix timer implementation of
alarm timers lacks quite some of the features of the regular hrtimer based
posix timers.

That makes alarm timer bases posix timer not conform to the spec and also
the unconditional self rearming of those timer is suboptimal.

Instead of copying the posix timer hrtimer based mechanism over to alarm
timers with the risk of getting out of sync sooner than later, I decided to
add a few callbacks to the kclock struct which describes the different
posix timer clocks, so the existing and proven to be correct implementation
can be reused and we have only one implementation to maintain, aside of the
special magic which handles the CPU/PROCESS/THREAD clock ids.

Patch 1/2 are the bugfixes addressing the reported problem (#1) and a
similar one (#2) which I detected in review.

Patch 3-8 are cleanups which I put in front of the actual rework, including
the removal of unused and prone to be implemnted wrong posix timer
callbacks for the dynamic posix clocks.

Patch 9-20 prepare the code in posix-timer.c for sharing and reuse and the
remaining patches 21-26 switch the alarm timers over.

Passes LTP and GLIBC tests and a few other test cases I have around.

Thanks,

	tglx
---
 b/kernel/time/posix-timers.h   |   13 +
 include/asm-generic/siginfo.h  |    2 
 include/linux/posix-clock.h    |   22 --
 include/linux/posix-timers.h   |  117 ++++++-----
 kernel/signal.c                |    2 
 kernel/time/alarmtimer.c       |  200 +++++++++-----------
 kernel/time/posix-clock.c      |  115 -----------
 kernel/time/posix-cpu-timers.c |   36 +--
 kernel/time/posix-timers.c     |  410 ++++++++++++++++++++++-------------------
 9 files changed, 429 insertions(+), 488 deletions(-)

[toc] | [next] | [standalone]


#1653572 — [patch 09/26] posix-timers: Unify overrun/requeue_pending handling

FromThomas Gleixner <tglx@linutronix.de>
Date2017-05-30 23:50 +0200
Subject[patch 09/26] posix-timers: Unify overrun/requeue_pending handling
Message-ID<tMXbz-4e6-7@gated-at.bofh.it>
In reply to#1653571
hrtimer based posix-timers and posix-cpu-timers handle the update of the
rearming and overflow related status fields differently.

Move that update to the common rearming code.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 kernel/time/posix-cpu-timers.c |   18 +++++++-----------
 kernel/time/posix-timers.c     |   15 ++++++++-------
 2 files changed, 15 insertions(+), 18 deletions(-)

--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -527,6 +527,7 @@ static void cpu_timer_fire(struct k_itim
 		 * ticking in case the signal is deliverable next time.
 		 */
 		posix_cpu_timer_schedule(timer);
+		++timer->it_requeue_pending;
 	}
 }
 
@@ -989,12 +990,12 @@ void posix_cpu_timer_schedule(struct k_i
 		cpu_clock_sample(timer->it_clock, p, &now);
 		bump_cpu_timer(timer, now);
 		if (unlikely(p->exit_state))
-			goto out;
+			return;
 
 		/* Protect timer list r/w in arm_timer() */
 		sighand = lock_task_sighand(p, &flags);
 		if (!sighand)
-			goto out;
+			return;
 	} else {
 		/*
 		 * Protect arm_timer() and timer sampling in case of call to
@@ -1007,11 +1008,10 @@ void posix_cpu_timer_schedule(struct k_i
 			 * We can't even collect a sample any more.
 			 */
 			timer->it.cpu.expires = 0;
-			goto out;
+			return;
 		} else if (unlikely(p->exit_state) && thread_group_empty(p)) {
-			unlock_task_sighand(p, &flags);
-			/* Optimizations: if the process is dying, no need to rearm */
-			goto out;
+			/* If the process is dying, no need to rearm */
+			goto unlock;
 		}
 		cpu_timer_sample_group(timer->it_clock, p, &now);
 		bump_cpu_timer(timer, now);
@@ -1023,12 +1023,8 @@ void posix_cpu_timer_schedule(struct k_i
 	 */
 	WARN_ON_ONCE(!irqs_disabled());
 	arm_timer(timer);
+unlock:
 	unlock_task_sighand(p, &flags);
-
-out:
-	timer->it_overrun_last = timer->it_overrun;
-	timer->it_overrun = -1;
-	++timer->it_requeue_pending;
 }
 
 /**
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -291,10 +291,6 @@ static void schedule_next_timer(struct k
 	timr->it_overrun += (unsigned int) hrtimer_forward(timer,
 						timer->base->get_time(),
 						timr->it.real.interval);
-
-	timr->it_overrun_last = timr->it_overrun;
-	timr->it_overrun = -1;
-	++timr->it_requeue_pending;
 	hrtimer_restart(timer);
 }
 
@@ -315,18 +311,23 @@ void do_schedule_next_timer(struct sigin
 	unsigned long flags;
 
 	timr = lock_timer(info->si_tid, &flags);
+	if (!timr)
+		return;
 
-	if (timr && timr->it_requeue_pending == info->si_sys_private) {
+	if (timr->it_requeue_pending == info->si_sys_private) {
 		if (timr->it_clock < 0)
 			posix_cpu_timer_schedule(timr);
 		else
 			schedule_next_timer(timr);
 
+		timr->it_overrun_last = timr->it_overrun;
+		timr->it_overrun = -1;
+		++timr->it_requeue_pending;
+
 		info->si_overrun += timr->it_overrun_last;
 	}
 
-	if (timr)
-		unlock_timer(timr, flags);
+	unlock_timer(timr, flags);
 }
 
 int posix_timer_event(struct k_itimer *timr, int si_private)

[toc] | [prev] | [next] | [standalone]


#1657369 — [tip:timers/core] posix-timers: Unify overrun/requeue_pending handling

Fromtip-bot for Thomas Gleixner <tipbot@zytor.com>
Date2017-06-05 10:30 +0200
Subject[tip:timers/core] posix-timers: Unify overrun/requeue_pending handling
Message-ID<tOVyG-rf-17@gated-at.bofh.it>
In reply to#1653572
Commit-ID:  af888d677a3f4473c198b4720319dd037f398b51
Gitweb:     http://git.kernel.org/tip/af888d677a3f4473c198b4720319dd037f398b51
Author:     Thomas Gleixner <tglx@linutronix.de>
AuthorDate: Tue, 30 May 2017 23:15:42 +0200
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Sun, 4 Jun 2017 15:40:24 +0200

posix-timers: Unify overrun/requeue_pending handling

hrtimer based posix-timers and posix-cpu-timers handle the update of the
rearming and overflow related status fields differently.

Move that update to the common rearming code.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: John Stultz <john.stultz@linaro.org>
Link: http://lkml.kernel.org/r/20170530211656.484936964@linutronix.de

---
 kernel/time/posix-cpu-timers.c | 18 +++++++-----------
 kernel/time/posix-timers.c     | 15 ++++++++-------
 2 files changed, 15 insertions(+), 18 deletions(-)

diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c
index a77a792..1683e50 100644
--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -527,6 +527,7 @@ static void cpu_timer_fire(struct k_itimer *timer)
 		 * ticking in case the signal is deliverable next time.
 		 */
 		posix_cpu_timer_schedule(timer);
+		++timer->it_requeue_pending;
 	}
 }
 
@@ -997,12 +998,12 @@ void posix_cpu_timer_schedule(struct k_itimer *timer)
 		cpu_clock_sample(timer->it_clock, p, &now);
 		bump_cpu_timer(timer, now);
 		if (unlikely(p->exit_state))
-			goto out;
+			return;
 
 		/* Protect timer list r/w in arm_timer() */
 		sighand = lock_task_sighand(p, &flags);
 		if (!sighand)
-			goto out;
+			return;
 	} else {
 		/*
 		 * Protect arm_timer() and timer sampling in case of call to
@@ -1015,11 +1016,10 @@ void posix_cpu_timer_schedule(struct k_itimer *timer)
 			 * We can't even collect a sample any more.
 			 */
 			timer->it.cpu.expires = 0;
-			goto out;
+			return;
 		} else if (unlikely(p->exit_state) && thread_group_empty(p)) {
-			unlock_task_sighand(p, &flags);
-			/* Optimizations: if the process is dying, no need to rearm */
-			goto out;
+			/* If the process is dying, no need to rearm */
+			goto unlock;
 		}
 		cpu_timer_sample_group(timer->it_clock, p, &now);
 		bump_cpu_timer(timer, now);
@@ -1031,12 +1031,8 @@ void posix_cpu_timer_schedule(struct k_itimer *timer)
 	 */
 	WARN_ON_ONCE(!irqs_disabled());
 	arm_timer(timer);
+unlock:
 	unlock_task_sighand(p, &flags);
-
-out:
-	timer->it_overrun_last = timer->it_overrun;
-	timer->it_overrun = -1;
-	++timer->it_requeue_pending;
 }
 
 /**
diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
index dee6a0d..79a00e0 100644
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -291,10 +291,6 @@ static void schedule_next_timer(struct k_itimer *timr)
 	timr->it_overrun += (unsigned int) hrtimer_forward(timer,
 						timer->base->get_time(),
 						timr->it.real.interval);
-
-	timr->it_overrun_last = timr->it_overrun;
-	timr->it_overrun = -1;
-	++timr->it_requeue_pending;
 	hrtimer_restart(timer);
 }
 
@@ -315,18 +311,23 @@ void do_schedule_next_timer(struct siginfo *info)
 	unsigned long flags;
 
 	timr = lock_timer(info->si_tid, &flags);
+	if (!timr)
+		return;
 
-	if (timr && timr->it_requeue_pending == info->si_sys_private) {
+	if (timr->it_requeue_pending == info->si_sys_private) {
 		if (timr->it_clock < 0)
 			posix_cpu_timer_schedule(timr);
 		else
 			schedule_next_timer(timr);
 
+		timr->it_overrun_last = timr->it_overrun;
+		timr->it_overrun = -1;
+		++timr->it_requeue_pending;
+
 		info->si_overrun += timr->it_overrun_last;
 	}
 
-	if (timr)
-		unlock_timer(timr, flags);
+	unlock_timer(timr, flags);
 }
 
 int posix_timer_event(struct k_itimer *timr, int si_private)

[toc] | [prev] | [next] | [standalone]


#1653574 — [patch 19/26] posix-timers: Add cancel/arm callbacks

FromThomas Gleixner <tglx@linutronix.de>
Date2017-05-30 23:50 +0200
Subject[patch 19/26] posix-timers: Add cancel/arm callbacks
Message-ID<tMXbz-4e6-15@gated-at.bofh.it>
In reply to#1653571
Add timer_try_to_cancel() and timer_arm() callbacks to kclock which allow
to make common_timer_set() usable by both hrtimer and alarmtimer based
clocks.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 include/linux/posix-timers.h |    3 +++
 1 file changed, 3 insertions(+)

Index: b/include/linux/posix-timers.h
===================================================================
--- a/include/linux/posix-timers.h
+++ b/include/linux/posix-timers.h
@@ -126,6 +126,9 @@ struct k_clock {
 	void	(*timer_rearm)(struct k_itimer *timr);
 	int	(*timer_forward)(struct k_itimer *timr, ktime_t now);
 	ktime_t	(*timer_remaining)(struct k_itimer *timr, ktime_t now);
+	int	(*timer_try_to_cancel)(struct k_itimer *timr);
+	void	(*timer_arm)(struct k_itimer *timr, ktime_t expires,
+			     bool absolute, bool sigev_none);
 };
 
 void run_posix_cpu_timers(struct task_struct *task);

[toc] | [prev] | [next] | [standalone]


#1657387 — [tip:timers/core] posix-timers: Add cancel/arm callbacks

Fromtip-bot for Thomas Gleixner <tipbot@zytor.com>
Date2017-06-05 10:30 +0200
Subject[tip:timers/core] posix-timers: Add cancel/arm callbacks
Message-ID<tOVyI-rf-57@gated-at.bofh.it>
In reply to#1653574
Commit-ID:  525b8ed91671e29e187dfe02d408b11190ccf494
Gitweb:     http://git.kernel.org/tip/525b8ed91671e29e187dfe02d408b11190ccf494
Author:     Thomas Gleixner <tglx@linutronix.de>
AuthorDate: Tue, 30 May 2017 23:15:52 +0200
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Sun, 4 Jun 2017 15:40:28 +0200

posix-timers: Add cancel/arm callbacks

Add timer_try_to_cancel() and timer_arm() callbacks to kclock which allow
to make common_timer_set() usable by both hrtimer and alarmtimer based
clocks.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: John Stultz <john.stultz@linaro.org>
Link: http://lkml.kernel.org/r/20170530211657.278022962@linutronix.de

---
 kernel/time/posix-timers.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/kernel/time/posix-timers.h b/kernel/time/posix-timers.h
index 3bc5b74..b0ad77e 100644
--- a/kernel/time/posix-timers.h
+++ b/kernel/time/posix-timers.h
@@ -21,6 +21,9 @@ struct k_clock {
 	void	(*timer_rearm)(struct k_itimer *timr);
 	int	(*timer_forward)(struct k_itimer *timr, ktime_t now);
 	ktime_t	(*timer_remaining)(struct k_itimer *timr, ktime_t now);
+	int	(*timer_try_to_cancel)(struct k_itimer *timr);
+	void	(*timer_arm)(struct k_itimer *timr, ktime_t expires,
+			     bool absolute, bool sigev_none);
 };
 
 extern const struct k_clock clock_posix_cpu;

[toc] | [prev] | [next] | [standalone]


#1653575 — [patch 11/26] posix-timers: Store k_clock pointer in k_itimer

FromThomas Gleixner <tglx@linutronix.de>
Date2017-05-30 23:50 +0200
Subject[patch 11/26] posix-timers: Store k_clock pointer in k_itimer
Message-ID<tMXbz-4e6-13@gated-at.bofh.it>
In reply to#1653571
Having the k_clock pointer in the k_itimer struct avoids the lookup in
several code pathes and makes the next steps of unification of the hrtimer
and alarmtimer based posix timers simpler.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 include/linux/posix-timers.h   |    2 ++
 kernel/time/posix-cpu-timers.c |    2 ++
 kernel/time/posix-timers.c     |    7 ++++---
 3 files changed, 8 insertions(+), 3 deletions(-)

--- a/include/linux/posix-timers.h
+++ b/include/linux/posix-timers.h
@@ -55,6 +55,7 @@ struct cpu_timer_list {
  * @list:		List head for binding the timer to signals->posix_timers
  * @t_hash:		Entry in the posix timer hash table
  * @it_lock:		Lock protecting the timer
+ * @kclock:		Pointer to the k_clock struct handling this timer
  * @it_clock:		The posix timer clock id
  * @it_id:		The posix timer id for identifying the timer
  * @it_overrun:		The overrun counter for pending signals
@@ -74,6 +75,7 @@ struct k_itimer {
 	struct list_head	list;
 	struct hlist_node	t_hash;
 	spinlock_t		it_lock;
+	const struct k_clock	*kclock;
 	clockid_t		it_clock;
 	timer_t			it_id;
 	int			it_overrun;
--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -324,6 +324,8 @@ static int posix_cpu_timer_create(struct
 	if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
 		return -EINVAL;
 
+	new_timer->kclock = &clock_posix_cpu;
+
 	INIT_LIST_HEAD(&new_timer->it.cpu.entry);
 
 	rcu_read_lock();
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -519,6 +519,7 @@ SYSCALL_DEFINE3(timer_create, const cloc
 	it_id_set = IT_ID_SET;
 	new_timer->it_id = (timer_t) new_timer_id;
 	new_timer->it_clock = which_clock;
+	new_timer->kclock = kc;
 	new_timer->it_overrun = -1;
 
 	if (timer_event_spec) {
@@ -679,7 +680,7 @@ SYSCALL_DEFINE2(timer_gettime, timer_t,
 	if (!timr)
 		return -EINVAL;
 
-	kc = clockid_to_kclock(timr->it_clock);
+	kc = timr->kclock;
 	if (WARN_ON_ONCE(!kc || !kc->timer_get))
 		ret = -EINVAL;
 	else
@@ -798,7 +799,7 @@ SYSCALL_DEFINE4(timer_settime, timer_t,
 	if (!timr)
 		return -EINVAL;
 
-	kc = clockid_to_kclock(timr->it_clock);
+	kc = timr->kclock;
 	if (WARN_ON_ONCE(!kc || !kc->timer_set))
 		error = -EINVAL;
 	else
@@ -829,7 +830,7 @@ static int common_timer_del(struct k_iti
 
 static inline int timer_delete_hook(struct k_itimer *timer)
 {
-	const struct k_clock *kc = clockid_to_kclock(timer->it_clock);
+	const struct k_clock *kc = timer->kclock;
 
 	if (WARN_ON_ONCE(!kc || !kc->timer_del))
 		return -EINVAL;

[toc] | [prev] | [next] | [standalone]


#1657367 — [tip:timers/core] posix-timers: Store k_clock pointer in k_itimer

Fromtip-bot for Thomas Gleixner <tipbot@zytor.com>
Date2017-06-05 10:30 +0200
Subject[tip:timers/core] posix-timers: Store k_clock pointer in k_itimer
Message-ID<tOVyG-rf-9@gated-at.bofh.it>
In reply to#1653575
Commit-ID:  d97bb75ddd2f38068df01da8abf26df78756253c
Gitweb:     http://git.kernel.org/tip/d97bb75ddd2f38068df01da8abf26df78756253c
Author:     Thomas Gleixner <tglx@linutronix.de>
AuthorDate: Tue, 30 May 2017 23:15:44 +0200
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Sun, 4 Jun 2017 15:40:25 +0200

posix-timers: Store k_clock pointer in k_itimer

Having the k_clock pointer in the k_itimer struct avoids the lookup in
several code pathes and makes the next steps of unification of the hrtimer
and alarmtimer based posix timers simpler.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: John Stultz <john.stultz@linaro.org>
Link: http://lkml.kernel.org/r/20170530211656.641222072@linutronix.de

---
 include/linux/posix-timers.h   | 2 ++
 kernel/time/posix-cpu-timers.c | 2 ++
 kernel/time/posix-timers.c     | 7 ++++---
 3 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/include/linux/posix-timers.h b/include/linux/posix-timers.h
index 908048f..8f9cca3 100644
--- a/include/linux/posix-timers.h
+++ b/include/linux/posix-timers.h
@@ -56,6 +56,7 @@ struct cpu_timer_list {
  * @list:		List head for binding the timer to signals->posix_timers
  * @t_hash:		Entry in the posix timer hash table
  * @it_lock:		Lock protecting the timer
+ * @kclock:		Pointer to the k_clock struct handling this timer
  * @it_clock:		The posix timer clock id
  * @it_id:		The posix timer id for identifying the timer
  * @it_overrun:		The overrun counter for pending signals
@@ -75,6 +76,7 @@ struct k_itimer {
 	struct list_head	list;
 	struct hlist_node	t_hash;
 	spinlock_t		it_lock;
+	const struct k_clock	*kclock;
 	clockid_t		it_clock;
 	timer_t			it_id;
 	int			it_overrun;
diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c
index 1683e50..0123ece 100644
--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -324,6 +324,8 @@ static int posix_cpu_timer_create(struct k_itimer *new_timer)
 	if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
 		return -EINVAL;
 
+	new_timer->kclock = &clock_posix_cpu;
+
 	INIT_LIST_HEAD(&new_timer->it.cpu.entry);
 
 	rcu_read_lock();
diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
index 7dd992c..eb007e1 100644
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -519,6 +519,7 @@ SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
 	it_id_set = IT_ID_SET;
 	new_timer->it_id = (timer_t) new_timer_id;
 	new_timer->it_clock = which_clock;
+	new_timer->kclock = kc;
 	new_timer->it_overrun = -1;
 
 	if (timer_event_spec) {
@@ -679,7 +680,7 @@ SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
 	if (!timr)
 		return -EINVAL;
 
-	kc = clockid_to_kclock(timr->it_clock);
+	kc = timr->kclock;
 	if (WARN_ON_ONCE(!kc || !kc->timer_get))
 		ret = -EINVAL;
 	else
@@ -798,7 +799,7 @@ retry:
 	if (!timr)
 		return -EINVAL;
 
-	kc = clockid_to_kclock(timr->it_clock);
+	kc = timr->kclock;
 	if (WARN_ON_ONCE(!kc || !kc->timer_set))
 		error = -EINVAL;
 	else
@@ -829,7 +830,7 @@ static int common_timer_del(struct k_itimer *timer)
 
 static inline int timer_delete_hook(struct k_itimer *timer)
 {
-	const struct k_clock *kc = clockid_to_kclock(timer->it_clock);
+	const struct k_clock *kc = timer->kclock;
 
 	if (WARN_ON_ONCE(!kc || !kc->timer_del))
 		return -EINVAL;

[toc] | [prev] | [next] | [standalone]


#1653576 — [patch 24/26] alarmtimer: Implement try_to_cancel callback

FromThomas Gleixner <tglx@linutronix.de>
Date2017-05-30 23:50 +0200
Subject[patch 24/26] alarmtimer: Implement try_to_cancel callback
Message-ID<tMXbz-4e6-17@gated-at.bofh.it>
In reply to#1653571
Preparatory change to utilize the common posix timer mechanisms.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 kernel/time/alarmtimer.c |   10 ++++++++++
 1 file changed, 10 insertions(+)

--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -573,6 +573,15 @@ static ktime_t alarm_timer_remaining(str
 }
 
 /**
+ * alarm_timer_try_to_cancel - Posix timer callback to cancel a timer
+ * @timr:	Pointer to the posixtimer data struct
+ */
+static int alarm_timer_try_to_cancel(struct k_itimer *timr)
+{
+	return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
+}
+
+/**
  * alarm_clock_getres - posix getres interface
  * @which_clock: clockid
  * @tp: timespec to fill
@@ -902,6 +911,7 @@ const struct k_clock alarm_clock = {
 	.timer_rearm		= alarm_timer_rearm,
 	.timer_forward		= alarm_timer_forward,
 	.timer_remaining	= alarm_timer_remaining,
+	.timer_try_to_cancel	= alarm_timer_try_to_cancel,
 	.nsleep			= alarm_timer_nsleep,
 };
 #endif /* CONFIG_POSIX_TIMERS */

[toc] | [prev] | [next] | [standalone]


#1657385 — [tip:timers/core] alarmtimer: Implement try_to_cancel callback

Fromtip-bot for Thomas Gleixner <tipbot@zytor.com>
Date2017-06-05 10:30 +0200
Subject[tip:timers/core] alarmtimer: Implement try_to_cancel callback
Message-ID<tOVyI-rf-53@gated-at.bofh.it>
In reply to#1653576
Commit-ID:  e344c9e76bc6af997926171bfd90d25bbae0a2c5
Gitweb:     http://git.kernel.org/tip/e344c9e76bc6af997926171bfd90d25bbae0a2c5
Author:     Thomas Gleixner <tglx@linutronix.de>
AuthorDate: Tue, 30 May 2017 23:15:57 +0200
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Sun, 4 Jun 2017 15:40:31 +0200

alarmtimer: Implement try_to_cancel callback

Preparatory change to utilize the common posix timer mechanisms.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: John Stultz <john.stultz@linaro.org>
Link: http://lkml.kernel.org/r/20170530211657.670026824@linutronix.de

---
 kernel/time/alarmtimer.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index 02ddc40..374bd85 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -573,6 +573,15 @@ static ktime_t alarm_timer_remaining(struct k_itimer *timr, ktime_t now)
 }
 
 /**
+ * alarm_timer_try_to_cancel - Posix timer callback to cancel a timer
+ * @timr:	Pointer to the posixtimer data struct
+ */
+static int alarm_timer_try_to_cancel(struct k_itimer *timr)
+{
+	return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
+}
+
+/**
  * alarm_clock_getres - posix getres interface
  * @which_clock: clockid
  * @tp: timespec to fill
@@ -902,6 +911,7 @@ const struct k_clock alarm_clock = {
 	.timer_rearm		= alarm_timer_rearm,
 	.timer_forward		= alarm_timer_forward,
 	.timer_remaining	= alarm_timer_remaining,
+	.timer_try_to_cancel	= alarm_timer_try_to_cancel,
 	.nsleep			= alarm_timer_nsleep,
 };
 #endif /* CONFIG_POSIX_TIMERS */

[toc] | [prev] | [next] | [standalone]


#1653577 — [patch 08/26] posix-timers: Move posix-timer internals to core

FromThomas Gleixner <tglx@linutronix.de>
Date2017-05-30 23:50 +0200
Subject[patch 08/26] posix-timers: Move posix-timer internals to core
Message-ID<tMXbA-4e6-21@gated-at.bofh.it>
In reply to#1653571
None of these declarations is required outside of kernel/time. Move them to
an internal header.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 include/linux/posix-timers.h   |   11 -----------
 kernel/time/alarmtimer.c       |    2 ++
 kernel/time/posix-clock.c      |    2 ++
 kernel/time/posix-cpu-timers.c |    2 ++
 kernel/time/posix-timers.c     |    1 +
 kernel/time/posix-timers.h     |    9 +++++++++
 6 files changed, 16 insertions(+), 11 deletions(-)

--- a/include/linux/posix-timers.h
+++ b/include/linux/posix-timers.h
@@ -118,17 +118,6 @@ struct k_clock {
 			   struct itimerspec64 *cur_setting);
 };
 
-extern const struct k_clock clock_posix_cpu;
-extern const struct k_clock clock_posix_dynamic;
-extern const struct k_clock clock_process;
-extern const struct k_clock clock_thread;
-extern const struct k_clock alarm_clock;
-
-/* function to call to trigger timer event */
-int posix_timer_event(struct k_itimer *timr, int si_private);
-
-void posix_cpu_timer_schedule(struct k_itimer *timer);
-
 void run_posix_cpu_timers(struct task_struct *task);
 void posix_cpu_timers_exit(struct task_struct *task);
 void posix_cpu_timers_exit_group(struct task_struct *task);
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -28,6 +28,8 @@
 #include <linux/workqueue.h>
 #include <linux/freezer.h>
 
+#include "posix-timers.h"
+
 #define CREATE_TRACE_POINTS
 #include <trace/events/alarmtimer.h>
 
--- a/kernel/time/posix-clock.c
+++ b/kernel/time/posix-clock.c
@@ -25,6 +25,8 @@
 #include <linux/syscalls.h>
 #include <linux/uaccess.h>
 
+#include "posix-timers.h"
+
 static void delete_clock(struct kref *kref);
 
 /*
--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -13,6 +13,8 @@
 #include <linux/tick.h>
 #include <linux/workqueue.h>
 
+#include "posix-timers.h"
+
 /*
  * Called after updating RLIMIT_CPU to run cpu timer and update
  * tsk->signal->cputime_expires expiration cache if necessary. Needs
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -51,6 +51,7 @@
 #include <linux/hashtable.h>
 
 #include "timekeeping.h"
+#include "posix-timers.h"
 
 /*
  * Management arrays for POSIX timers. Timers are now kept in static hash table
--- /dev/null
+++ b/kernel/time/posix-timers.h
@@ -0,0 +1,9 @@
+extern const struct k_clock clock_posix_cpu;
+extern const struct k_clock clock_posix_dynamic;
+extern const struct k_clock clock_process;
+extern const struct k_clock clock_thread;
+extern const struct k_clock alarm_clock;
+
+int posix_timer_event(struct k_itimer *timr, int si_private);
+
+void posix_cpu_timer_schedule(struct k_itimer *timer);

[toc] | [prev] | [next] | [standalone]


#1654356 — Re: [patch 08/26] posix-timers: Move posix-timer internals to core

FromChristoph Hellwig <hch@infradead.org>
Date2017-05-31 17:40 +0200
SubjectRe: [patch 08/26] posix-timers: Move posix-timer internals to core
Message-ID<tNdT3-6EZ-1@gated-at.bofh.it>
In reply to#1653577
On Tue, May 30, 2017 at 11:15:41PM +0200, Thomas Gleixner wrote:
> None of these declarations is required outside of kernel/time. Move them to
> an internal header.

It seems like struct k_clock itself could be marked private as well.

And with a small exported helper for /the /proc/timers implementation
struct k_itimer could be marked private, too.

[toc] | [prev] | [next] | [standalone]


#1657347 — [tip:timers/core] posix-timers: Move posix-timer internals to core

Fromtip-bot for Thomas Gleixner <tipbot@zytor.com>
Date2017-06-05 10:20 +0200
Subject[tip:timers/core] posix-timers: Move posix-timer internals to core
Message-ID<tOVp0-nU-13@gated-at.bofh.it>
In reply to#1653577
Commit-ID:  bab0aae9dcba9466dcc968b8bd21914f8f691631
Gitweb:     http://git.kernel.org/tip/bab0aae9dcba9466dcc968b8bd21914f8f691631
Author:     Thomas Gleixner <tglx@linutronix.de>
AuthorDate: Tue, 30 May 2017 23:15:41 +0200
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Sun, 4 Jun 2017 15:40:23 +0200

posix-timers: Move posix-timer internals to core

None of these declarations is required outside of kernel/time. Move them to
an internal header.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: John Stultz <john.stultz@linaro.org>
Cc: Christoph Hellwig <hch@lst.de>
Link: http://lkml.kernel.org/r/20170530211656.394803853@linutronix.de

---
 include/linux/posix-timers.h   | 30 ------------------------------
 kernel/time/alarmtimer.c       |  2 ++
 kernel/time/posix-clock.c      |  2 ++
 kernel/time/posix-cpu-timers.c |  2 ++
 kernel/time/posix-timers.c     |  1 +
 kernel/time/posix-timers.h     | 29 +++++++++++++++++++++++++++++
 6 files changed, 36 insertions(+), 30 deletions(-)

diff --git a/include/linux/posix-timers.h b/include/linux/posix-timers.h
index e06062c..a372e7e 100644
--- a/include/linux/posix-timers.h
+++ b/include/linux/posix-timers.h
@@ -100,36 +100,6 @@ struct k_itimer {
 	} it;
 };
 
-struct k_clock {
-	int (*clock_getres) (const clockid_t which_clock, struct timespec64 *tp);
-	int (*clock_set) (const clockid_t which_clock,
-			  const struct timespec64 *tp);
-	int (*clock_get) (const clockid_t which_clock, struct timespec64 *tp);
-	int (*clock_adj) (const clockid_t which_clock, struct timex *tx);
-	int (*timer_create) (struct k_itimer *timer);
-	int (*nsleep) (const clockid_t which_clock, int flags,
-		       struct timespec64 *, struct timespec __user *);
-	long (*nsleep_restart) (struct restart_block *restart_block);
-	int (*timer_set) (struct k_itimer *timr, int flags,
-			  struct itimerspec64 *new_setting,
-			  struct itimerspec64 *old_setting);
-	int (*timer_del) (struct k_itimer *timr);
-#define TIMER_RETRY 1
-	void (*timer_get) (struct k_itimer *timr,
-			   struct itimerspec64 *cur_setting);
-};
-
-extern const struct k_clock clock_posix_cpu;
-extern const struct k_clock clock_posix_dynamic;
-extern const struct k_clock clock_process;
-extern const struct k_clock clock_thread;
-extern const struct k_clock alarm_clock;
-
-/* function to call to trigger timer event */
-int posix_timer_event(struct k_itimer *timr, int si_private);
-
-void posix_cpu_timer_schedule(struct k_itimer *timer);
-
 void run_posix_cpu_timers(struct task_struct *task);
 void posix_cpu_timers_exit(struct task_struct *task);
 void posix_cpu_timers_exit_group(struct task_struct *task);
diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index 2a8675f..36855d6 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -28,6 +28,8 @@
 #include <linux/workqueue.h>
 #include <linux/freezer.h>
 
+#include "posix-timers.h"
+
 #define CREATE_TRACE_POINTS
 #include <trace/events/alarmtimer.h>
 
diff --git a/kernel/time/posix-clock.c b/kernel/time/posix-clock.c
index bd4fb78..17cdc55 100644
--- a/kernel/time/posix-clock.c
+++ b/kernel/time/posix-clock.c
@@ -25,6 +25,8 @@
 #include <linux/syscalls.h>
 #include <linux/uaccess.h>
 
+#include "posix-timers.h"
+
 static void delete_clock(struct kref *kref);
 
 /*
diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c
index c994347..a77a792 100644
--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -13,6 +13,8 @@
 #include <linux/tick.h>
 #include <linux/workqueue.h>
 
+#include "posix-timers.h"
+
 /*
  * Called after updating RLIMIT_CPU to run cpu timer and update
  * tsk->signal->cputime_expires expiration cache if necessary. Needs
diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
index b60b655..dee6a0d 100644
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -51,6 +51,7 @@
 #include <linux/hashtable.h>
 
 #include "timekeeping.h"
+#include "posix-timers.h"
 
 /*
  * Management arrays for POSIX timers. Timers are now kept in static hash table
diff --git a/kernel/time/posix-timers.h b/kernel/time/posix-timers.h
new file mode 100644
index 0000000..ad2dbd2
--- /dev/null
+++ b/kernel/time/posix-timers.h
@@ -0,0 +1,29 @@
+#define TIMER_RETRY 1
+
+struct k_clock {
+	int (*clock_getres) (const clockid_t which_clock, struct timespec64 *tp);
+	int (*clock_set) (const clockid_t which_clock,
+			  const struct timespec64 *tp);
+	int (*clock_get) (const clockid_t which_clock, struct timespec64 *tp);
+	int (*clock_adj) (const clockid_t which_clock, struct timex *tx);
+	int (*timer_create) (struct k_itimer *timer);
+	int (*nsleep) (const clockid_t which_clock, int flags,
+		       struct timespec64 *, struct timespec __user *);
+	long (*nsleep_restart) (struct restart_block *restart_block);
+	int (*timer_set) (struct k_itimer *timr, int flags,
+			  struct itimerspec64 *new_setting,
+			  struct itimerspec64 *old_setting);
+	int (*timer_del) (struct k_itimer *timr);
+	void (*timer_get) (struct k_itimer *timr,
+			   struct itimerspec64 *cur_setting);
+};
+
+extern const struct k_clock clock_posix_cpu;
+extern const struct k_clock clock_posix_dynamic;
+extern const struct k_clock clock_process;
+extern const struct k_clock clock_thread;
+extern const struct k_clock alarm_clock;
+
+int posix_timer_event(struct k_itimer *timr, int si_private);
+
+void posix_cpu_timer_schedule(struct k_itimer *timer);

[toc] | [prev] | [next] | [standalone]


#1653578 — [patch 17/26] posix-timers: Make use of forward/remaining callbacks

FromThomas Gleixner <tglx@linutronix.de>
Date2017-05-30 23:50 +0200
Subject[patch 17/26] posix-timers: Make use of forward/remaining callbacks
Message-ID<tMXbA-4e6-19@gated-at.bofh.it>
In reply to#1653571
Replace the hrtimer calls by calls to the new forward/remaining kclock
callbacks and move the hrtimer specific implementation into the
corresponding callback functions.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 kernel/time/posix-timers.c |   64 ++++++++++++++++++++++++++++++++++-----------
 1 file changed, 49 insertions(+), 15 deletions(-)

--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -607,6 +607,20 @@ static struct k_itimer *__lock_timer(tim
 	return NULL;
 }
 
+static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
+{
+	struct hrtimer *timer = &timr->it.real.timer;
+
+	return __hrtimer_expires_remaining_adjusted(timer, now);
+}
+
+static int common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
+{
+	struct hrtimer *timer = &timr->it.real.timer;
+
+	return (int)hrtimer_forward(timer, now, timr->it_interval);
+}
+
 /*
  * Get the time remaining on a POSIX.1b interval timer.  This function
  * is ALWAYS called with spin_lock_irq on the timer, thus it must not
@@ -626,42 +640,54 @@ static struct k_itimer *__lock_timer(tim
 static void
 common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
 {
+	const struct k_clock *kc = timr->kclock;
 	ktime_t now, remaining, iv;
-	struct hrtimer *timer = &timr->it.real.timer;
+	struct timespec64 ts64;
+	bool sig_none;
 
 	memset(cur_setting, 0, sizeof(*cur_setting));
 
+	sig_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE;
 	iv = timr->it_interval;
 
 	/* interval timer ? */
-	if (iv)
+	if (iv) {
 		cur_setting->it_interval = ktime_to_timespec64(iv);
-	else if (!hrtimer_active(timer) &&
-		 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
-		return;
+	} else if (!timr->it_active) {
+		/*
+		 * SIGEV_NONE oneshot timers are never queued. Check them
+		 * below.
+		 */
+		if (!sig_none)
+			return;
+	}
 
-	now = timer->base->get_time();
+	/*
+	 * The timespec64 based conversion is suboptimal, but it's not
+	 * worth to implement yet another callback.
+	 */
+	kc->clock_get(timr->it_clock, &ts64);
+	now = timespec64_to_ktime(ts64);
 
 	/*
-	 * When a requeue is pending or this is a SIGEV_NONE
-	 * timer move the expiry time forward by intervals, so
-	 * expiry is > now.
+	 * When a requeue is pending or this is a SIGEV_NONE timer move the
+	 * expiry time forward by intervals, so expiry is > now.
 	 */
-	if (iv && (timr->it_requeue_pending & REQUEUE_PENDING ||
-		   (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE))
-		timr->it_overrun += (unsigned int) hrtimer_forward(timer, now, iv);
+	if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
+		timr->it_overrun += kc->timer_forward(timr, now);
 
-	remaining = __hrtimer_expires_remaining_adjusted(timer, now);
+	remaining = kc->timer_remaining(timr, now);
 	/* Return 0 only, when the timer is expired and not pending */
 	if (remaining <= 0) {
 		/*
 		 * A single shot SIGEV_NONE timer must return 0, when
 		 * it is expired !
 		 */
-		if ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
+		if (!sig_none)
 			cur_setting->it_value.tv_nsec = 1;
-	} else
+	} else {
 		cur_setting->it_value = ktime_to_timespec64(remaining);
+	}
 }
 
 /* Get the time remaining on a POSIX.1b interval timer. */
@@ -1049,6 +1075,8 @@ static const struct k_clock clock_realti
 	.timer_get	= common_timer_get,
 	.timer_del	= common_timer_del,
 	.timer_rearm	= common_hrtimer_rearm,
+	.timer_forward	= common_hrtimer_forward,
+	.timer_remaining= common_hrtimer_remaining,
 };
 
 static const struct k_clock clock_monotonic = {
@@ -1061,6 +1089,8 @@ static const struct k_clock clock_monoto
 	.timer_get	= common_timer_get,
 	.timer_del	= common_timer_del,
 	.timer_rearm	= common_hrtimer_rearm,
+	.timer_forward	= common_hrtimer_forward,
+	.timer_remaining= common_hrtimer_remaining,
 };
 
 static const struct k_clock clock_monotonic_raw = {
@@ -1088,6 +1118,8 @@ static const struct k_clock clock_tai =
 	.timer_get	= common_timer_get,
 	.timer_del	= common_timer_del,
 	.timer_rearm	= common_hrtimer_rearm,
+	.timer_forward	= common_hrtimer_forward,
+	.timer_remaining= common_hrtimer_remaining,
 };
 
 static const struct k_clock clock_boottime = {
@@ -1100,6 +1132,8 @@ static const struct k_clock clock_bootti
 	.timer_get	= common_timer_get,
 	.timer_del	= common_timer_del,
 	.timer_rearm	= common_hrtimer_rearm,
+	.timer_forward	= common_hrtimer_forward,
+	.timer_remaining= common_hrtimer_remaining,
 };
 
 static const struct k_clock * const posix_clocks[] = {

[toc] | [prev] | [next] | [standalone]


#1657366 — [tip:timers/core] posix-timers: Make use of forward/remaining callbacks

Fromtip-bot for Thomas Gleixner <tipbot@zytor.com>
Date2017-06-05 10:30 +0200
Subject[tip:timers/core] posix-timers: Make use of forward/remaining callbacks
Message-ID<tOVyG-rf-7@gated-at.bofh.it>
In reply to#1653578
Commit-ID:  91d57bae08689199c8acc77a8b3b41150cafab1c
Gitweb:     http://git.kernel.org/tip/91d57bae08689199c8acc77a8b3b41150cafab1c
Author:     Thomas Gleixner <tglx@linutronix.de>
AuthorDate: Tue, 30 May 2017 23:15:50 +0200
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Sun, 4 Jun 2017 15:40:27 +0200

posix-timers: Make use of forward/remaining callbacks

Replace the hrtimer calls by calls to the new forward/remaining kclock
callbacks and move the hrtimer specific implementation into the
corresponding callback functions.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: John Stultz <john.stultz@linaro.org>
Link: http://lkml.kernel.org/r/20170530211657.121437232@linutronix.de

---
 kernel/time/posix-timers.c | 64 +++++++++++++++++++++++++++++++++++-----------
 1 file changed, 49 insertions(+), 15 deletions(-)

diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
index 795215b..48f6c37 100644
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -607,6 +607,20 @@ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
 	return NULL;
 }
 
+static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
+{
+	struct hrtimer *timer = &timr->it.real.timer;
+
+	return __hrtimer_expires_remaining_adjusted(timer, now);
+}
+
+static int common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
+{
+	struct hrtimer *timer = &timr->it.real.timer;
+
+	return (int)hrtimer_forward(timer, now, timr->it_interval);
+}
+
 /*
  * Get the time remaining on a POSIX.1b interval timer.  This function
  * is ALWAYS called with spin_lock_irq on the timer, thus it must not
@@ -626,42 +640,54 @@ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
 static void
 common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
 {
+	const struct k_clock *kc = timr->kclock;
 	ktime_t now, remaining, iv;
-	struct hrtimer *timer = &timr->it.real.timer;
+	struct timespec64 ts64;
+	bool sig_none;
 
 	memset(cur_setting, 0, sizeof(*cur_setting));
 
+	sig_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE;
 	iv = timr->it_interval;
 
 	/* interval timer ? */
-	if (iv)
+	if (iv) {
 		cur_setting->it_interval = ktime_to_timespec64(iv);
-	else if (!hrtimer_active(timer) &&
-		 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
-		return;
+	} else if (!timr->it_active) {
+		/*
+		 * SIGEV_NONE oneshot timers are never queued. Check them
+		 * below.
+		 */
+		if (!sig_none)
+			return;
+	}
 
-	now = timer->base->get_time();
+	/*
+	 * The timespec64 based conversion is suboptimal, but it's not
+	 * worth to implement yet another callback.
+	 */
+	kc->clock_get(timr->it_clock, &ts64);
+	now = timespec64_to_ktime(ts64);
 
 	/*
-	 * When a requeue is pending or this is a SIGEV_NONE
-	 * timer move the expiry time forward by intervals, so
-	 * expiry is > now.
+	 * When a requeue is pending or this is a SIGEV_NONE timer move the
+	 * expiry time forward by intervals, so expiry is > now.
 	 */
-	if (iv && (timr->it_requeue_pending & REQUEUE_PENDING ||
-		   (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE))
-		timr->it_overrun += (unsigned int) hrtimer_forward(timer, now, iv);
+	if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
+		timr->it_overrun += kc->timer_forward(timr, now);
 
-	remaining = __hrtimer_expires_remaining_adjusted(timer, now);
+	remaining = kc->timer_remaining(timr, now);
 	/* Return 0 only, when the timer is expired and not pending */
 	if (remaining <= 0) {
 		/*
 		 * A single shot SIGEV_NONE timer must return 0, when
 		 * it is expired !
 		 */
-		if ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
+		if (!sig_none)
 			cur_setting->it_value.tv_nsec = 1;
-	} else
+	} else {
 		cur_setting->it_value = ktime_to_timespec64(remaining);
+	}
 }
 
 /* Get the time remaining on a POSIX.1b interval timer. */
@@ -1049,6 +1075,8 @@ static const struct k_clock clock_realtime = {
 	.timer_get	= common_timer_get,
 	.timer_del	= common_timer_del,
 	.timer_rearm	= common_hrtimer_rearm,
+	.timer_forward	= common_hrtimer_forward,
+	.timer_remaining= common_hrtimer_remaining,
 };
 
 static const struct k_clock clock_monotonic = {
@@ -1061,6 +1089,8 @@ static const struct k_clock clock_monotonic = {
 	.timer_get	= common_timer_get,
 	.timer_del	= common_timer_del,
 	.timer_rearm	= common_hrtimer_rearm,
+	.timer_forward	= common_hrtimer_forward,
+	.timer_remaining= common_hrtimer_remaining,
 };
 
 static const struct k_clock clock_monotonic_raw = {
@@ -1088,6 +1118,8 @@ static const struct k_clock clock_tai = {
 	.timer_get	= common_timer_get,
 	.timer_del	= common_timer_del,
 	.timer_rearm	= common_hrtimer_rearm,
+	.timer_forward	= common_hrtimer_forward,
+	.timer_remaining= common_hrtimer_remaining,
 };
 
 static const struct k_clock clock_boottime = {
@@ -1100,6 +1132,8 @@ static const struct k_clock clock_boottime = {
 	.timer_get	= common_timer_get,
 	.timer_del	= common_timer_del,
 	.timer_rearm	= common_hrtimer_rearm,
+	.timer_forward	= common_hrtimer_forward,
+	.timer_remaining= common_hrtimer_remaining,
 };
 
 static const struct k_clock * const posix_clocks[] = {

[toc] | [prev] | [next] | [standalone]


#1653580 — [patch 07/26] posix-timers: Cleanup struct k_itimer

FromThomas Gleixner <tglx@linutronix.de>
Date2017-05-30 23:50 +0200
Subject[patch 07/26] posix-timers: Cleanup struct k_itimer
Message-ID<tMXbA-4e6-25@gated-at.bofh.it>
In reply to#1653571
As a preparation for further changes, cleanup the formatting of the
k_itimer structure and add kernel doc comments.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 include/linux/posix-timers.h |   61 ++++++++++++++++++++++++++++---------------
 1 file changed, 40 insertions(+), 21 deletions(-)

--- a/include/linux/posix-timers.h
+++ b/include/linux/posix-timers.h
@@ -48,35 +48,54 @@ struct cpu_timer_list {
 #define FD_TO_CLOCKID(fd)	((~(clockid_t) (fd) << 3) | CLOCKFD)
 #define CLOCKID_TO_FD(clk)	((unsigned int) ~((clk) >> 3))
 
-/* POSIX.1b interval timer structure. */
-struct k_itimer {
-	struct list_head list;		/* free/ allocate list */
-	struct hlist_node t_hash;
-	spinlock_t it_lock;
-	clockid_t it_clock;		/* which timer type */
-	timer_t it_id;			/* timer id */
-	int it_overrun;			/* overrun on pending signal  */
-	int it_overrun_last;		/* overrun on last delivered signal */
-	int it_requeue_pending;		/* waiting to requeue this timer */
 #define REQUEUE_PENDING 1
-	int it_sigev_notify;		/* notify word of sigevent struct */
-	struct signal_struct *it_signal;
+
+/**
+ * struct k_itimer - POSIX.1b interval timer structure.
+ * @list:		List head for binding the timer to signals->posix_timers
+ * @t_hash:		Entry in the posix timer hash table
+ * @it_lock:		Lock protecting the timer
+ * @it_clock:		The posix timer clock id
+ * @it_id:		The posix timer id for identifying the timer
+ * @it_overrun:		The overrun counter for pending signals
+ * @it_overrun_last:	The overrun at the time of the last delivered signal
+ * @it_requeue_pending:	Indicator that timer waits for being requeued on
+ *			signal delivery
+ * @it_sigev_notify:	The notify word of sigevent struct for signal delivery
+ * @it_signal:		Pointer to the creators signal struct
+ * @it_pid:		The pid of the process/task targeted by the signal
+ * @it_process:		The task to wakeup on clock_nanosleep (CPU timers)
+ * @sigq:		Pointer to preallocated sigqueue
+ * @it:			Union representing the various posix timer type
+ *			internals. Also used for rcu freeing the timer.
+ */
+struct k_itimer {
+	struct list_head	list;
+	struct hlist_node	t_hash;
+	spinlock_t		it_lock;
+	clockid_t		it_clock;
+	timer_t			it_id;
+	int			it_overrun;
+	int			it_overrun_last;
+	int			it_requeue_pending;
+	int			it_sigev_notify;
+	struct signal_struct	*it_signal;
 	union {
-		struct pid *it_pid;	/* pid of process to send signal to */
-		struct task_struct *it_process;	/* for clock_nanosleep */
+		struct pid		*it_pid;
+		struct task_struct	*it_process;
 	};
-	struct sigqueue *sigq;		/* signal queue entry. */
+	struct sigqueue		*sigq;
 	union {
 		struct {
-			struct hrtimer timer;
-			ktime_t interval;
+			struct hrtimer	timer;
+			ktime_t		interval;
 		} real;
-		struct cpu_timer_list cpu;
+		struct cpu_timer_list	cpu;
 		struct {
-			struct alarm alarmtimer;
-			ktime_t interval;
+			struct alarm	alarmtimer;
+			ktime_t		interval;
 		} alarm;
-		struct rcu_head rcu;
+		struct rcu_head		rcu;
 	} it;
 };
 

[toc] | [prev] | [next] | [standalone]


#1657360 — [tip:timers/core] posix-timers: Cleanup struct k_itimer

Fromtip-bot for Thomas Gleixner <tipbot@zytor.com>
Date2017-06-05 10:20 +0200
Subject[tip:timers/core] posix-timers: Cleanup struct k_itimer
Message-ID<tOVp1-nU-43@gated-at.bofh.it>
In reply to#1653580
Commit-ID:  03676b41a8ffcbb1f6d9eb6ca754b2bfa431fd59
Gitweb:     http://git.kernel.org/tip/03676b41a8ffcbb1f6d9eb6ca754b2bfa431fd59
Author:     Thomas Gleixner <tglx@linutronix.de>
AuthorDate: Tue, 30 May 2017 23:15:40 +0200
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Sun, 4 Jun 2017 15:40:23 +0200

posix-timers: Cleanup struct k_itimer

As a preparation for further changes, cleanup the formatting of the
k_itimer structure and add kernel doc comments.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: John Stultz <john.stultz@linaro.org>
Link: http://lkml.kernel.org/r/20170530211656.316574129@linutronix.de

---
 include/linux/posix-timers.h | 61 +++++++++++++++++++++++++++++---------------
 1 file changed, 40 insertions(+), 21 deletions(-)

diff --git a/include/linux/posix-timers.h b/include/linux/posix-timers.h
index 8929f7e..e06062c 100644
--- a/include/linux/posix-timers.h
+++ b/include/linux/posix-timers.h
@@ -49,35 +49,54 @@ struct cpu_timer_list {
 #define FD_TO_CLOCKID(fd)	((~(clockid_t) (fd) << 3) | CLOCKFD)
 #define CLOCKID_TO_FD(clk)	((unsigned int) ~((clk) >> 3))
 
-/* POSIX.1b interval timer structure. */
-struct k_itimer {
-	struct list_head list;		/* free/ allocate list */
-	struct hlist_node t_hash;
-	spinlock_t it_lock;
-	clockid_t it_clock;		/* which timer type */
-	timer_t it_id;			/* timer id */
-	int it_overrun;			/* overrun on pending signal  */
-	int it_overrun_last;		/* overrun on last delivered signal */
-	int it_requeue_pending;		/* waiting to requeue this timer */
 #define REQUEUE_PENDING 1
-	int it_sigev_notify;		/* notify word of sigevent struct */
-	struct signal_struct *it_signal;
+
+/**
+ * struct k_itimer - POSIX.1b interval timer structure.
+ * @list:		List head for binding the timer to signals->posix_timers
+ * @t_hash:		Entry in the posix timer hash table
+ * @it_lock:		Lock protecting the timer
+ * @it_clock:		The posix timer clock id
+ * @it_id:		The posix timer id for identifying the timer
+ * @it_overrun:		The overrun counter for pending signals
+ * @it_overrun_last:	The overrun at the time of the last delivered signal
+ * @it_requeue_pending:	Indicator that timer waits for being requeued on
+ *			signal delivery
+ * @it_sigev_notify:	The notify word of sigevent struct for signal delivery
+ * @it_signal:		Pointer to the creators signal struct
+ * @it_pid:		The pid of the process/task targeted by the signal
+ * @it_process:		The task to wakeup on clock_nanosleep (CPU timers)
+ * @sigq:		Pointer to preallocated sigqueue
+ * @it:			Union representing the various posix timer type
+ *			internals. Also used for rcu freeing the timer.
+ */
+struct k_itimer {
+	struct list_head	list;
+	struct hlist_node	t_hash;
+	spinlock_t		it_lock;
+	clockid_t		it_clock;
+	timer_t			it_id;
+	int			it_overrun;
+	int			it_overrun_last;
+	int			it_requeue_pending;
+	int			it_sigev_notify;
+	struct signal_struct	*it_signal;
 	union {
-		struct pid *it_pid;	/* pid of process to send signal to */
-		struct task_struct *it_process;	/* for clock_nanosleep */
+		struct pid		*it_pid;
+		struct task_struct	*it_process;
 	};
-	struct sigqueue *sigq;		/* signal queue entry. */
+	struct sigqueue		*sigq;
 	union {
 		struct {
-			struct hrtimer timer;
-			ktime_t interval;
+			struct hrtimer	timer;
+			ktime_t		interval;
 		} real;
-		struct cpu_timer_list cpu;
+		struct cpu_timer_list	cpu;
 		struct {
-			struct alarm alarmtimer;
-			ktime_t interval;
+			struct alarm	alarmtimer;
+			ktime_t		interval;
 		} alarm;
-		struct rcu_head rcu;
+		struct rcu_head		rcu;
 	} it;
 };
 

[toc] | [prev] | [next] | [standalone]


#1653581 — [patch 02/26] alarmtimer: Rate limit periodic intervals

FromThomas Gleixner <tglx@linutronix.de>
Date2017-05-30 23:50 +0200
Subject[patch 02/26] alarmtimer: Rate limit periodic intervals
Message-ID<tMXbA-4e6-29@gated-at.bofh.it>
In reply to#1653571
The alarmtimer code has another source of potentially rearming itself too
fast. Interval timers with a very samll interval have a similar CPU hog
effect as the previously fixed overflow issue.

The reason is that alarmtimers do not implement the normal protection
against this kind of problem which the other posix timer use:

  timer expires -> queue signal -> deliver signal -> rearm timer

This scheme brings the rearming under scheduler control and prevents
permanently firing timers which hog the CPU.

Bringing this scheme to the alarm timer code is a major overhaul because it
lacks all the necessary mechanisms completely.

So for a quick fix limit the interval to one jiffie. This is not
problematic in practice as alarmtimers are usually backed by an RTC for
suspend which have 1 second resolution. It could be therefor argued that
the resolution of this clock should be set to 1 second in general, but
that's outside the scope of this fix.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Kostya Serebryany <kcc@google.com>
Cc: syzkaller <syzkaller@googlegroups.com>
---
 kernel/time/alarmtimer.c |    8 ++++++++
 1 file changed, 8 insertions(+)

--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -662,6 +662,14 @@ static int alarm_timer_set(struct k_itim
 
 	/* start the timer */
 	timr->it.alarm.interval = timespec64_to_ktime(new_setting->it_interval);
+
+	/*
+	 * Rate limit to the tick as a hot fix to prevent DOS. Will be
+	 * mopped up later.
+	 */
+	if (timr->it.alarm.interval < TICK_NSEC)
+		timr->it.alarm.interval = TICK_NSEC;
+
 	exp = timespec64_to_ktime(new_setting->it_value);
 	/* Convert (if necessary) to absolute time */
 	if (flags != TIMER_ABSTIME) {

[toc] | [prev] | [next] | [standalone]


#1657070 — [tip:timers/urgent] alarmtimer: Rate limit periodic intervals

Fromtip-bot for Thomas Gleixner <tipbot@zytor.com>
Date2017-06-04 15:30 +0200
Subject[tip:timers/urgent] alarmtimer: Rate limit periodic intervals
Message-ID<tODLr-5EB-1@gated-at.bofh.it>
In reply to#1653581
Commit-ID:  a90b040d8a7a206dd1863c60a47ace5319c99321
Gitweb:     http://git.kernel.org/tip/a90b040d8a7a206dd1863c60a47ace5319c99321
Author:     Thomas Gleixner <tglx@linutronix.de>
AuthorDate: Tue, 30 May 2017 23:15:35 +0200
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Sun, 4 Jun 2017 15:18:28 +0200

alarmtimer: Rate limit periodic intervals

The alarmtimer code has another source of potentially rearming itself too
fast. Interval timers with a very samll interval have a similar CPU hog
effect as the previously fixed overflow issue.

The reason is that alarmtimers do not implement the normal protection
against this kind of problem which the other posix timer use:

  timer expires -> queue signal -> deliver signal -> rearm timer

This scheme brings the rearming under scheduler control and prevents
permanently firing timers which hog the CPU.

Bringing this scheme to the alarm timer code is a major overhaul because it
lacks all the necessary mechanisms completely.

So for a quick fix limit the interval to one jiffie. This is not
problematic in practice as alarmtimers are usually backed by an RTC for
suspend which have 1 second resolution. It could be therefor argued that
the resolution of this clock should be set to 1 second in general, but
that's outside the scope of this fix.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Kostya Serebryany <kcc@google.com>
Cc: syzkaller <syzkaller@googlegroups.com>
Cc: John Stultz <john.stultz@linaro.org>
Cc: Dmitry Vyukov <dvyukov@google.com>
Link: http://lkml.kernel.org/r/20170530211655.896767100@linutronix.de

---
 kernel/time/alarmtimer.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index 2b2e032..ee2f420 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -660,6 +660,14 @@ static int alarm_timer_set(struct k_itimer *timr, int flags,
 
 	/* start the timer */
 	timr->it.alarm.interval = timespec64_to_ktime(new_setting->it_interval);
+
+	/*
+	 * Rate limit to the tick as a hot fix to prevent DOS. Will be
+	 * mopped up later.
+	 */
+	if (timr->it.alarm.interval < TICK_NSEC)
+		timr->it.alarm.interval = TICK_NSEC;
+
 	exp = timespec64_to_ktime(new_setting->it_value);
 	/* Convert (if necessary) to absolute time */
 	if (flags != TIMER_ABSTIME) {

[toc] | [prev] | [next] | [standalone]


#1657073 — [tip:timers/urgent] alarmtimer: Rate limit periodic intervals

Fromtip-bot for Thomas Gleixner <tipbot@zytor.com>
Date2017-06-04 15:40 +0200
Subject[tip:timers/urgent] alarmtimer: Rate limit periodic intervals
Message-ID<tODV7-5Ir-11@gated-at.bofh.it>
In reply to#1653581
Commit-ID:  ff86bf0c65f14346bf2440534f9ba5ac232c39a0
Gitweb:     http://git.kernel.org/tip/ff86bf0c65f14346bf2440534f9ba5ac232c39a0
Author:     Thomas Gleixner <tglx@linutronix.de>
AuthorDate: Tue, 30 May 2017 23:15:35 +0200
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Sun, 4 Jun 2017 15:21:18 +0200

alarmtimer: Rate limit periodic intervals

The alarmtimer code has another source of potentially rearming itself too
fast. Interval timers with a very samll interval have a similar CPU hog
effect as the previously fixed overflow issue.

The reason is that alarmtimers do not implement the normal protection
against this kind of problem which the other posix timer use:

  timer expires -> queue signal -> deliver signal -> rearm timer

This scheme brings the rearming under scheduler control and prevents
permanently firing timers which hog the CPU.

Bringing this scheme to the alarm timer code is a major overhaul because it
lacks all the necessary mechanisms completely.

So for a quick fix limit the interval to one jiffie. This is not
problematic in practice as alarmtimers are usually backed by an RTC for
suspend which have 1 second resolution. It could be therefor argued that
the resolution of this clock should be set to 1 second in general, but
that's outside the scope of this fix.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Kostya Serebryany <kcc@google.com>
Cc: syzkaller <syzkaller@googlegroups.com>
Cc: John Stultz <john.stultz@linaro.org>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: stable@vger.kernel.org
Link: http://lkml.kernel.org/r/20170530211655.896767100@linutronix.de

---
 kernel/time/alarmtimer.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index 2b2e032..ee2f420 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -660,6 +660,14 @@ static int alarm_timer_set(struct k_itimer *timr, int flags,
 
 	/* start the timer */
 	timr->it.alarm.interval = timespec64_to_ktime(new_setting->it_interval);
+
+	/*
+	 * Rate limit to the tick as a hot fix to prevent DOS. Will be
+	 * mopped up later.
+	 */
+	if (timr->it.alarm.interval < TICK_NSEC)
+		timr->it.alarm.interval = TICK_NSEC;
+
 	exp = timespec64_to_ktime(new_setting->it_value);
 	/* Convert (if necessary) to absolute time */
 	if (flags != TIMER_ABSTIME) {

[toc] | [prev] | [next] | [standalone]


#1653582 — [patch 14/26] posix-timers: Use timer_rearm() callback in posixtimer_rearm()

FromThomas Gleixner <tglx@linutronix.de>
Date2017-05-30 23:50 +0200
Subject[patch 14/26] posix-timers: Use timer_rearm() callback in posixtimer_rearm()
Message-ID<tMXbA-4e6-27@gated-at.bofh.it>
In reply to#1653571
Use the new timer_rearm() callback to replace the conditional hardcoded
calls into the hrtimer and cpu timer code.

This allows later to bring the same logic to alarmtimers.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 kernel/time/posix-cpu-timers.c |    7 +++++--
 kernel/time/posix-timers.c     |   12 ++++++------
 kernel/time/posix-timers.h     |    2 --
 3 files changed, 11 insertions(+), 10 deletions(-)

--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -15,6 +15,8 @@
 
 #include "posix-timers.h"
 
+static void posix_cpu_timer_rearm(struct k_itimer *timer);
+
 /*
  * Called after updating RLIMIT_CPU to run cpu timer and update
  * tsk->signal->cputime_expires expiration cache if necessary. Needs
@@ -528,7 +530,7 @@ static void cpu_timer_fire(struct k_itim
 		 * reload the timer.  But we need to keep it
 		 * ticking in case the signal is deliverable next time.
 		 */
-		posix_cpu_timer_schedule(timer);
+		posix_cpu_timer_rearm(timer);
 		++timer->it_requeue_pending;
 	}
 }
@@ -976,7 +978,7 @@ static void check_process_timers(struct
  * This is called from the signal code (via posixtimer_rearm)
  * when the last timer signal was delivered and we have to reload the timer.
  */
-void posix_cpu_timer_schedule(struct k_itimer *timer)
+static void posix_cpu_timer_rearm(struct k_itimer *timer)
 {
 	struct sighand_struct *sighand;
 	unsigned long flags;
@@ -1423,6 +1425,7 @@ const struct k_clock clock_posix_cpu = {
 	.timer_set	= posix_cpu_timer_set,
 	.timer_del	= posix_cpu_timer_del,
 	.timer_get	= posix_cpu_timer_get,
+	.timer_rearm	= posix_cpu_timer_rearm,
 };
 
 const struct k_clock clock_process = {
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -278,10 +278,9 @@ static __init int init_posix_timers(void
 					NULL);
 	return 0;
 }
-
 __initcall(init_posix_timers);
 
-static void schedule_next_timer(struct k_itimer *timr)
+static void common_hrtimer_rearm(struct k_itimer *timr)
 {
 	struct hrtimer *timer = &timr->it.real.timer;
 
@@ -315,10 +314,7 @@ void posixtimer_rearm(struct siginfo *in
 		return;
 
 	if (timr->it_requeue_pending == info->si_sys_private) {
-		if (timr->it_clock < 0)
-			posix_cpu_timer_schedule(timr);
-		else
-			schedule_next_timer(timr);
+		timr->kclock->timer_rearm(timr);
 
 		timr->it_overrun_last = timr->it_overrun;
 		timr->it_overrun = -1;
@@ -1046,6 +1042,7 @@ static const struct k_clock clock_realti
 	.timer_set	= common_timer_set,
 	.timer_get	= common_timer_get,
 	.timer_del	= common_timer_del,
+	.timer_rearm	= common_hrtimer_rearm,
 };
 
 static const struct k_clock clock_monotonic = {
@@ -1057,6 +1054,7 @@ static const struct k_clock clock_monoto
 	.timer_set	= common_timer_set,
 	.timer_get	= common_timer_get,
 	.timer_del	= common_timer_del,
+	.timer_rearm	= common_hrtimer_rearm,
 };
 
 static const struct k_clock clock_monotonic_raw = {
@@ -1083,6 +1081,7 @@ static const struct k_clock clock_tai =
 	.timer_set	= common_timer_set,
 	.timer_get	= common_timer_get,
 	.timer_del	= common_timer_del,
+	.timer_rearm	= common_hrtimer_rearm,
 };
 
 static const struct k_clock clock_boottime = {
@@ -1094,6 +1093,7 @@ static const struct k_clock clock_bootti
 	.timer_set	= common_timer_set,
 	.timer_get	= common_timer_get,
 	.timer_del	= common_timer_del,
+	.timer_rearm	= common_hrtimer_rearm,
 };
 
 static const struct k_clock * const posix_clocks[] = {
--- a/kernel/time/posix-timers.h
+++ b/kernel/time/posix-timers.h
@@ -5,5 +5,3 @@ extern const struct k_clock clock_thread
 extern const struct k_clock alarm_clock;
 
 int posix_timer_event(struct k_itimer *timr, int si_private);
-
-void posix_cpu_timer_schedule(struct k_itimer *timer);

[toc] | [prev] | [next] | [standalone]


Page 1 of 3  [1] 2 3  Next page →

Back to top | Article view | linux.kernel


csiph-web