Skip to content

Commit 028cf5e

Browse files
committed
posix-timers: Annotate concurrent access to k_itimer:: It_signal
k_itimer::it_signal is read lockless in the RCU protected hash lookup, but it can be written concurrently in the timer_create() and timer_delete() path. Annotate these places with READ_ONCE() and WRITE_ONCE() Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Frederic Weisbecker <frederic@kernel.org> Link: https://lore.kernel.org/r/20230425183313.143596887@linutronix.de
1 parent ae88967 commit 028cf5e

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

kernel/time/posix-timers.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ static struct k_itimer *__posix_timers_find(struct hlist_head *head,
109109
{
110110
struct k_itimer *timer;
111111

112-
hlist_for_each_entry_rcu(timer, head, t_hash,
113-
lockdep_is_held(&hash_lock)) {
114-
if ((timer->it_signal == sig) && (timer->it_id == id))
112+
hlist_for_each_entry_rcu(timer, head, t_hash, lockdep_is_held(&hash_lock)) {
113+
/* timer->it_signal can be set concurrently */
114+
if ((READ_ONCE(timer->it_signal) == sig) && (timer->it_id == id))
115115
return timer;
116116
}
117117
return NULL;
@@ -558,7 +558,7 @@ static int do_timer_create(clockid_t which_clock, struct sigevent *event,
558558

559559
spin_lock_irq(&current->sighand->siglock);
560560
/* This makes the timer valid in the hash table */
561-
new_timer->it_signal = current->signal;
561+
WRITE_ONCE(new_timer->it_signal, current->signal);
562562
list_add(&new_timer->list, &current->signal->posix_timers);
563563
spin_unlock_irq(&current->sighand->siglock);
564564

@@ -1052,10 +1052,10 @@ SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
10521052
list_del(&timer->list);
10531053
spin_unlock(&current->sighand->siglock);
10541054
/*
1055-
* This keeps any tasks waiting on the spin lock from thinking
1056-
* they got something (see the lock code above).
1055+
* A concurrent lookup could check timer::it_signal lockless. It
1056+
* will reevaluate with timer::it_lock held and observe the NULL.
10571057
*/
1058-
timer->it_signal = NULL;
1058+
WRITE_ONCE(timer->it_signal, NULL);
10591059

10601060
unlock_timer(timer, flags);
10611061
release_posix_timer(timer, IT_ID_SET);

0 commit comments

Comments
 (0)