[17/45] posix-cpu-timers: Make k_itimer::it_active consistent

Message ID 20230606142031.983662966@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
  Posix CPU timers are not updating k_itimer::it_active which makes it
impossible to base decisions in the common posix timer code on it.

Update it when queueing or dequeueing posix CPU timers.

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

Comments

Frederic Weisbecker July 3, 2023, 10:30 p.m. UTC | #1
On Tue, Jun 06, 2023 at 04:37:45PM +0200, Thomas Gleixner wrote:
> @@ -787,6 +790,7 @@ static u64 collect_timerqueue(struct tim
>  
>  	while ((next = timerqueue_getnext(head))) {
>  		struct cpu_timer *ctmr;
> +		struct k_itimer *ktmr;
>  		u64 expires;
>  
>  		ctmr = container_of(next, struct cpu_timer, node);
> @@ -799,6 +803,8 @@ static u64 collect_timerqueue(struct tim
>  		/* See posix_cpu_timer_wait_running() */
>  		rcu_assign_pointer(ctmr->handling, current);
>  		cpu_timer_dequeue(ctmr);
> +		ktmr = container_of(ctmr, struct k_itimer, it.cpu);
> +		ktmr->it_active = 0;

Maybe move this to cpu_timer_fire() because:

1) Other posix-timer implementation set it_active = 0
   from their actual handler.

2) This place doesn't have the timer locked. Better Make
   sure we have this protection to avoid mistakes against
   potential future use of it_active.

Thanks.

>  		list_add_tail(&ctmr->elist, firing);
>  	}
>  
>
  

Patch

--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -453,6 +453,7 @@  static void disarm_timer(struct k_itimer
 	struct cpu_timer *ctmr = &timer->it.cpu;
 	struct posix_cputimer_base *base;
 
+	timer->it_active = 0;
 	if (!cpu_timer_dequeue(ctmr))
 		return;
 
@@ -559,6 +560,7 @@  static void arm_timer(struct k_itimer *t
 	struct cpu_timer *ctmr = &timer->it.cpu;
 	u64 newexp = cpu_timer_getexpires(ctmr);
 
+	timer->it_active = 1;
 	if (!cpu_timer_enqueue(&base->tqhead, ctmr))
 		return;
 
@@ -668,6 +670,7 @@  static int posix_cpu_timer_set(struct k_
 		ret = TIMER_RETRY;
 	} else {
 		cpu_timer_dequeue(ctmr);
+		timer->it_active = 0;
 	}
 
 	/*
@@ -787,6 +790,7 @@  static u64 collect_timerqueue(struct tim
 
 	while ((next = timerqueue_getnext(head))) {
 		struct cpu_timer *ctmr;
+		struct k_itimer *ktmr;
 		u64 expires;
 
 		ctmr = container_of(next, struct cpu_timer, node);
@@ -799,6 +803,8 @@  static u64 collect_timerqueue(struct tim
 		/* See posix_cpu_timer_wait_running() */
 		rcu_assign_pointer(ctmr->handling, current);
 		cpu_timer_dequeue(ctmr);
+		ktmr = container_of(ctmr, struct k_itimer, it.cpu);
+		ktmr->it_active = 0;
 		list_add_tail(&ctmr->elist, firing);
 	}