posix-timers: Move rcu_head out of it union
authorSebastian Andrzej Siewior <bigeasy@linutronix.de>
Tue, 30 Jul 2019 22:33:54 +0000 (00:33 +0200)
committerThomas Gleixner <tglx@linutronix.de>
Thu, 1 Aug 2019 18:51:25 +0000 (20:51 +0200)
Timer deletion on PREEMPT_RT is prone to priority inversion and live
locks. The hrtimer code has a synchronization mechanism for this. Posix CPU
timers will grow one.

But that mechanism cannot be invoked while holding the k_itimer lock
because that can deadlock against the running timer callback. So the lock
must be dropped which allows the timer to be freed.

The timer free can be prevented by taking RCU readlock before dropping the
lock, but because the rcu_head is part of the 'it' union a concurrent free
will overwrite the hrtimer on which the task is trying to synchronize.

Move the rcu_head out of the union to prevent this.

[ tglx: Fixed up kernel-doc. Rewrote changelog ]

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20190730223828.965541887@linutronix.de
include/linux/posix-timers.h
kernel/time/posix-timers.c

index b20798fc5191da497f50c602c58f1943218660c2..604cec0e41ba8cd1d6da4c9608785252a4c2cae4 100644 (file)
@@ -85,7 +85,8 @@ static inline int clockid_to_fd(const clockid_t clk)
  * @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.
+ *                     internals.
+ * @rcu:               RCU head for freeing the timer.
  */
 struct k_itimer {
        struct list_head        list;
@@ -114,8 +115,8 @@ struct k_itimer {
                struct {
                        struct alarm    alarmtimer;
                } alarm;
-               struct rcu_head         rcu;
        } it;
+       struct rcu_head         rcu;
 };
 
 void run_posix_cpu_timers(struct task_struct *task);
index bbe8f9686a70c7e5f922473e1cd3bf25f58f4315..3e663f982c82b3114f95d23764ccf8dbfd20d173 100644 (file)
@@ -442,7 +442,7 @@ static struct k_itimer * alloc_posix_timer(void)
 
 static void k_itimer_rcu_free(struct rcu_head *head)
 {
-       struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu);
+       struct k_itimer *tmr = container_of(head, struct k_itimer, rcu);
 
        kmem_cache_free(posix_timers_cache, tmr);
 }
@@ -459,7 +459,7 @@ static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
        }
        put_pid(tmr->it_pid);
        sigqueue_free(tmr->sigq);
-       call_rcu(&tmr->it.rcu, k_itimer_rcu_free);
+       call_rcu(&tmr->rcu, k_itimer_rcu_free);
 }
 
 static int common_timer_create(struct k_itimer *new_timer)