Commit a31a300c authored by Thomas Gleixner's avatar Thomas Gleixner
Browse files

posix-timers: Use guards in a few places



Switch locking and RCU to guards where applicable.

Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
Reviewed-by: default avatarFrederic Weisbecker <frederic@kernel.org>
Link: https://lore.kernel.org/all/20250308155623.892762130@linutronix.de
parent f6d0c3d2
Loading
Loading
Loading
Loading
+30 −38
Original line number Diff line number Diff line
@@ -397,9 +397,8 @@ void posixtimer_free_timer(struct k_itimer *tmr)

static void posix_timer_unhash_and_free(struct k_itimer *tmr)
{
	spin_lock(&hash_lock);
	scoped_guard (spinlock, &hash_lock)
		hlist_del_rcu(&tmr->t_hash);
	spin_unlock(&hash_lock);
	posixtimer_putref(tmr);
}

@@ -443,9 +442,8 @@ static int do_timer_create(clockid_t which_clock, struct sigevent *event,
	new_timer->it_overrun = -1LL;

	if (event) {
		rcu_read_lock();
		scoped_guard (rcu)
			new_timer->it_pid = get_pid(good_sigevent(event));
		rcu_read_unlock();
		if (!new_timer->it_pid) {
			error = -EINVAL;
			goto out;
@@ -579,7 +577,7 @@ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
	 * can't change, but timr::it_signal becomes NULL during
	 * destruction.
	 */
	rcu_read_lock();
	guard(rcu)();
	timr = posix_timer_by_id(timer_id);
	if (timr) {
		spin_lock_irqsave(&timr->it_lock, *flags);
@@ -587,14 +585,10 @@ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
		 * Validate under timr::it_lock that timr::it_signal is
		 * still valid. Pairs with #1 above.
		 */
		if (timr->it_signal == current->signal) {
			rcu_read_unlock();
		if (timr->it_signal == current->signal)
			return timr;
		}
		spin_unlock_irqrestore(&timr->it_lock, *flags);
	}
	rcu_read_unlock();

	return NULL;
}

@@ -825,16 +819,15 @@ static struct k_itimer *timer_wait_running(struct k_itimer *timer,
	timer_t timer_id = READ_ONCE(timer->it_id);

	/* Prevent kfree(timer) after dropping the lock */
	rcu_read_lock();
	scoped_guard (rcu) {
		unlock_timer(timer, *flags);

		/*
		 * kc->timer_wait_running() might drop RCU lock. So @timer
		 * cannot be touched anymore after the function returns!
		 */
		timer->kclock->timer_wait_running(timer);
	}

	rcu_read_unlock();
	/* Relock the timer. It might be not longer hashed. */
	return lock_timer(timer_id, flags);
}
@@ -1020,7 +1013,7 @@ SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
		goto retry_delete;
	}

	spin_lock(&current->sighand->siglock);
	scoped_guard (spinlock, &current->sighand->siglock) {
		hlist_del(&timer->list);
		posix_timer_cleanup_ignored(timer);
		/*
@@ -1033,7 +1026,7 @@ SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
		 * timer to the ignore list.
		 */
		WRITE_ONCE(timer->it_signal, NULL);
	spin_unlock(&current->sighand->siglock);
	}

	unlock_timer(timer, flags);
	posix_timer_unhash_and_free(timer);
@@ -1106,9 +1099,8 @@ void exit_itimers(struct task_struct *tsk)
		return;

	/* Protect against concurrent read via /proc/$PID/timers */
	spin_lock_irq(&tsk->sighand->siglock);
	scoped_guard (spinlock_irq, &tsk->sighand->siglock)
		hlist_move_list(&tsk->signal->posix_timers, &timers);
	spin_unlock_irq(&tsk->sighand->siglock);

	/* The timers are not longer accessible via tsk::signal */
	while (!hlist_empty(&timers)) {