[16/45] posix-timers: Consolidate timer setup

Message ID 20230606142031.928021762@linutronix.de
State New
Headers
Series posix-timers: Cure inconsistencies and the SIG_IGN mess |

Commit Message

Thomas Gleixner June 6, 2023, 2:37 p.m. UTC
  hrtimer based and CPU timers have their own way to install the new interval
and to reset overrun and signal handling related data.

Create a helper function and do the same operation for all variants.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 kernel/time/posix-cpu-timers.c |   15 +--------------
 kernel/time/posix-timers.c     |   23 +++++++++++++++++------
 kernel/time/posix-timers.h     |    1 +
 3 files changed, 19 insertions(+), 20 deletions(-)
  

Comments

Frederic Weisbecker July 3, 2023, 9:12 p.m. UTC | #1
Le Tue, Jun 06, 2023 at 04:37:44PM +0200, Thomas Gleixner a écrit :
> hrtimer based and CPU timers have their own way to install the new interval
> and to reset overrun and signal handling related data.
> 
> Create a helper function and do the same operation for all variants.
> 
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
> ---
>  kernel/time/posix-cpu-timers.c |   15 +--------------
>  kernel/time/posix-timers.c     |   23 +++++++++++++++++------
>  kernel/time/posix-timers.h     |    1 +
>  3 files changed, 19 insertions(+), 20 deletions(-)
> 
> --- a/kernel/time/posix-cpu-timers.c
> +++ b/kernel/time/posix-cpu-timers.c
> @@ -709,21 +709,8 @@ static int posix_cpu_timer_set(struct k_
>  		trigger_base_recalc_expires(timer, p);
>  
>  	unlock_task_sighand(p, &flags);
> -	/*
> -	 * Install the new reload setting, and
> -	 * set up the signal and overrun bookkeeping.
> -	 */
> -	timer->it_interval = timespec64_to_ktime(new->it_interval);
>  
> -	/*
> -	 * This acts as a modification timestamp for the timer,
> -	 * so any automatic reload attempt will punt on seeing
> -	 * that we have reset the timer manually.
> -	 */
> -	timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
> -		~REQUEUE_PENDING;
> -	timer->it_overrun_last = 0;
> -	timer->it_overrun = -1;
> +	posix_timer_set_common(timer, new);

So before this patch if a posix cpu timer is set with:

   .it_value = 0,
   .it_interval = 5

a further retrieval would return the same. But after this patch
it will return:

   .it_value = 0,
   .it_interval = 0

That's an ABI change, not sure if it matters though...
  

Patch

--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -709,21 +709,8 @@  static int posix_cpu_timer_set(struct k_
 		trigger_base_recalc_expires(timer, p);
 
 	unlock_task_sighand(p, &flags);
-	/*
-	 * Install the new reload setting, and
-	 * set up the signal and overrun bookkeeping.
-	 */
-	timer->it_interval = timespec64_to_ktime(new->it_interval);
 
-	/*
-	 * This acts as a modification timestamp for the timer,
-	 * so any automatic reload attempt will punt on seeing
-	 * that we have reset the timer manually.
-	 */
-	timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
-		~REQUEUE_PENDING;
-	timer->it_overrun_last = 0;
-	timer->it_overrun = -1;
+	posix_timer_set_common(timer, new);
 
 	/*
 	 * If the new expiry time was already in the past the timer was not
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -853,6 +853,21 @@  static struct k_itimer *timer_wait_runni
 	return lock_timer(timer_id, flags);
 }
 
+/*
+ * Set up the new interval and reset the signal delivery data
+ */
+void posix_timer_set_common(struct k_itimer *timer, struct itimerspec64 *new_setting)
+{
+	if (new_setting->it_value.tv_sec || new_setting->it_value.tv_nsec)
+		timer->it_interval = timespec64_to_ktime(new_setting->it_interval);
+
+	/* Prevent reloading in case there is a signal pending */
+	timer->it_requeue_pending = (timer->it_requeue_pending + 2) & ~REQUEUE_PENDING;
+	/* Reset overrun accounting */
+	timer->it_overrun_last = 0;
+	timer->it_overrun = -1LL;
+}
+
 /* Set a POSIX.1b interval timer. */
 int common_timer_set(struct k_itimer *timr, int flags,
 		     struct itimerspec64 *new_setting,
@@ -875,16 +890,12 @@  int common_timer_set(struct k_itimer *ti
 		return TIMER_RETRY;
 
 	timr->it_active = 0;
-	timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
-		~REQUEUE_PENDING;
-	timr->it_overrun_last = 0;
-	timr->it_overrun = -1LL;
+	posix_timer_set_common(timr, new_setting);
 
-	/* Switch off the timer when it_value is zero */
+	/* Keep timer disarmed when it_value is zero */
 	if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
 		return 0;
 
-	timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
 	expires = timespec64_to_ktime(new_setting->it_value);
 	if (flags & TIMER_ABSTIME)
 		expires = timens_ktime_to_host(timr->it_clock, expires);
--- a/kernel/time/posix-timers.h
+++ b/kernel/time/posix-timers.h
@@ -42,4 +42,5 @@  void common_timer_get(struct k_itimer *t
 int common_timer_set(struct k_itimer *timr, int flags,
 		     struct itimerspec64 *new_setting,
 		     struct itimerspec64 *old_setting);
+void posix_timer_set_common(struct k_itimer *timer, struct itimerspec64 *new_setting);
 int common_timer_del(struct k_itimer *timer);