Re: [PATCH] posix-cpu-timers: Implement the missing timer_wait_running callback

From: Frederic Weisbecker
Date: Tue Apr 18 2023 - 12:45:09 EST


Le Mon, Apr 17, 2023 at 03:37:55PM +0200, Thomas Gleixner a écrit :
> struct cpu_timer {
> struct timerqueue_node node;
> @@ -72,6 +74,7 @@ struct cpu_timer {
> struct pid *pid;
> struct list_head elist;
> int firing;
> + struct task_struct *handling;

I guess it can be made __rcu

> };
>
> static inline bool cpu_timer_enqueue(struct timerqueue_head *head,
> @@ -846,6 +846,8 @@ static u64 collect_timerqueue(struct tim
> return expires;
>
> ctmr->firing = 1;
> + /* See posix_cpu_timer_wait_running() */
> + WRITE_ONCE(ctmr->handling, current);

That can be rcu_assign_pointer()

> cpu_timer_dequeue(ctmr);
> list_add_tail(&ctmr->elist, firing);
> }
> @@ -1161,7 +1163,49 @@ static void handle_posix_cpu_timers(stru
> +static void posix_cpu_timer_wait_running(struct k_itimer *timr)
> +{
> + struct task_struct *tsk = READ_ONCE(timr->it.cpu.handling);

And rcu_dereference()

> +
> + /* Has the handling task completed expiry already? */
> + if (!tsk)
> + return;
> +
> + /* Ensure that the task cannot go away */
> + get_task_struct(tsk);
> + /* Now drop the RCU protection so the mutex can be locked */
> + rcu_read_unlock();
> + /* Wait on the expiry mutex */
> + mutex_lock(&tsk->posix_cputimers_work.mutex);
> + /* Release it immediately again. */
> + mutex_unlock(&tsk->posix_cputimers_work.mutex);
> + /* Drop the task reference. */
> + put_task_struct(tsk);
> + /* Relock RCU so the callsite is balanced */
> + rcu_read_lock();
> +}
> @@ -1363,6 +1420,8 @@ static void handle_posix_cpu_timers(stru
> */
> if (likely(cpu_firing >= 0))
> cpu_timer_fire(timer);
> + /* See posix_cpu_timer_wait_running() */
> + WRITE_ONCE(timer->it.cpu.handling, NULL);

And rcu_assign_pointer()

Aside the boring details:

Reviewed-by: Frederic Weisbecker <frederic@xxxxxxxxxx>