From nobody Fri Dec 19 07:18:45 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D1F24202F9A for ; Sat, 8 Mar 2025 16:48:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741452513; cv=none; b=IjZDxkqzKAfanR2QA1FiN/h1z69ZsybPLDHoLDIpKqcgugPnFRt2dbUPWoDZj8nlYlVsCSgDHiZ6FqO+g0XyXyVXVVEXGCd2GDTv9+x4WYgz+HQveh0twKduT0zHRXrg3Viiv2HBccajpvmemLRZlAFXRVW3v2nz8qpLZ4MlDEA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741452513; c=relaxed/simple; bh=Kikv1cOuZ52fHMF2lOdP2stNkQm++hSxwwV0PI7NR58=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=CLLV3S5zqte5nrjUT3jaI3ryRbeumsYUCnVUMwWYo042vj3OoAYxx/UK77coTSS+wjqRiHHTEJx85irWK9SCBja5azMB+JADx4og80rKtAomFcMZ7A3zCzV1JsB8hFC+5/ZvsuUhqchg9CDinqEfP6odiFt9Rmrwi1m3RgrvPnM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=bibw8EBU; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=lrJEa/Lz; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="bibw8EBU"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="lrJEa/Lz" Message-ID: <20250308155623.892762130@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1741452510; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=2fHIYPPSBTnFA9+ynp17I3Mj7eH6sqePOgXAxOrPoEc=; b=bibw8EBUlnBLK5BmB/v5H87skuJHn3DAu7OlSzT8Bre5TESDMGoDsiTmobXu7h3w2zVXm+ yQuQUvgf8tz8E6sw7vCUoxs8L22jisV3cGU6X5COdism7iSCo5VKMup8MfDwgHoZGvHPTK wCc3Absf5jYNc0JlhYXNEvmxxXdnmtsnDxiqQKho7m3DpEoja0PdyzZnJLrGpjupQQgrIA uVf+XEas+2Zn+6z7YCsREUIZFH6qwTnRhIDL4xVyk+u9naO30rZeEIG2NSlwgPGGjMmeGR i9D+1aqXyRGPevU9xVQ+/ZKmKHXRn3c3yKgftDKFOxxoqxGNz4H1tEW+19Srjg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1741452510; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=2fHIYPPSBTnFA9+ynp17I3Mj7eH6sqePOgXAxOrPoEc=; b=lrJEa/LzFe5MayDiBOFX6bvxOhUQt18ZsjXwB9uYX+ojf065NkchhhJEmvQ104p+ii1cPL g3lMOcZaqiUO2bBA== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , Benjamin Segall , Eric Dumazet , Andrey Vagin , Pavel Tikhomirov , Peter Zijlstra , Cyrill Gorcunov Subject: [patch V3 07/18] posix-timers: Use guards in a few places References: <20250308155501.391430556@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Sat, 8 Mar 2025 17:48:28 +0100 (CET) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Switch locking and RCU to guards where applicable. Signed-off-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker --- V2: New patch --- kernel/time/posix-timers.c | 68 +++++++++++++++++++---------------------= ----- 1 file changed, 30 insertions(+), 38 deletions(-) --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -397,9 +397,8 @@ void posixtimer_free_timer(struct k_itim =20 static void posix_timer_unhash_and_free(struct k_itimer *tmr) { - spin_lock(&hash_lock); - hlist_del_rcu(&tmr->t_hash); - spin_unlock(&hash_lock); + scoped_guard (spinlock, &hash_lock) + hlist_del_rcu(&tmr->t_hash); posixtimer_putref(tmr); } =20 @@ -443,9 +442,8 @@ static int do_timer_create(clockid_t whi new_timer->it_overrun =3D -1LL; =20 if (event) { - rcu_read_lock(); - new_timer->it_pid =3D get_pid(good_sigevent(event)); - rcu_read_unlock(); + scoped_guard (rcu) + new_timer->it_pid =3D get_pid(good_sigevent(event)); if (!new_timer->it_pid) { error =3D -EINVAL; goto out; @@ -579,7 +577,7 @@ static struct k_itimer *__lock_timer(tim * can't change, but timr::it_signal becomes NULL during * destruction. */ - rcu_read_lock(); + guard(rcu)(); timr =3D 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(tim * Validate under timr::it_lock that timr::it_signal is * still valid. Pairs with #1 above. */ - if (timr->it_signal =3D=3D current->signal) { - rcu_read_unlock(); + if (timr->it_signal =3D=3D current->signal) return timr; - } spin_unlock_irqrestore(&timr->it_lock, *flags); } - rcu_read_unlock(); - return NULL; } =20 @@ -825,16 +819,15 @@ static struct k_itimer *timer_wait_runni timer_t timer_id =3D READ_ONCE(timer->it_id); =20 /* Prevent kfree(timer) after dropping the lock */ - rcu_read_lock(); - 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); + 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); + } =20 - rcu_read_unlock(); /* Relock the timer. It might be not longer hashed. */ return lock_timer(timer_id, flags); } @@ -1020,20 +1013,20 @@ SYSCALL_DEFINE1(timer_delete, timer_t, t goto retry_delete; } =20 - spin_lock(¤t->sighand->siglock); - hlist_del(&timer->list); - posix_timer_cleanup_ignored(timer); - /* - * A concurrent lookup could check timer::it_signal lockless. It - * will reevaluate with timer::it_lock held and observe the NULL. - * - * It must be written with siglock held so that the signal code - * observes timer->it_signal =3D=3D NULL in do_sigaction(SIG_IGN), - * which prevents it from moving a pending signal of a deleted - * timer to the ignore list. - */ - WRITE_ONCE(timer->it_signal, NULL); - spin_unlock(¤t->sighand->siglock); + scoped_guard (spinlock, ¤t->sighand->siglock) { + hlist_del(&timer->list); + posix_timer_cleanup_ignored(timer); + /* + * A concurrent lookup could check timer::it_signal lockless. It + * will reevaluate with timer::it_lock held and observe the NULL. + * + * It must be written with siglock held so that the signal code + * observes timer->it_signal =3D=3D NULL in do_sigaction(SIG_IGN), + * which prevents it from moving a pending signal of a deleted + * timer to the ignore list. + */ + WRITE_ONCE(timer->it_signal, NULL); + } =20 unlock_timer(timer, flags); posix_timer_unhash_and_free(timer); @@ -1106,9 +1099,8 @@ void exit_itimers(struct task_struct *ts return; =20 /* Protect against concurrent read via /proc/$PID/timers */ - spin_lock_irq(&tsk->sighand->siglock); - hlist_move_list(&tsk->signal->posix_timers, &timers); - spin_unlock_irq(&tsk->sighand->siglock); + scoped_guard (spinlock_irq, &tsk->sighand->siglock) + hlist_move_list(&tsk->signal->posix_timers, &timers); =20 /* The timers are not longer accessible via tsk::signal */ while (!hlist_empty(&timers)) {