From nobody Thu Nov 28 13:41:55 2024 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 A68D11E4AF for ; Tue, 1 Oct 2024 08:42:02 +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=1727772124; cv=none; b=l6SmsOan5XI7JzrnXaNL2cDKpTblrNUm3FpCl/1xyY2fXPq1iXBTP0MitYD5rrfJhRSr+CBV5E1fUFD1a/j6QvbBjjIaMqT/QATidntk3C2+vsEftTjy6d4yQXBItWOOa9uzGFtgG6LOtpEQc6S04obclf9SVMGJxDCnjM6uf3Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772124; c=relaxed/simple; bh=fhSPKX8PS5jBj1jD/BvvLqlYrYimhgE67tPyDk5gkpw=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=bFGrLGGL7+RzmzotOjSznu8q9qRe4ztJTtk9XpTxapOqAaANTrZThOGcQrlhumsvzmnHP/TTdO4AweqwjxheiCgxeVkvhnL9ldnfSUiH8ixgN+sQox5nGF/gITQjds19kTMmiqxDQvogPcZ4goxs7zy9YPclxkZIemXRY3hiPtk= 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=e7CaYWHV; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=YTw3kA+H; 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="e7CaYWHV"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="YTw3kA+H" Message-ID: <20241001083835.314100569@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772121; 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=jY/Ysxs3ZzGz/myGmp3wKK1jf40clRFR+vHBO2p3aPY=; b=e7CaYWHVFCpmEgah9VALGTh98sYTzvdB9kDyAEJPtgN/URnZJjuVXY94X5Xk0pxWxg2Er7 fWmi922GCJ+U9WCn4KOHmgXuRvcrdK1iqimObdz3QEpWkn+xGpkrwZskxZBkW46nMLN0pK oh4GXE3T0/yrBwY1156uM6K4jbSCXqFt/98+jRRoV1h3ZbwihpTHH6ILGHa+GnS32CBP8H lvpEyTAf2ZZDLs3DTmKDI3d6WwSnM6lxDSBo1e3qy/99NZpJN9SFsYCYypd1wBhJ4p4cWx hYff5shKH2+zfVJMYodcUaieEws+7XDHo11jGFsg08dnJ0uxjSBbMiIxaLDvQQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772121; 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=jY/Ysxs3ZzGz/myGmp3wKK1jf40clRFR+vHBO2p3aPY=; b=YTw3kA+HEpt7MkftscBTp3TCTSe+PpNt5LbOtc6q9eNpvJam/uAPBVvEQogtWivff/BTeG uqnzi+pNEnU6q9Dw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 01/26] signal: Confine POSIX_TIMERS properly References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:00 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner Move the itimer rearming out of the signal code and consolidate all posix timer related functions in the signal code under one ifdef. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- include/linux/posix-timers.h | 5 +- kernel/signal.c | 125 +++++++++++++++------------------------= ----- kernel/time/itimer.c | 22 +++++++- kernel/time/posix-timers.c | 15 ++++- 4 files changed, 81 insertions(+), 86 deletions(-) --- diff --git a/include/linux/posix-timers.h b/include/linux/posix-timers.h index 453691710839..670bf03a56ef 100644 --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -100,6 +100,8 @@ static inline void posix_cputimers_rt_watchdog(struct p= osix_cputimers *pct, { pct->bases[CPUCLOCK_SCHED].nextevt =3D runtime; } +void posixtimer_rearm_itimer(struct task_struct *p); +void posixtimer_rearm(struct kernel_siginfo *info); =20 /* Init task static initializer */ #define INIT_CPU_TIMERBASE(b) { \ @@ -122,6 +124,8 @@ struct cpu_timer { }; static inline void posix_cputimers_init(struct posix_cputimers *pct) { } static inline void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit) { } +static inline void posixtimer_rearm_itimer(struct task_struct *p) { } +static inline void posixtimer_rearm(struct kernel_siginfo *info) { } #endif =20 #ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK @@ -196,5 +200,4 @@ void set_process_cpu_timer(struct task_struct *task, un= signed int clock_idx, =20 int update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new); =20 -void posixtimer_rearm(struct kernel_siginfo *info); #endif diff --git a/kernel/signal.c b/kernel/signal.c index 6f3a5aa39b09..a83ea99f9389 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -478,42 +478,6 @@ void flush_signals(struct task_struct *t) } EXPORT_SYMBOL(flush_signals); =20 -#ifdef CONFIG_POSIX_TIMERS -static void __flush_itimer_signals(struct sigpending *pending) -{ - sigset_t signal, retain; - struct sigqueue *q, *n; - - signal =3D pending->signal; - sigemptyset(&retain); - - list_for_each_entry_safe(q, n, &pending->list, list) { - int sig =3D q->info.si_signo; - - if (likely(q->info.si_code !=3D SI_TIMER)) { - sigaddset(&retain, sig); - } else { - sigdelset(&signal, sig); - list_del_init(&q->list); - __sigqueue_free(q); - } - } - - sigorsets(&pending->signal, &signal, &retain); -} - -void flush_itimer_signals(void) -{ - struct task_struct *tsk =3D current; - unsigned long flags; - - spin_lock_irqsave(&tsk->sighand->siglock, flags); - __flush_itimer_signals(&tsk->pending); - __flush_itimer_signals(&tsk->signal->shared_pending); - spin_unlock_irqrestore(&tsk->sighand->siglock, flags); -} -#endif - void ignore_signals(struct task_struct *t) { int i; @@ -636,31 +600,9 @@ int dequeue_signal(sigset_t *mask, kernel_siginfo_t *i= nfo, enum pid_type *type) *type =3D PIDTYPE_TGID; signr =3D __dequeue_signal(&tsk->signal->shared_pending, mask, info, &resched_timer); -#ifdef CONFIG_POSIX_TIMERS - /* - * itimer signal ? - * - * itimers are process shared and we restart periodic - * itimers in the signal delivery path to prevent DoS - * attacks in the high resolution timer case. This is - * compliant with the old way of self-restarting - * itimers, as the SIGALRM is a legacy signal and only - * queued once. Changing the restart behaviour to - * restart the timer in the signal dequeue path is - * reducing the timer noise on heavy loaded !highres - * systems too. - */ - if (unlikely(signr =3D=3D SIGALRM)) { - struct hrtimer *tmr =3D &tsk->signal->real_timer; - - if (!hrtimer_is_queued(tmr) && - tsk->signal->it_real_incr !=3D 0) { - hrtimer_forward(tmr, tmr->base->get_time(), - tsk->signal->it_real_incr); - hrtimer_restart(tmr); - } - } -#endif + + if (unlikely(signr =3D=3D SIGALRM)) + posixtimer_rearm_itimer(tsk); } =20 recalc_sigpending(); @@ -682,22 +624,12 @@ int dequeue_signal(sigset_t *mask, kernel_siginfo_t *= info, enum pid_type *type) */ current->jobctl |=3D JOBCTL_STOP_DEQUEUED; } -#ifdef CONFIG_POSIX_TIMERS - if (resched_timer) { - /* - * Release the siglock to ensure proper locking order - * of timer locks outside of siglocks. Note, we leave - * irqs disabled here, since the posix-timers code is - * about to disable them again anyway. - */ - spin_unlock(&tsk->sighand->siglock); - posixtimer_rearm(info); - spin_lock(&tsk->sighand->siglock); =20 - /* Don't expose the si_sys_private value to userspace */ - info->si_sys_private =3D 0; + if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { + if (unlikely(resched_timer)) + posixtimer_rearm(info); } -#endif + return signr; } EXPORT_SYMBOL_GPL(dequeue_signal); @@ -1922,15 +1854,43 @@ int kill_pid(struct pid *pid, int sig, int priv) } EXPORT_SYMBOL(kill_pid); =20 +#ifdef CONFIG_POSIX_TIMERS /* - * These functions support sending signals using preallocated sigqueue - * structures. This is needed "because realtime applications cannot - * afford to lose notifications of asynchronous events, like timer - * expirations or I/O completions". In the case of POSIX Timers - * we allocate the sigqueue structure from the timer_create. If this - * allocation fails we are able to report the failure to the application - * with an EAGAIN error. + * These functions handle POSIX timer signals. POSIX timers use + * preallocated sigqueue structs for sending signals. */ +static void __flush_itimer_signals(struct sigpending *pending) +{ + sigset_t signal, retain; + struct sigqueue *q, *n; + + signal =3D pending->signal; + sigemptyset(&retain); + + list_for_each_entry_safe(q, n, &pending->list, list) { + int sig =3D q->info.si_signo; + + if (likely(q->info.si_code !=3D SI_TIMER)) { + sigaddset(&retain, sig); + } else { + sigdelset(&signal, sig); + list_del_init(&q->list); + __sigqueue_free(q); + } + } + + sigorsets(&pending->signal, &signal, &retain); +} + +void flush_itimer_signals(void) +{ + struct task_struct *tsk =3D current; + + guard(spinlock_irqsave)(&tsk->sighand->siglock); + __flush_itimer_signals(&tsk->pending); + __flush_itimer_signals(&tsk->signal->shared_pending); +} + struct sigqueue *sigqueue_alloc(void) { return __sigqueue_alloc(-1, current, GFP_KERNEL, 0, SIGQUEUE_PREALLOC); @@ -2027,6 +1987,7 @@ int send_sigqueue(struct sigqueue *q, struct pid *pid= , enum pid_type type) rcu_read_unlock(); return ret; } +#endif /* CONFIG_POSIX_TIMERS */ =20 void do_notify_pidfd(struct task_struct *task) { diff --git a/kernel/time/itimer.c b/kernel/time/itimer.c index 00629e658ca1..876d389b2e21 100644 --- a/kernel/time/itimer.c +++ b/kernel/time/itimer.c @@ -151,7 +151,27 @@ COMPAT_SYSCALL_DEFINE2(getitimer, int, which, #endif =20 /* - * The timer is automagically restarted, when interval !=3D 0 + * Invoked from dequeue_signal() when SIG_ALRM is delivered. + * + * Restart the ITIMER_REAL timer if it is armed as periodic timer. Doing + * this in the signal delivery path instead of self rearming prevents a DoS + * with small increments in the high reolution timer case and reduces timer + * noise in general. + */ +void posixtimer_rearm_itimer(struct task_struct *tsk) +{ + struct hrtimer *tmr =3D &tsk->signal->real_timer; + + if (!hrtimer_is_queued(tmr) && tsk->signal->it_real_incr !=3D 0) { + hrtimer_forward(tmr, tmr->base->get_time(), + tsk->signal->it_real_incr); + hrtimer_restart(tmr); + } +} + +/* + * Interval timers are restarted in the signal delivery path. See + * posixtimer_rearm_itimer(). */ enum hrtimer_restart it_real_fn(struct hrtimer *timer) { diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 1cc830ef93a7..bcd5e56412e7 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -251,7 +251,7 @@ static void common_hrtimer_rearm(struct k_itimer *timr) =20 /* * This function is called from the signal delivery code if - * info->si_sys_private is not zero, which indicates that the timer has to + * info::si_sys_private is not zero, which indicates that the timer has to * be rearmed. Restart the timer and update info::si_overrun. */ void posixtimer_rearm(struct kernel_siginfo *info) @@ -259,9 +259,15 @@ void posixtimer_rearm(struct kernel_siginfo *info) struct k_itimer *timr; unsigned long flags; =20 + /* + * Release siglock to ensure proper locking order versus + * timr::it_lock. Keep interrupts disabled. + */ + spin_unlock(¤t->sighand->siglock); + timr =3D lock_timer(info->si_tid, &flags); if (!timr) - return; + goto out; =20 if (timr->it_interval && timr->it_requeue_pending =3D=3D info->si_sys_pri= vate) { timr->kclock->timer_rearm(timr); @@ -275,6 +281,11 @@ void posixtimer_rearm(struct kernel_siginfo *info) } =20 unlock_timer(timr, flags); +out: + spin_lock(¤t->sighand->siglock); + + /* Don't expose the si_sys_private value to userspace */ + info->si_sys_private =3D 0; } =20 int posix_timer_queue_signal(struct k_itimer *timr) From nobody Thu Nov 28 13:41:55 2024 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 4773C19C565 for ; Tue, 1 Oct 2024 08:42:04 +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=1727772125; cv=none; b=oxP/s8/YBgRNutmZOWANoSD+nti4LKrRcBqLKMZ1DZHHaM1S5KooSnCr1w4LK7t3V8nkFPxnqSHzQ5735er44xZrvd99meIJ+TrbTFCRSyZauye3ACTBM5AaJN7JJzu3JwPq65KzjTVNJ2vesZmtsUlVw8E+qY1lXESSvkf99Xo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772125; c=relaxed/simple; bh=M3fXAdRWxuZ07Q0D267FKx6zLSR7I6ET0k9tQy3SdSk=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=BbD7ZVpqq9Gn+NN84oBVyPZfiYPZEzHBQQVzvjElHgKNOhe12j4uhfN6yV+7k0C1edVTNEfNUIT/aewBYX5zU2ZGw4kxrzakE48yEujLePvdUyJ3C44NipsOfZWGf2Bn+RIDlKBlzhXKxQl7/yRmkGRapjTviy5oUqSTdZrHwLE= 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=aGq7vSxE; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=tWvzEeVi; 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="aGq7vSxE"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="tWvzEeVi" Message-ID: <20241001083835.374933959@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772122; 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=6LPrqCS5pkWr7LC6gMgNz6vB8PRM7Rz4RG9MQ7Cuv3Q=; b=aGq7vSxEHV2S6x0lsh8cOa4eDefHjxTgjC96myAyVJlzlNkEWMv3YqgXQnVjIkrO4+07Rh QmI8DSYmya+0DIYrmrtkJeHFrPDuEzxvSgqeVgKNn5DB3X04l3r99t+iBRRiuLeVNertZ3 Z4EWJZMhsuTnMHqSGgXcpqgPms0fYCUitwoF/3+MGU/aWLGRRsJmPWjdJvehfDo/G4QXIE QjgjR/BBeGnQiU/eblWf6J6KlJ8uDQs/50uBcp40cZhqfzlQOAxDS58Hc9s+ArwNxU9Tx3 wtqzPdCftd4+BMv4RD/EA1rKRJ9O5vg5ctnSDZDZcj57XJ9gJscQFnKQQ6kmog== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772122; 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=6LPrqCS5pkWr7LC6gMgNz6vB8PRM7Rz4RG9MQ7Cuv3Q=; b=tWvzEeVixAPN+ZPsX67fieON/DPhkCqJn6UKvWaBte1KsPkHqDZcnDoXvOvp9QE4PK3RlS +B3901mm+NyPToCQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 02/26] signal: Cleanup flush_sigqueue_mask() References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:02 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Mop up the stale return value comment and add a lockdep check instead of commenting on the locking requirement. Signed-off-by: Thomas Gleixner Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- kernel/signal.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) --- a/kernel/signal.c +++ b/kernel/signal.c @@ -704,17 +704,14 @@ void signal_wake_up_state(struct task_st kick_process(t); } =20 -/* - * Remove signals in mask from the pending set and queue. - * Returns 1 if any signals were found. - * - * All callers must be holding the siglock. - */ -static void flush_sigqueue_mask(sigset_t *mask, struct sigpending *s) +/* Remove signals in mask from the pending set and queue. */ +static void flush_sigqueue_mask(struct task_struct *p, sigset_t *mask, str= uct sigpending *s) { struct sigqueue *q, *n; sigset_t m; =20 + lockdep_assert_held(&p->sighand->siglock); + sigandsets(&m, mask, &s->signal); if (sigisemptyset(&m)) return; @@ -848,18 +845,18 @@ static bool prepare_signal(int sig, stru * This is a stop signal. Remove SIGCONT from all queues. */ siginitset(&flush, sigmask(SIGCONT)); - flush_sigqueue_mask(&flush, &signal->shared_pending); + flush_sigqueue_mask(p, &flush, &signal->shared_pending); for_each_thread(p, t) - flush_sigqueue_mask(&flush, &t->pending); + flush_sigqueue_mask(p, &flush, &t->pending); } else if (sig =3D=3D SIGCONT) { unsigned int why; /* * Remove all stop signals from all queues, wake all threads. */ siginitset(&flush, SIG_KERNEL_STOP_MASK); - flush_sigqueue_mask(&flush, &signal->shared_pending); + flush_sigqueue_mask(p, &flush, &signal->shared_pending); for_each_thread(p, t) { - flush_sigqueue_mask(&flush, &t->pending); + flush_sigqueue_mask(p, &flush, &t->pending); task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING); if (likely(!(t->ptrace & PT_SEIZED))) { t->jobctl &=3D ~JOBCTL_STOPPED; @@ -4114,8 +4111,8 @@ void kernel_sigaction(int sig, __sighand sigemptyset(&mask); sigaddset(&mask, sig); =20 - flush_sigqueue_mask(&mask, ¤t->signal->shared_pending); - flush_sigqueue_mask(&mask, ¤t->pending); + flush_sigqueue_mask(current, &mask, ¤t->signal->shared_pending); + flush_sigqueue_mask(current, &mask, ¤t->pending); recalc_sigpending(); } spin_unlock_irq(¤t->sighand->siglock); @@ -4182,9 +4179,9 @@ int do_sigaction(int sig, struct k_sigac if (sig_handler_ignored(sig_handler(p, sig), sig)) { sigemptyset(&mask); sigaddset(&mask, sig); - flush_sigqueue_mask(&mask, &p->signal->shared_pending); + flush_sigqueue_mask(p, &mask, &p->signal->shared_pending); for_each_thread(p, t) - flush_sigqueue_mask(&mask, &t->pending); + flush_sigqueue_mask(p, &mask, &t->pending); } } From nobody Thu Nov 28 13:41:55 2024 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 32CB419CC10 for ; Tue, 1 Oct 2024 08:42:05 +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=1727772126; cv=none; b=DErwMTp3M4mn7+9dyz3ATAjnmii6c3KTwCP6txl95oLqGa+XSJp4pKA3be22Yrd4GE2lw8Grx5l8laxg6zSO2XwuoxRIN30B7mUq968gcZMJG3rdpg7oln5fSngr+2QOnHV48ZqS/6xXKe1r0vV5nKDQeTDC8RgpxB3fsupkPdg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772126; c=relaxed/simple; bh=XTy2ZNeK7EMPqchjlUR0zI191FYgGBIFUYbh/0+ygwY=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=FOEJNZxEl4T2rqCswGAOKPpHF4sbWgpA1b7r0OdOEEHM6rzJjyLuyEHbITju/UT12Vgg3rpahx/julxw7nm6E1Dw9equMPlsv1p0nZ5ThLfTO9p5YiXA/1eZuYqZGu48PznKroQAYzlhWIKsu0ecFVfptJzeaSyx4PxytFfnl6g= 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=a6NLIMbL; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=IzQgrbYT; 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="a6NLIMbL"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="IzQgrbYT" Message-ID: <20241001083835.434338954@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772123; 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=SdD1XuzyyTjiv1VlRVUmflvcPUaAwKIRKorXREyi9yI=; b=a6NLIMbLV/HGNFDTHVe0xaQrBxzWf5xY17zba1l5FNhd4n+v3j4mpiPD2xvqmhVSZftmGk CVSSG53xSUi6gBM+lBiDScdFau0DnErVRe92Gh05jYQWdMJWsyjMwro10g/pGYUXCBrSzv 2cMAnBJ35wsEv3yGgFjkD+ongGbQAe+RrZZfzNBhsvGs/WrTB9qj7VdKUjDHwcD0lSq6dH +H69hxWDsg3Jb91qM3gPcCirdd6UX+kRCp2dKQmaJqaQ4FHk0+OVkDAbM9EMQm2C/PIuHx 9s9cCrqtcBBIGmIfU3aFcFi71D0gQ3RsryXIEIidFijmVfG6NiGJzSRFaRgQXg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772123; 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=SdD1XuzyyTjiv1VlRVUmflvcPUaAwKIRKorXREyi9yI=; b=IzQgrbYTUpNzLIDZbFcRssUkpTsVh6Jc3VMmC3qY0AYPsMy/ky/PljfSscfArQKE+jkx6Y 5hKU+YmbQUllloDQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 03/26] posix-timers: Cure si_sys_private race References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:03 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner The si_sys_private member of the siginfo which is embedded in the preallocated sigqueue is used by the posix timer code to decide whether a timer must be reprogrammed on signal delivery. The handling of this is racy as a long standing comment in that code documents. It is modified with the timer lock held, but without sighand lock being held. The actual signal delivery code checks for it under sighand lock without holding the timer lock. Hand the new value to send_sigqueue() as argument and store it with sighand lock held. This is an intermediate change to address this issue. The arguments to this function will be cleanup in subsequent changes. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- V5: Document in the change log that this is intermediate. --- include/linux/sched/signal.h | 2 +- kernel/signal.c | 10 +++++++++- kernel/time/posix-timers.c | 15 +-------------- 3 files changed, 11 insertions(+), 16 deletions(-) --- --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -340,7 +340,7 @@ extern int send_sig(int, struct task_str extern int zap_other_threads(struct task_struct *p); extern struct sigqueue *sigqueue_alloc(void); extern void sigqueue_free(struct sigqueue *); -extern int send_sigqueue(struct sigqueue *, struct pid *, enum pid_type); +extern int send_sigqueue(struct sigqueue *, struct pid *, enum pid_type, i= nt si_private); extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *); =20 static inline void clear_notify_signal(void) --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1919,7 +1919,7 @@ void sigqueue_free(struct sigqueue *q) __sigqueue_free(q); } =20 -int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type) +int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type,= int si_private) { int sig =3D q->info.si_signo; struct sigpending *pending; @@ -1954,6 +1954,14 @@ int send_sigqueue(struct sigqueue *q, st if (!likely(lock_task_sighand(t, &flags))) goto ret; =20 + /* + * Update @q::info::si_sys_private for posix timer signals with + * sighand locked to prevent a race against dequeue_signal() which + * decides based on si_sys_private whether to invoke + * posixtimer_rearm() or not. + */ + q->info.si_sys_private =3D si_private; + ret =3D 1; /* the signal is ignored */ result =3D TRACE_SIGNAL_IGNORED; if (!prepare_signal(sig, t, false)) --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -299,21 +299,8 @@ int posix_timer_queue_signal(struct k_it if (timr->it_interval) si_private =3D ++timr->it_requeue_pending; =20 - /* - * FIXME: if ->sigq is queued we can race with - * dequeue_signal()->posixtimer_rearm(). - * - * If dequeue_signal() sees the "right" value of - * si_sys_private it calls posixtimer_rearm(). - * We re-queue ->sigq and drop ->it_lock(). - * posixtimer_rearm() locks the timer - * and re-schedules it while ->sigq is pending. - * Not really bad, but not that we want. - */ - timr->sigq->info.si_sys_private =3D si_private; - type =3D !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDT= YPE_PID; - ret =3D send_sigqueue(timr->sigq, timr->it_pid, type); + ret =3D send_sigqueue(timr->sigq, timr->it_pid, type, si_private); /* If we failed to send the signal the timer stops. */ return ret > 0; } From nobody Thu Nov 28 13:41:55 2024 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 D612919D089 for ; Tue, 1 Oct 2024 08:42:06 +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=1727772128; cv=none; b=GznxZ5I571tcdpIzk6BNT5m20E5JW3w3oX+IyFKSMggF3/2GVD9sVdaHDM85Ky0dKBGMt43z2YIUUr4wJNonzyqp0HLwnvt6R3/v47cJFGT8okEXtSK1LSv6oPcEmo+6Xxf4Os962V3GQpIR95UilZGWzlClJdw++Ty2XC1Q3Xc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772128; c=relaxed/simple; bh=i2LJtxWLqFkcIAuw/Qy0eM1iRXPz0nzC5BS3WiF7Ya8=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=N0t5JEryCpP/rgzoKiPVAEZR7yZdL0Mb0/WaWB3bgpn55Kl7DGKewz6hMBR7rU3PtigT2y2EOi1k1H2AWdJ9fW29O+/CdtyswJeLAGKnFs85ovQO3qDgVGmewTaJy7nJLaSpCRGpeucr5FI+aNalPPktXRi158HgJ6KtwB3SX9M= 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=UUMsG9ov; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=v3kQm0vV; 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="UUMsG9ov"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="v3kQm0vV" Message-ID: <20241001083835.494416923@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772125; 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=bMfQs4RXzVSNN9s6N1/ETgB73QLiI7DZqMzM0+UeQF4=; b=UUMsG9ovnGgSJ4bZ20MkW0l5JbcbHtMsxgUODdkRjJ9+VdgTysqjtOdIxIUrGGrYva4+II 8lpG1UM8YqRq/ASUQ7VX2Dy8VxXEjzZvV850Brv5RtFNKXLanlpS6tKwSUElu+8QxltDRo tHPB5cXXr8Qborxt3+FD7xBIZJhDmheptdR+fip7UoZ/AWONsSSGIOexrx/LZOlVoR0/se 1/FQB/HEuLees6u69pho3Sm5pc4ZKBGfUI8yYNJRyA7z2Wc3wj+p5ahRvbYQ3KsJ0Rbj4q OwmqlalEBsfmZUfYfn1+7CU2tVsTKm9qN2bgq9u96MFGzIH/+cLojo6ErU3vvA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772125; 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=bMfQs4RXzVSNN9s6N1/ETgB73QLiI7DZqMzM0+UeQF4=; b=v3kQm0vVmRdlFvGo+UD6dGD5BqLT0HyAHvXUXTFAyXjMMZ0VnFxLEQAYLo6k35N9KopYAL 8fslsuETvTe0joDg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 04/26] signal: Allow POSIX timer signals to be dropped References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:04 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner In case that a timer was reprogrammed or deleted an already pending signal is obsolete. Right now such signals are kept around and eventually delivered. While POSIX is blury about this: - "The effect of disarming or resetting a timer with pending expiration notifications is unspecified." - "The disposition of pending signals for the deleted timer is unspecified." it is reasonable in both cases to expect that pending signals are discarded as they have no meaning anymore. Prepare the signal code to allow dropping posix timer signals. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- include/linux/posix-timers.h | 5 +++-- kernel/signal.c | 7 ++++--- kernel/time/posix-timers.c | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-) --- --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -100,8 +100,9 @@ static inline void posix_cputimers_rt_wa { pct->bases[CPUCLOCK_SCHED].nextevt =3D runtime; } + void posixtimer_rearm_itimer(struct task_struct *p); -void posixtimer_rearm(struct kernel_siginfo *info); +bool posixtimer_deliver_signal(struct kernel_siginfo *info); =20 /* Init task static initializer */ #define INIT_CPU_TIMERBASE(b) { \ @@ -125,7 +126,7 @@ static inline void posix_cputimers_init( static inline void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit) { } static inline void posixtimer_rearm_itimer(struct task_struct *p) { } -static inline void posixtimer_rearm(struct kernel_siginfo *info) { } +static inline bool posixtimer_deliver_signal(struct kernel_siginfo *info) = { return false; } #endif =20 #ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK --- a/kernel/signal.c +++ b/kernel/signal.c @@ -594,6 +594,7 @@ int dequeue_signal(sigset_t *mask, kerne =20 lockdep_assert_held(&tsk->sighand->siglock); =20 +again: *type =3D PIDTYPE_PID; signr =3D __dequeue_signal(&tsk->pending, mask, info, &resched_timer); if (!signr) { @@ -625,9 +626,9 @@ int dequeue_signal(sigset_t *mask, kerne current->jobctl |=3D JOBCTL_STOP_DEQUEUED; } =20 - if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { - if (unlikely(resched_timer)) - posixtimer_rearm(info); + if (IS_ENABLED(CONFIG_POSIX_TIMERS) && unlikely(resched_timer)) { + if (!posixtimer_deliver_signal(info)) + goto again; } =20 return signr; --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -254,7 +254,7 @@ static void common_hrtimer_rearm(struct * info::si_sys_private is not zero, which indicates that the timer has to * be rearmed. Restart the timer and update info::si_overrun. */ -void posixtimer_rearm(struct kernel_siginfo *info) +bool posixtimer_deliver_signal(struct kernel_siginfo *info) { struct k_itimer *timr; unsigned long flags; @@ -286,6 +286,7 @@ void posixtimer_rearm(struct kernel_sigi =20 /* Don't expose the si_sys_private value to userspace */ info->si_sys_private =3D 0; + return true; } =20 int posix_timer_queue_signal(struct k_itimer *timr) From nobody Thu Nov 28 13:41:55 2024 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 0E7FB19D896 for ; Tue, 1 Oct 2024 08:42:08 +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=1727772129; cv=none; b=h8ez9+UkVsHfxq9dXoqre3puN/c5fecHgz4uv6OepnGs3Lc5J5o5FSdDO89cqWIzYpq3VqHPFRxr4KI6ZtfTwmq9DkK79uGjlYBM7almoh2GvhIWwa2RK22yyLa7moPQp2oo1m9azJynZC1gFDzBHquOAIEc4NjmLgVA+UTf1RQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772129; c=relaxed/simple; bh=lbAJsxEIBuEi5j1fxs6XlVgx5F/bE/5BIwwAUQBVOy8=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=HzMNtPMVPMo8zroJd92Capt8h3xD26f/fEihbkmn2JXNELSmuzMDbjOM4fJEBEkV00P3gJfCv1RJKUbL315HgP8CofdSFSfe51mUD/np3myaqS3Z7ve5o7BeMWPt33G0B7a/nbFetzAY9OChzfvCQrT6zYy7g1NmRj94MkFgyRs= 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=DPQjWPZl; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=6Fzl19jD; 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="DPQjWPZl"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="6Fzl19jD" Message-ID: <20241001083835.553646280@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772126; 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=5PUBsJTvJ1KFQG92jonGbwEiSpgHpeT/5KITtDrmIx4=; b=DPQjWPZlLfZ1hi4UA+/Q0yu35uFVsLCUcvqKvlebPnATvGhm+6M6P7ejGlPfmFkT7lTyGR DcVj0jGfPxyVp4jVxQw6bII6cTawGbXEKH2VoYgD6g2daNDGe5aQ9NWKBhHs6+NUtBPSpg YoC+AeeHquPJSZleHFyjQ7+YO36wD0k2u80xu139TDarhXzIAQEbjHK1i0MFLVcsGBOVpI DK2XVYglM/8tly+xdwpiryMh2f5efKuO2WAyspZmJgu8Tb0YSMnRrK0vCIBP7FZ4cR193W jooTeUQQjM7mJIDAZUFQ6i553mus2ZP0dWzgtEtH6+G1wSshxQFP83vKWM/M2Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772126; 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=5PUBsJTvJ1KFQG92jonGbwEiSpgHpeT/5KITtDrmIx4=; b=6Fzl19jDE8EcZl/Lgh9d6HVkLI8SNke7RvmP4BXJ3asN2+L2wyCOmynufUFiNf6TKLb6M2 Ci8J8hUGSRcUqMDA== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 05/26] posix-timers: Drop signal if timer has been deleted or reprogrammed References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:06 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner No point in delivering a signal from the past. POSIX does not specify the behaviour here: - "The effect of disarming or resetting a timer with pending expiration notifications is unspecified." - "The disposition of pending signals for the deleted timer is unspecified= ." In both cases it is reasonable to expect that pending signals are discarded. Especially in the reprogramming case it does not make sense to account for previous overruns or to deliver a signal for a timer which has been disarmed. Drop the signal as that is conistent and understandable behaviour. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- kernel/time/posix-timers.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) --- diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index d7ed7542f803..b5d7e71c10f2 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -250,14 +250,14 @@ static void common_hrtimer_rearm(struct k_itimer *tim= r) } =20 /* - * This function is called from the signal delivery code if - * info::si_sys_private is not zero, which indicates that the timer has to - * be rearmed. Restart the timer and update info::si_overrun. + * This function is called from the signal delivery code. It decides + * whether the signal should be dropped and rearms interval timers. */ bool posixtimer_deliver_signal(struct kernel_siginfo *info) { struct k_itimer *timr; unsigned long flags; + bool ret =3D false; =20 /* * Release siglock to ensure proper locking order versus @@ -279,6 +279,7 @@ bool posixtimer_deliver_signal(struct kernel_siginfo *i= nfo) =20 info->si_overrun =3D timer_overrun_to_int(timr, info->si_overrun); } + ret =3D true; =20 unlock_timer(timr, flags); out: @@ -286,7 +287,7 @@ bool posixtimer_deliver_signal(struct kernel_siginfo *i= nfo) =20 /* Don't expose the si_sys_private value to userspace */ info->si_sys_private =3D 0; - return true; + return ret; } =20 int posix_timer_queue_signal(struct k_itimer *timr) From nobody Thu Nov 28 13:41:55 2024 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 803B319E80A for ; Tue, 1 Oct 2024 08:42:09 +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=1727772132; cv=none; b=Gyf2fDCXbqUWZhSpffcD/7nrMgaRs/aonIZQTX+/BB66XiozeXfQijOeElbplnjSlZbgvxJAkRzxP3Ht6RAdW7yC5mybMFaT8tXAiH4RuVfysYQCmAgO5AJpbvz9FU+RawUo8SiGJEa9KxxvALXcg1h6N42vdFIDSLU+coFF1Uc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772132; c=relaxed/simple; bh=7cvYyslHnX2Jf275TKsBgbpcsrgLg3w2OAOMpNyZnqg=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=V5BS+I1DZiiLSeTpzh8Bm6e90maBCjZihzjyOlUMS6CYXD1T97l9EfK5q2+NGzCkZpR4UiJFyIE6B8WvXTE+2UaKEfLP6VlT6qNDSUsJ5hp6n4N7EH2Nu1E6sKWIRrxYDNO+k/26aLjAORfHOjweD6xIMYYrnBAgBc1S3GwOZoU= 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=j1h9501R; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=2x2JEmA3; 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="j1h9501R"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="2x2JEmA3" Message-ID: <20241001083835.611997737@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772128; 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=d6JevVhVI/1VH0mDd9yh89FDebBHZv3TZQSw7wec8CU=; b=j1h9501RZ3TzQEfnOonf+mwKCiJkXKZpCRmtwqxM4fOS8Gi8e2j+Spl90eNpBWQgwNoSHd j7PUqgQv3ZfpqMu2KqPCIi+rwR9Rao+/UdQ0PePojIXoI1avpU02WYtWO5MBWmzizdgFGJ pKr2BP7yNCIhuopLkr2FYM4BZnLYnBKB3aVF1KnMxdlqYBHJRvRWjK0bQZVJtaTQxX0GSM PP2d75MFMWQDg4T8Pg5xs8sEHkPjfmtd4FxEjT1o4xQWlk8fuFJ+sfBoKgtvlKoRpvHntW hgeM6KTAWJYR1pKISoUtLZp/aWGc22o/GahUmmL2HjsfoJ89rDbPiFbHeZ8KrA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772128; 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=d6JevVhVI/1VH0mDd9yh89FDebBHZv3TZQSw7wec8CU=; b=2x2JEmA3R4kl2mI1FLbuRQaqcEcg1aD3qrJLzed/bGMcNQr7puTycupK25WIeQasXTQe4n M81BWu/ziALqnBCg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 06/26] posix-timers: Rename k_itimer::it_requeue_pending References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:07 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner Prepare for using this struct member to do a proper reprogramming and deletion accounting so that stale signals can be dropped. No functional change. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- include/linux/posix-timers.h | 5 ++--- kernel/time/alarmtimer.c | 2 +- kernel/time/posix-cpu-timers.c | 4 ++-- kernel/time/posix-timers.c | 12 ++++++------ 4 files changed, 11 insertions(+), 12 deletions(-) --- diff --git a/include/linux/posix-timers.h b/include/linux/posix-timers.h index 4ab49e5c42af..253d106fac2c 100644 --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -150,8 +150,7 @@ static inline void posix_cputimers_init_work(void) { } * @it_active: Marker that timer is active * @it_overrun: The overrun counter for pending signals * @it_overrun_last: The overrun at the time of the last delivered signal - * @it_requeue_pending: Indicator that timer waits for being requeued on - * signal delivery + * @it_signal_seq: Sequence count to control signal delivery * @it_sigev_notify: The notify word of sigevent struct for signal delivery * @it_interval: The interval for periodic timers * @it_signal: Pointer to the creators signal struct @@ -172,7 +171,7 @@ struct k_itimer { int it_active; s64 it_overrun; s64 it_overrun_last; - int it_requeue_pending; + unsigned int it_signal_seq; int it_sigev_notify; ktime_t it_interval; struct signal_struct *it_signal; diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c index 76bd4fda3472..22d5145dd9a7 100644 --- a/kernel/time/alarmtimer.c +++ b/kernel/time/alarmtimer.c @@ -584,7 +584,7 @@ static enum alarmtimer_restart alarm_handle_timer(struc= t alarm *alarm, * small intervals cannot starve the system. */ ptr->it_overrun +=3D __alarm_forward_now(alarm, ptr->it_interval, true); - ++ptr->it_requeue_pending; + ++ptr->it_signal_seq; ptr->it_active =3D 1; result =3D ALARMTIMER_RESTART; } diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c index 6bcee4704059..993243b5be98 100644 --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -608,7 +608,7 @@ static void cpu_timer_fire(struct k_itimer *timer) * ticking in case the signal is deliverable next time. */ posix_cpu_timer_rearm(timer); - ++timer->it_requeue_pending; + ++timer->it_signal_seq; } } =20 @@ -745,7 +745,7 @@ static void __posix_cpu_timer_get(struct k_itimer *time= r, struct itimerspec64 *i * - Timers which expired, but the signal has not yet been * delivered */ - if (iv && ((timer->it_requeue_pending & REQUEUE_PENDING) || sigev_none)) + if (iv && ((timer->it_signal_seq & REQUEUE_PENDING) || sigev_none)) expires =3D bump_cpu_timer(timer, now); else expires =3D cpu_timer_getexpires(&timer->it.cpu); diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index b5d7e71c10f2..26243d38d27d 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -269,13 +269,13 @@ bool posixtimer_deliver_signal(struct kernel_siginfo = *info) if (!timr) goto out; =20 - if (timr->it_interval && timr->it_requeue_pending =3D=3D info->si_sys_pri= vate) { + if (timr->it_interval && timr->it_signal_seq =3D=3D info->si_sys_private)= { timr->kclock->timer_rearm(timr); =20 timr->it_active =3D 1; timr->it_overrun_last =3D timr->it_overrun; timr->it_overrun =3D -1LL; - ++timr->it_requeue_pending; + ++timr->it_signal_seq; =20 info->si_overrun =3D timer_overrun_to_int(timr, info->si_overrun); } @@ -299,7 +299,7 @@ int posix_timer_queue_signal(struct k_itimer *timr) =20 timr->it_active =3D 0; if (timr->it_interval) - si_private =3D ++timr->it_requeue_pending; + si_private =3D ++timr->it_signal_seq; =20 type =3D !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDT= YPE_PID; ret =3D send_sigqueue(timr->sigq, timr->it_pid, type, si_private); @@ -366,7 +366,7 @@ static enum hrtimer_restart posix_timer_fn(struct hrtim= er *timer) =20 timr->it_overrun +=3D hrtimer_forward(timer, now, timr->it_interval); ret =3D HRTIMER_RESTART; - ++timr->it_requeue_pending; + ++timr->it_signal_seq; timr->it_active =3D 1; } } @@ -667,7 +667,7 @@ void common_timer_get(struct k_itimer *timr, struct iti= merspec64 *cur_setting) * is a SIGEV_NONE timer move the expiry time forward by intervals, * so expiry is > now. */ - if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none)) + if (iv && (timr->it_signal_seq & REQUEUE_PENDING || sig_none)) timr->it_overrun +=3D kc->timer_forward(timr, now); =20 remaining =3D kc->timer_remaining(timr, now); @@ -868,7 +868,7 @@ void posix_timer_set_common(struct k_itimer *timer, str= uct itimerspec64 *new_set timer->it_interval =3D 0; =20 /* Prevent reloading in case there is a signal pending */ - timer->it_requeue_pending =3D (timer->it_requeue_pending + 2) & ~REQUEUE_= PENDING; + timer->it_signal_seq =3D (timer->it_signal_seq + 2) & ~REQUEUE_PENDING; /* Reset overrun accounting */ timer->it_overrun_last =3D 0; timer->it_overrun =3D -1LL; From nobody Thu Nov 28 13:41:55 2024 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 245B019DF9F for ; Tue, 1 Oct 2024 08:42:10 +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=1727772132; cv=none; b=Ag0OixWoC5WKxifDaPWqDXAATORbOmU93z/eLBoP03kPuGS7SWs63w6O3qhZusgDQhxvDTKrkcECs5JzY/uC/XUUy3y3ezEZwlO6fIWOp+9V6d0h1L6nD5hHI5/Cqcl1WHREI3oGevdByOgXGdJVifTH9oelPQKq7VTdj2PG77A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772132; c=relaxed/simple; bh=clViyTAEZoc01OFi7/aP1Tt8qw0WNjRvu7qqdwmShh8=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=OU351Mkky5xcPZhMybU3ZGLbF9Z3dkk5WQW+kcRwGF9YKTiKjz8oreooW9IBxNtEqRHOLDGEG3WstiZDBhykvSWsssgfWxKZqea7fZiQAVn7ZayutiQ1CDIGfxtVrYk/hVFt7/flsPZ6CjLRJrsoE55jomw9hB/exPDMnoUHv9c= 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=06BWc3rN; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=kMjvoT2x; 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="06BWc3rN"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="kMjvoT2x" Message-ID: <20241001083835.670337048@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772129; 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=NKyH6fMg17+OFdHjowqTtHauTTI5dnbrUbSQxwMduOE=; b=06BWc3rN978Fv0qaRejiYMspjTU6FC+HROCkR4IoTFe/Q+ouSqNRBMuGiec3LMwS51dUbN uKaK3UoNQyN0qSmF1Jr28eTd3YRWGnkD3Sh8JPXKfZZwCzYjIp2spY3aWzo/VA6y26sfVt /c+lHANeRHuIoWBqrXE2gZhCwB+ZyeNCeJonFpZrUbXQzybSWrbaVXHQZt0hN2j+739sH9 XySdQJTo9076VUxjS3+7xCsfyKl0mjTSV/vndpSO89Gc0QuosNyvCzMEO6EuqqTyDnyC4A 7YO78t/QUAgKtxRn+1EA/4hd/6xb92geqI8uvoCKaJK+y+C5vQgebHVsSFAQqA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772129; 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=NKyH6fMg17+OFdHjowqTtHauTTI5dnbrUbSQxwMduOE=; b=kMjvoT2x9y5RuOlwpDETRUjSoGdEc3DiAfFW6SMTaPtzxnwH/xVWxx03nf//Ugnlt3m2Q2 rnUugahDSebbmyBQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 07/26] posix-timers: Add proper state tracking References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:09 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner Right now the state tracking is done by two struct members: - it_active: A boolean which tracks armed/disarmed state - it_signal_seq: A sequence counter which is used to invalidate settings and prevent rearming Replace it_active with it_status and keep properly track about the states in one place. This allows to reuse it_signal_seq to track reprogramming, disarm and delete operations in order to drop signals which are related to the state previous of those operations. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- include/linux/posix-timers.h | 4 ++-- kernel/time/alarmtimer.c | 2 +- kernel/time/posix-cpu-timers.c | 15 ++++++++------- kernel/time/posix-timers.c | 22 +++++++++++++--------- kernel/time/posix-timers.h | 6 ++++++ 5 files changed, 30 insertions(+), 19 deletions(-) --- diff --git a/include/linux/posix-timers.h b/include/linux/posix-timers.h index 253d106fac2c..02afbb4da7f7 100644 --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -147,7 +147,7 @@ static inline void posix_cputimers_init_work(void) { } * @kclock: Pointer to the k_clock struct handling this timer * @it_clock: The posix timer clock id * @it_id: The posix timer id for identifying the timer - * @it_active: Marker that timer is active + * @it_status: The status of the timer * @it_overrun: The overrun counter for pending signals * @it_overrun_last: The overrun at the time of the last delivered signal * @it_signal_seq: Sequence count to control signal delivery @@ -168,7 +168,7 @@ struct k_itimer { const struct k_clock *kclock; clockid_t it_clock; timer_t it_id; - int it_active; + int it_status; s64 it_overrun; s64 it_overrun_last; unsigned int it_signal_seq; diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c index 22d5145dd9a7..71360f8072d4 100644 --- a/kernel/time/alarmtimer.c +++ b/kernel/time/alarmtimer.c @@ -585,7 +585,7 @@ static enum alarmtimer_restart alarm_handle_timer(struc= t alarm *alarm, */ ptr->it_overrun +=3D __alarm_forward_now(alarm, ptr->it_interval, true); ++ptr->it_signal_seq; - ptr->it_active =3D 1; + ptr->it_status =3D POSIX_TIMER_ARMED; result =3D ALARMTIMER_RESTART; } spin_unlock_irqrestore(&ptr->it_lock, flags); diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c index 993243b5be98..12f828d704b1 100644 --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -453,7 +453,6 @@ static void disarm_timer(struct k_itimer *timer, struct= task_struct *p) struct cpu_timer *ctmr =3D &timer->it.cpu; struct posix_cputimer_base *base; =20 - timer->it_active =3D 0; if (!cpu_timer_dequeue(ctmr)) return; =20 @@ -494,11 +493,12 @@ static int posix_cpu_timer_del(struct k_itimer *timer) */ WARN_ON_ONCE(ctmr->head || timerqueue_node_queued(&ctmr->node)); } else { - if (timer->it.cpu.firing) + if (timer->it.cpu.firing) { ret =3D TIMER_RETRY; - else + } else { disarm_timer(timer, p); - + timer->it_status =3D POSIX_TIMER_DISARMED; + } unlock_task_sighand(p, &flags); } =20 @@ -560,7 +560,7 @@ static void arm_timer(struct k_itimer *timer, struct ta= sk_struct *p) struct cpu_timer *ctmr =3D &timer->it.cpu; u64 newexp =3D cpu_timer_getexpires(ctmr); =20 - timer->it_active =3D 1; + timer->it_status =3D POSIX_TIMER_ARMED; if (!cpu_timer_enqueue(&base->tqhead, ctmr)) return; =20 @@ -586,7 +586,8 @@ static void cpu_timer_fire(struct k_itimer *timer) { struct cpu_timer *ctmr =3D &timer->it.cpu; =20 - timer->it_active =3D 0; + timer->it_status =3D POSIX_TIMER_DISARMED; + if (unlikely(timer->sigq =3D=3D NULL)) { /* * This a special case for clock_nanosleep, @@ -671,7 +672,7 @@ static int posix_cpu_timer_set(struct k_itimer *timer, = int timer_flags, ret =3D TIMER_RETRY; } else { cpu_timer_dequeue(ctmr); - timer->it_active =3D 0; + timer->it_status =3D POSIX_TIMER_DISARMED; } =20 /* diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 26243d38d27d..6f0dacec25e0 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -272,7 +272,7 @@ bool posixtimer_deliver_signal(struct kernel_siginfo *i= nfo) if (timr->it_interval && timr->it_signal_seq =3D=3D info->si_sys_private)= { timr->kclock->timer_rearm(timr); =20 - timr->it_active =3D 1; + timr->it_status =3D POSIX_TIMER_ARMED; timr->it_overrun_last =3D timr->it_overrun; timr->it_overrun =3D -1LL; ++timr->it_signal_seq; @@ -292,14 +292,17 @@ bool posixtimer_deliver_signal(struct kernel_siginfo = *info) =20 int posix_timer_queue_signal(struct k_itimer *timr) { + enum posix_timer_state state =3D POSIX_TIMER_DISARMED; int ret, si_private =3D 0; enum pid_type type; =20 lockdep_assert_held(&timr->it_lock); =20 - timr->it_active =3D 0; - if (timr->it_interval) + if (timr->it_interval) { + state =3D POSIX_TIMER_REQUEUE_PENDING; si_private =3D ++timr->it_signal_seq; + } + timr->it_status =3D state; =20 type =3D !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDT= YPE_PID; ret =3D send_sigqueue(timr->sigq, timr->it_pid, type, si_private); @@ -367,7 +370,7 @@ static enum hrtimer_restart posix_timer_fn(struct hrtim= er *timer) timr->it_overrun +=3D hrtimer_forward(timer, now, timr->it_interval); ret =3D HRTIMER_RESTART; ++timr->it_signal_seq; - timr->it_active =3D 1; + timr->it_status =3D POSIX_TIMER_ARMED; } } =20 @@ -647,10 +650,10 @@ void common_timer_get(struct k_itimer *timr, struct i= timerspec64 *cur_setting) /* interval timer ? */ if (iv) { cur_setting->it_interval =3D ktime_to_timespec64(iv); - } else if (!timr->it_active) { + } else if (timr->it_status =3D=3D POSIX_TIMER_DISARMED) { /* * SIGEV_NONE oneshot timers are never queued and therefore - * timr->it_active is always false. The check below + * timr->it_status is always DISARMED. The check below * vs. remaining time will handle this case. * * For all other timers there is nothing to update here, so @@ -895,7 +898,7 @@ int common_timer_set(struct k_itimer *timr, int flags, if (kc->timer_try_to_cancel(timr) < 0) return TIMER_RETRY; =20 - timr->it_active =3D 0; + timr->it_status =3D POSIX_TIMER_DISARMED; posix_timer_set_common(timr, new_setting); =20 /* Keep timer disarmed when it_value is zero */ @@ -908,7 +911,8 @@ int common_timer_set(struct k_itimer *timr, int flags, sigev_none =3D timr->it_sigev_notify =3D=3D SIGEV_NONE; =20 kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none); - timr->it_active =3D !sigev_none; + if (!sigev_none) + timr->it_status =3D POSIX_TIMER_ARMED; return 0; } =20 @@ -1007,7 +1011,7 @@ int common_timer_del(struct k_itimer *timer) timer->it_interval =3D 0; if (kc->timer_try_to_cancel(timer) < 0) return TIMER_RETRY; - timer->it_active =3D 0; + timer->it_status =3D POSIX_TIMER_DISARMED; return 0; } =20 diff --git a/kernel/time/posix-timers.h b/kernel/time/posix-timers.h index 4784ea65f685..4d09677e584e 100644 --- a/kernel/time/posix-timers.h +++ b/kernel/time/posix-timers.h @@ -1,6 +1,12 @@ /* SPDX-License-Identifier: GPL-2.0 */ #define TIMER_RETRY 1 =20 +enum posix_timer_state { + POSIX_TIMER_DISARMED, + POSIX_TIMER_ARMED, + POSIX_TIMER_REQUEUE_PENDING, +}; + struct k_clock { int (*clock_getres)(const clockid_t which_clock, struct timespec64 *tp); From nobody Thu Nov 28 13:41:55 2024 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 5984F19C576 for ; Tue, 1 Oct 2024 08:42:12 +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=1727772134; cv=none; b=qi4au7NLAD4ClGXPS7rWIUFg2V8/6CfaZKELjiSgNsCUTb4cYBGebQY33TmeSGWPVNItFIc6QtTSilzFkgu6o0YwQdbYgcUijR8dJY24/OZu4WABSjByvwy7yJSBgvmJBE6yS9rYs07xUNQT5eQ/AF6ve46bfo7xmgOPU8JMZNk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772134; c=relaxed/simple; bh=/WC7Qot4kfXVlcXY0CB88UfzooEt6WkGW4zw2WwwdHA=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=d6uspnpz/xQZxVpVGacL0jBuamW/Av+oQAazcyK9oDBtASe5bo08L7qaaoQP7tgXaJOh2W8bUgDsl3vax8HGFP/ljdnsDAViNcTJVMH2kbpAuW7AaIACNLjAHbgqz4Sl/PhBvUoLkH37UlLqgqSLntB6PsNPgRhQOFvOLq3LeUY= 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=3Kw45Q6m; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=317b2EbA; 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="3Kw45Q6m"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="317b2EbA" Message-ID: <20241001083835.730528613@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772130; 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=ETLRJxVhG9+FzwxJUANI1YWB6ZQWGTcoZ5dDjrMSiiM=; b=3Kw45Q6mPkuPeQ+7V2/APXA4x7bs6aHqxGpKncAwZLtkj6WkMLVPD0+WwThuCKjuGXglj3 aS0Q+0aaE4ZbRS9sWkrtD92p6umsaKm1tZCjUm31MVP5sKYoZ/U2ujtpp3rBHY/mWfpheA shucTX28Yg+yjmRHtxHhLRtcDkehPFCz2fDEoFSgIOGy0auawPMUn8/7UNroRXJkpVlQht RkZKLqiUPUnzXDpsPp/MEHaA6adHjafl7IjhkpLhmVbWMRZUiF0ZgKQcb7jIdxGXpbqxkE rsltq4uDxuTO8uxjpHj5PUFMepOw7KmJIVqdDZG270hQNhNlUM2azRqTWGPDYg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772130; 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=ETLRJxVhG9+FzwxJUANI1YWB6ZQWGTcoZ5dDjrMSiiM=; b=317b2EbATEj6cgAFBcp5CJwEXLwplbp9uxVPS8dMGEgc2rl2MFRBl9VDf+Ni6aay5COppB lALd4N/Cw+fwKHAg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 08/26] posix-timers: Make signal delivery consistent References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:10 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner Signals of timers which are reprogammed, disarmed or deleted can deliver signals related to the past. The POSIX spec is blury about this: - "The effect of disarming or resetting a timer with pending expiration notifications is unspecified." - "The disposition of pending signals for the deleted timer is unspecified." In both cases it is reasonable to expect that pending signals are discarded. Especially in the reprogramming case it does not make sense to account for previous overruns or to deliver a signal for a timer which has been disarmed. This makes the behaviour consistent and understandable. Remove the si_sys_private check from the signal delivery code and invoke posix_timer_deliver_signal() unconditionally for posix timer related signals. Change posix_timer_deliver_signal() so it controls the actual signal delive= ry via the return value. It now instructs the signal code to drop the signal when: 1) The timer does not longer exist in the hash table 2) The timer signal_seq value is not the same as the si_sys_private value which was set when the signal was queued. This is also a preparatory change to embed the sigqueue into the k_itimer structure, which in turn allows to remove the si_sys_private magic. Signed-off-by: Thomas Gleixner Acked-by: "Eric W. Biederman" --- include/linux/posix-timers.h | 2 -- kernel/signal.c | 6 ++---- kernel/time/posix-cpu-timers.c | 2 +- kernel/time/posix-timers.c | 25 +++++++++++++++---------- 4 files changed, 18 insertions(+), 17 deletions(-) --- --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -137,8 +137,6 @@ static inline void clear_posix_cputimers static inline void posix_cputimers_init_work(void) { } #endif =20 -#define REQUEUE_PENDING 1 - /** * struct k_itimer - POSIX.1b interval timer structure. * @list: List head for binding the timer to signals->posix_timers --- a/kernel/signal.c +++ b/kernel/signal.c @@ -550,10 +550,8 @@ static void collect_signal(int sig, stru list_del_init(&first->list); copy_siginfo(info, &first->info); =20 - *resched_timer =3D - (first->flags & SIGQUEUE_PREALLOC) && - (info->si_code =3D=3D SI_TIMER) && - (info->si_sys_private); + *resched_timer =3D (first->flags & SIGQUEUE_PREALLOC) && + (info->si_code =3D=3D SI_TIMER); =20 __sigqueue_free(first); } else { --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -746,7 +746,7 @@ static void __posix_cpu_timer_get(struct * - Timers which expired, but the signal has not yet been * delivered */ - if (iv && ((timer->it_signal_seq & REQUEUE_PENDING) || sigev_none)) + if (iv && timer->it_status !=3D POSIX_TIMER_ARMED) expires =3D bump_cpu_timer(timer, now); else expires =3D cpu_timer_getexpires(&timer->it.cpu); --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -269,7 +269,10 @@ bool posixtimer_deliver_signal(struct ke if (!timr) goto out; =20 - if (timr->it_interval && timr->it_signal_seq =3D=3D info->si_sys_private)= { + if (timr->it_signal_seq !=3D info->si_sys_private) + goto out_unlock; + + if (timr->it_interval && timr->it_status =3D=3D POSIX_TIMER_REQUEUE_PENDI= NG) { timr->kclock->timer_rearm(timr); =20 timr->it_status =3D POSIX_TIMER_ARMED; @@ -281,6 +284,7 @@ bool posixtimer_deliver_signal(struct ke } ret =3D true; =20 +out_unlock: unlock_timer(timr, flags); out: spin_lock(¤t->sighand->siglock); @@ -293,19 +297,19 @@ bool posixtimer_deliver_signal(struct ke int posix_timer_queue_signal(struct k_itimer *timr) { enum posix_timer_state state =3D POSIX_TIMER_DISARMED; - int ret, si_private =3D 0; enum pid_type type; + int ret; =20 lockdep_assert_held(&timr->it_lock); =20 if (timr->it_interval) { + timr->it_signal_seq++; state =3D POSIX_TIMER_REQUEUE_PENDING; - si_private =3D ++timr->it_signal_seq; } timr->it_status =3D state; =20 type =3D !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDT= YPE_PID; - ret =3D send_sigqueue(timr->sigq, timr->it_pid, type, si_private); + ret =3D send_sigqueue(timr->sigq, timr->it_pid, type, timr->it_signal_seq= ); /* If we failed to send the signal the timer stops. */ return ret > 0; } @@ -670,7 +674,7 @@ void common_timer_get(struct k_itimer *t * is a SIGEV_NONE timer move the expiry time forward by intervals, * so expiry is > now. */ - if (iv && (timr->it_signal_seq & REQUEUE_PENDING || sig_none)) + if (iv && timr->it_status !=3D POSIX_TIMER_ARMED) timr->it_overrun +=3D kc->timer_forward(timr, now); =20 remaining =3D kc->timer_remaining(timr, now); @@ -870,8 +874,6 @@ void posix_timer_set_common(struct k_iti else timer->it_interval =3D 0; =20 - /* Prevent reloading in case there is a signal pending */ - timer->it_signal_seq =3D (timer->it_signal_seq + 2) & ~REQUEUE_PENDING; /* Reset overrun accounting */ timer->it_overrun_last =3D 0; timer->it_overrun =3D -1LL; @@ -889,8 +891,6 @@ int common_timer_set(struct k_itimer *ti if (old_setting) common_timer_get(timr, old_setting); =20 - /* Prevent rearming by clearing the interval */ - timr->it_interval =3D 0; /* * Careful here. On SMP systems the timer expiry function could be * active and spinning on timr->it_lock. @@ -940,6 +940,9 @@ static int do_timer_settime(timer_t time if (old_spec64) old_spec64->it_interval =3D ktime_to_timespec64(timr->it_interval); =20 + /* Prevent signal delivery and rearming. */ + timr->it_signal_seq++; + kc =3D timr->kclock; if (WARN_ON_ONCE(!kc || !kc->timer_set)) error =3D -EINVAL; @@ -1008,7 +1011,6 @@ int common_timer_del(struct k_itimer *ti { const struct k_clock *kc =3D timer->kclock; =20 - timer->it_interval =3D 0; if (kc->timer_try_to_cancel(timer) < 0) return TIMER_RETRY; timer->it_status =3D POSIX_TIMER_DISARMED; @@ -1036,6 +1038,9 @@ SYSCALL_DEFINE1(timer_delete, timer_t, t if (!timer) return -EINVAL; =20 + /* Prevent signal delivery and rearming. */ + timer->it_signal_seq++; + if (unlikely(timer_delete_hook(timer) =3D=3D TIMER_RETRY)) { /* Unlocks and relocks the timer if it still exists */ timer =3D timer_wait_running(timer, &flags); From nobody Thu Nov 28 13:41:55 2024 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 9BDAA19F125 for ; Tue, 1 Oct 2024 08:42:13 +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=1727772135; cv=none; b=BNHq0RBawD/I8IZiKr7/PONwoM6Q3WO2LOVjTeyXVS+WhvjFmNCg5k8kmUKNqI+mvx6Q1EjjWcQButtQ517y28TboAr41xdKmItg5UTYd/Kk7siB5GhQW0swQRwr+9FX5drXDwyM2FVfQf/PJQgafRh3ZrQB5P5lwH1cllDo+YY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772135; c=relaxed/simple; bh=AEiot9FztQ13Wj30VxZ4rIX1Z97/r+X89CCBUXoiuz0=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=JQMEi49iByl+paj+kLus0Jdn62bV1yM6rlvihZASvwkTZJTCy98KN5MrJ8SFVfkAhCXKxvclzZJGomr+nF+q+0AeNTM5xv5CVgthGwJL371RNFv86lLWkEJ+A+UD0f/42V9RikKM8AOE24zzFrLN88LPqnLdGPoTEesn6GgGSj4= 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=XBkjREkZ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=/0Nwl1NU; 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="XBkjREkZ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="/0Nwl1NU" Message-ID: <20241001083835.790542522@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772132; 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=fGGpvUvjuTFoEqWI2WeRDpfCt8hIp9FqKEHSvmTlWgc=; b=XBkjREkZrn3gNKRsVffYhXLtvUaARO6gI53WHJHBrHPtVYy3imWHj2XV9AngJhT0QdzT2s dW+Fi92s3tm5WLWaDuX4w3lHDmAvRxsPB5pT+zKX1JAEmTgl3Jqv640IYVxfM07vXVJWY/ gTxz001rYMjeM0fOWMQQs21GZtXJNBXNvAwejp3aY1e/3flCwOw4U6oWPXysytxxKeFQ8L 3ajES3hdFvcsSEWNx+5EkTed6reaArZrQhCQtKfB7LYWDIjjPAckc/0+YWanxRpFVAP8JV sJ/EdQMJwuGnWOCqfqIVs3E+Qfc6LEGPiFWktC/ouFBPXvBZGoGL5X6tk/84Qw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772132; 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=fGGpvUvjuTFoEqWI2WeRDpfCt8hIp9FqKEHSvmTlWgc=; b=/0Nwl1NUyYF3Vz2FB+W2cDgGDIg/AxM9FchY/7wrQF4100SQNWbJkTZrAYAIMVLHb7iUpf ZFen2xC3la8ZuNAQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 09/26] posix-timers: Make signal overrun accounting sensible References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:12 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner The handling of the timer overrun in the signal code is inconsistent as it takes previous overruns into account. This is just wrong as after the reprogramming of a timer the overrun count starts over from a clean state, i.e. 0. Make the accounting in send_sigqueue() consistent with that. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" --- kernel/signal.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) --- --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1961,6 +1961,34 @@ int send_sigqueue(struct sigqueue *q, st */ q->info.si_sys_private =3D si_private; =20 + /* + * Set the overrun count to zero unconditionally. The posix timer + * code does not self rearm periodic timers. They are rearmed from + * dequeue_signal(). + * + * But there is a situation where @q is already enqueued: + * + * 1) timer_settime() + * arm_timer() + * 2) timer_expires() + * send_sigqueue(@q) + * enqueue(@q) + * 3) timer_settime() + * arm_timer() + * 4) timer_expires() + * send_sigqueue(@q) <- Observes @q already queued + * + * In this case incrementing si_overrun does not make sense because + * there is no relationship between timer_settime() #1 and #2. + * + * The POSIX specification is useful as always: "The effect of + * disarming or resetting a timer with pending expiration + * notifications is unspecified." + * + * Just do the sensible thing and reset the overrun. + */ + q->info.si_overrun =3D 0; + ret =3D 1; /* the signal is ignored */ result =3D TRACE_SIGNAL_IGNORED; if (!prepare_signal(sig, t, false)) @@ -1968,15 +1996,9 @@ int send_sigqueue(struct sigqueue *q, st =20 ret =3D 0; if (unlikely(!list_empty(&q->list))) { - /* - * If an SI_TIMER entry is already queue just increment - * the overrun count. - */ - q->info.si_overrun++; result =3D TRACE_SIGNAL_ALREADY_PENDING; goto out; } - q->info.si_overrun =3D 0; =20 signalfd_notify(t, sig); pending =3D (type !=3D PIDTYPE_PID) ? &t->signal->shared_pending : &t->pe= nding; From nobody Thu Nov 28 13:41:55 2024 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 1117419F430 for ; Tue, 1 Oct 2024 08:42:15 +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=1727772136; cv=none; b=lr+QUY2ZkZfbkGinLCMfvPcubUyCSCumhUobJ35XDjOu6i585/NQWZndy4c7NKMLTqb0cvKWHi+0horb0JXIb55txntv0TEB5q2froUQKCyQ3WgWuxcn4LWftLy6qjO+P/YblrTgnBO8uZEkTjEnc1lijuOy+h/7L5I0UaP1q+8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772136; c=relaxed/simple; bh=+2fIijOLWcSBLiaSrFrK6FdMUB/dFmsW0E5g8XCruQY=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=DoI2AOidPeWf9gZXUbcSkGSfLynaTwMbsQ7U3QCbms8yU9h79i5POG4YagEO8/FGSaBrKiw+rzR9hpSGwfN9koUfJ7r96C1gijV+PBbnll3rttLZuSMK4cpg6hSNpIFziFrAGKdKCaeLwon79FEXZ/pGJNlZr6TJMfGHJyLFBx8= 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=BJVB0Z/f; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=lfL7KSwA; 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="BJVB0Z/f"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="lfL7KSwA" Message-ID: <20241001083835.850997918@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772133; 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=tQCVdEvPXh4Wm/hJB6IEGBX+1Rtg0oGtuB4fY52s0bM=; b=BJVB0Z/fB/eJcx9kB+VfGxERZ7zvIU0h3eZJL8UGBz47F8rxP4tBYuROr7mxB62y1sX9G9 4jgIZDhHIU4pftTwy6lI59LVOSNl674HYndk6SkhyOKlO+jigBQ9k7YGHmXL4Re4Tz4XJU BSKGYSOfuxASdyjuL8VN8RbIlUMZ1r5ZAOlj4xoHMF0OpGAouxhWz6ngzPw9e/5Upy8d/y j4uRuDHGnOlGSE7/n7kcDy4QAgt9UY/lgO+iB/0eVdTNBQHn1XzJP4vM1VORDhreMec8Xq idUo4GlrfH1XxtvvWzIGAOS4b2iRa4R58al3yBRa9MVxTxYgLOJRfaXSUoxD5A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772133; 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=tQCVdEvPXh4Wm/hJB6IEGBX+1Rtg0oGtuB4fY52s0bM=; b=lfL7KSwAsrp4fvOgAoD7g+Q+G0/LPnxGE3j9vghiUeTjdp1ID54/LTmLlsRDHGU86pq/rP KVL1QSve9zOwVsCQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 10/26] posix-cpu-timers: Use dedicated flag for CPU timer nanosleep References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:13 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner POSIX CPU timer nanosleep creates a k_itimer on stack and uses the sigq pointer to detect the nanosleep case in the expiry function. Prepare for embedding sigqueue into struct k_itimer by using a dedicated flag for nanosleep. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- include/linux/posix-timers.h | 4 +++- kernel/time/posix-cpu-timers.c | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) --- diff --git a/include/linux/posix-timers.h b/include/linux/posix-timers.h index 8c6d97412526..bcd01208d795 100644 --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -42,6 +42,7 @@ static inline int clockid_to_fd(const clockid_t clk) * @pid: Pointer to target task PID * @elist: List head for the expiry list * @firing: Timer is currently firing + * @nanosleep: Timer is used for nanosleep and is not a regular posix-timer * @handling: Pointer to the task which handles expiry */ struct cpu_timer { @@ -49,7 +50,8 @@ struct cpu_timer { struct timerqueue_head *head; struct pid *pid; struct list_head elist; - int firing; + bool firing; + bool nanosleep; struct task_struct __rcu *handling; }; =20 diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c index bc2cd32b7a40..ea1835cb541a 100644 --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -588,7 +588,7 @@ static void cpu_timer_fire(struct k_itimer *timer) =20 timer->it_status =3D POSIX_TIMER_DISARMED; =20 - if (unlikely(timer->sigq =3D=3D NULL)) { + if (unlikely(ctmr->nanosleep)) { /* * This a special case for clock_nanosleep, * not a normal timer from sys_timer_create. @@ -1479,6 +1479,7 @@ static int do_cpu_nanosleep(const clockid_t which_clo= ck, int flags, timer.it_overrun =3D -1; error =3D posix_cpu_timer_create(&timer); timer.it_process =3D current; + timer.it.cpu.nanosleep =3D true; =20 if (!error) { static struct itimerspec64 zero_it; From nobody Thu Nov 28 13:41:55 2024 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 B565E19FA7B for ; Tue, 1 Oct 2024 08:42:16 +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=1727772138; cv=none; b=RsvzVpGLG9KfXGO+gQsxI99EZVe7wYGK9xr2IjnPdGIg9qb9zAGBqaivSKMDgXViaZrsNUC00iGl1e1O0CrzYfNKEeuVM+MotIX9pHlGKGvtaj59SuFftfX3mo6D6Oq5YjplKxBP+/LcsquX6OXdaPLUyFzvUo6uKjJG3roWzTA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772138; c=relaxed/simple; bh=GAJCGvHih3fFKPS1AZBCNxGZaNoCaC/odd/osWX/9c8=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=qwQSejEEQZcMEY9Fhpdk1jF7y7qjKdkm3V3i6w4mGFcSFZ/8eQsDXG9QR5jmDn41/qNXVV/bOEaD78evz68GUJAbnAUOkrInrtPygsn/Vaf+7jNsRPndv951WYPlmRMMxRYY28h+wgcbMAyPwI6jmUq1aYtuBHSr/EVlv/MBiOA= 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=WFzGTMtX; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=NVeEJjdU; 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="WFzGTMtX"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="NVeEJjdU" Message-ID: <20241001083835.910895102@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772135; 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=xbbHc0pM0K9mADkAU9snuiAtYLGmq3SBtRf9ESn+KJI=; b=WFzGTMtXy839efvc1FFK7cMJavnBK9N/uaObcoMPsoA4KewZ08Ys189P4nZemM8wppbZOd /9NZ9mqU14mLs8pyvAr1G+jvGHYCQ8EDJBto4kiA/w1xT7WJzxfux2UCbl7RJVKuKll5Nd 7g3RJy9mRYoAwb0eZ1U21Q0HXmupji1XscWy7GQenEJCf+h+LwjwqwEe6GW7bco595Eshy TsGwaVBx7lpDF2z8cf3lqpRj8dDo2Dm9t5Z0Pm+sQf9t9eqlqyNaiV+lbYU5iMvvfTucf0 Qb6Lv7X+iyAi64HNGuMaYWJ3s8tOyI9a1zXmy5xEL9JSCAfcP5l4WYawH57Rhw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772135; 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=xbbHc0pM0K9mADkAU9snuiAtYLGmq3SBtRf9ESn+KJI=; b=NVeEJjdUbPO98KM7dlqedCbh88ZgBZpylZHRILW1rRPRHLwJYjot1840yyStyXaSokGInF xW3o9SfZ0KOH11BQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 11/26] posix-timers: Add a refcount to struct k_itimer References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:14 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner To cure the SIG_IGN handling for posix interval timers, the preallocated sigqueue needs to be embedded into struct k_itimer to prevent life time races of all sorts. To make that work correctly it needs reference counting so that timer deletion does not free the timer prematuraly when there is a signal queued or delivered concurrently. Add a rcuref to the posix timer part. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- include/linux/posix-timers.h | 14 ++++++++++++++ kernel/time/posix-timers.c | 7 ++++--- 2 files changed, 18 insertions(+), 3 deletions(-) --- diff --git a/include/linux/posix-timers.h b/include/linux/posix-timers.h index bcd01208d795..9740fd0c2933 100644 --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -6,11 +6,13 @@ #include #include #include +#include #include #include =20 struct kernel_siginfo; struct task_struct; +struct k_itimer; =20 static inline clockid_t make_process_cpuclock(const unsigned int pid, const clockid_t clock) @@ -105,6 +107,7 @@ static inline void posix_cputimers_rt_watchdog(struct p= osix_cputimers *pct, =20 void posixtimer_rearm_itimer(struct task_struct *p); bool posixtimer_deliver_signal(struct kernel_siginfo *info); +void posixtimer_free_timer(struct k_itimer *timer); =20 /* Init task static initializer */ #define INIT_CPU_TIMERBASE(b) { \ @@ -129,6 +132,7 @@ static inline void posix_cputimers_group_init(struct po= six_cputimers *pct, u64 cpu_limit) { } static inline void posixtimer_rearm_itimer(struct task_struct *p) { } static inline bool posixtimer_deliver_signal(struct kernel_siginfo *info) = { return false; } +static inline void posixtimer_free_timer(struct k_itimer *timer) { } #endif =20 #ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK @@ -156,6 +160,7 @@ static inline void posix_cputimers_init_work(void) { } * @it_signal: Pointer to the creators signal struct * @it_pid: The pid of the process/task targeted by the signal * @it_process: The task to wakeup on clock_nanosleep (CPU timers) + * @rcuref: Reference count for life time management * @sigq: Pointer to preallocated sigqueue * @it: Union representing the various posix timer type * internals. @@ -180,6 +185,7 @@ struct k_itimer { struct task_struct *it_process; }; struct sigqueue *sigq; + rcuref_t rcuref; union { struct { struct hrtimer timer; @@ -200,4 +206,12 @@ void set_process_cpu_timer(struct task_struct *task, u= nsigned int clock_idx, =20 int update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new); =20 +#ifdef CONFIG_POSIX_TIMERS +static inline void posixtimer_putref(struct k_itimer *tmr) +{ + if (rcuref_put(&tmr->rcuref)) + posixtimer_free_timer(tmr); +} +#endif /* !CONFIG_POSIX_TIMERS */ + #endif diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 1231efb7c30f..1c2f6090b767 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -417,6 +417,7 @@ static struct k_itimer * alloc_posix_timer(void) return NULL; } clear_siginfo(&tmr->sigq->info); + rcuref_init(&tmr->rcuref, 1); return tmr; } =20 @@ -427,7 +428,7 @@ static void k_itimer_rcu_free(struct rcu_head *head) kmem_cache_free(posix_timers_cache, tmr); } =20 -static void posix_timer_free(struct k_itimer *tmr) +void posixtimer_free_timer(struct k_itimer *tmr) { put_pid(tmr->it_pid); sigqueue_free(tmr->sigq); @@ -439,7 +440,7 @@ 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); - posix_timer_free(tmr); + posixtimer_putref(tmr); } =20 static int common_timer_create(struct k_itimer *new_timer) @@ -474,7 +475,7 @@ static int do_timer_create(clockid_t which_clock, struc= t sigevent *event, */ new_timer_id =3D posix_timer_add(new_timer); if (new_timer_id < 0) { - posix_timer_free(new_timer); + posixtimer_free_timer(new_timer); return new_timer_id; } From nobody Thu Nov 28 13:41:55 2024 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 2C3731A00F3 for ; Tue, 1 Oct 2024 08:42:17 +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=1727772139; cv=none; b=ZBp7mxpq88gXE4J3if6vbBeiyrtCLqu1EIxUdIz1xQ2wFHLpHFFrUCcYikwDMEd1JJTDlB2D0eiwdB79zVjpZl11OVg2kmV9a2M3l/M6Ncu4H9PJFw+VM5ejLJXYX2yuxd8004rmfNV1KizJFzGS3eqHWFu2XxPQxxyKnaa1dO0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772139; c=relaxed/simple; bh=Zh/YIxm87UGGG23ecLig7pP6cwITl+LA711478LPV3c=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=OsCQ19azbXEoB/JuelyJ7lq8zLQeJWqsg2aDXPrvtzf5oBxQa5rnTjn6PN53Ih1PKTzAGltV/m3dhS1lgPbfbCeko4e/UIJF0+b8kXMDm9MbkcCANMgXSlkDAAsGLyDLpFcJ8ClqPzLXjVhw9LGSqohd1+8JzixcFnQXNeeyEPk= 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=UDFY94Nq; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=HvHB1kbB; 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="UDFY94Nq"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="HvHB1kbB" Message-ID: <20241001083835.971100589@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772136; 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=9pYCvucO0XJm5P4L8+u+NQUdYnVt14RcjMaKh5/8YAo=; b=UDFY94Nq/jAy5AoGmsB6XRwlzexz5yjKcxeS0PMdZI26xHgstNUeIwvAgcPzfFOT7VQlDH eOtRfSUkV4VUvgn8alM5TPctMdEwUOJcSkLOaukb6jAE3rMOS4BDzv6ewM2FFN0jSz7uke NsVgR/d+mpZ+DHUT/7ZfhM4KSCWYendYbXBH/8xSLKkYQqlOwO4g0sXTAcHK5fZ0JJ+9uI Gj/FupQuG1w6zapeWv2XeJ91FX+yMWXbbs/44JdlrG0+YR+V7FfX0v9Dn1KA8Ql0/asO7o HNUme9Oe2ua9f4zlvsV4zlgOPsKX/OTyrlDcHMgUkWgqJKVabJ2Dl95BApntLw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772136; 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=9pYCvucO0XJm5P4L8+u+NQUdYnVt14RcjMaKh5/8YAo=; b=HvHB1kbB2wwlgL6qaoXVzpQl1sSZV4whsb/LIABV+8ka7X5hMHSHVTYfz6OBVCVSrz/3sl NfoRNoHLm1mW71Aw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 12/26] signal: Split up __sigqueue_alloc() References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:16 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner To cure the SIG_IGN handling for posix interval timers, the preallocated sigqueue needs to be embedded into struct k_itimer to prevent life time races of all sorts. Reorganize __sigqueue_alloc() so the ucounts retrieval and the initialization can be used independently. No functional change. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- kernel/signal.c | 52 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 17 deletions(-) --- diff --git a/kernel/signal.c b/kernel/signal.c index a99274287902..87c349a2ddf7 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -396,16 +396,9 @@ void task_join_group_stop(struct task_struct *task) task_set_jobctl_pending(task, mask | JOBCTL_STOP_PENDING); } =20 -/* - * allocate a new signal queue record - * - this may be called without locks if and only if t =3D=3D current, oth= erwise an - * appropriate lock must be held to stop the target task from exiting - */ -static struct sigqueue * -__sigqueue_alloc(int sig, struct task_struct *t, gfp_t gfp_flags, - int override_rlimit, const unsigned int sigqueue_flags) +static struct ucounts *sig_get_ucounts(struct task_struct *t, int sig, + int override_rlimit) { - struct sigqueue *q =3D NULL; struct ucounts *ucounts; long sigpending; =20 @@ -424,19 +417,44 @@ __sigqueue_alloc(int sig, struct task_struct *t, gfp_= t gfp_flags, if (!sigpending) return NULL; =20 - if (override_rlimit || likely(sigpending <=3D task_rlimit(t, RLIMIT_SIGPE= NDING))) { - q =3D kmem_cache_alloc(sigqueue_cachep, gfp_flags); - } else { + if (unlikely(!override_rlimit && sigpending > task_rlimit(t, RLIMIT_SIGPE= NDING))) { + dec_rlimit_put_ucounts(ucounts, UCOUNT_RLIMIT_SIGPENDING); print_dropped_signal(sig); + return NULL; } =20 - if (unlikely(q =3D=3D NULL)) { + return ucounts; +} + +static void __sigqueue_init(struct sigqueue *q, struct ucounts *ucounts, + const unsigned int sigqueue_flags) +{ + INIT_LIST_HEAD(&q->list); + q->flags =3D sigqueue_flags; + q->ucounts =3D ucounts; +} + +/* + * allocate a new signal queue record + * - this may be called without locks if and only if t =3D=3D current, oth= erwise an + * appropriate lock must be held to stop the target task from exiting + */ +static struct sigqueue *__sigqueue_alloc(int sig, struct task_struct *t, g= fp_t gfp_flags, + int override_rlimit, const unsigned int sigqueue_flags) +{ + struct ucounts *ucounts =3D sig_get_ucounts(t, sig, override_rlimit); + struct sigqueue *q; + + if (!ucounts) + return NULL; + + q =3D kmem_cache_alloc(sigqueue_cachep, gfp_flags); + if (!q) { dec_rlimit_put_ucounts(ucounts, UCOUNT_RLIMIT_SIGPENDING); - } else { - INIT_LIST_HEAD(&q->list); - q->flags =3D sigqueue_flags; - q->ucounts =3D ucounts; + return NULL; } + + __sigqueue_init(q, ucounts, sigqueue_flags); return q; } From nobody Thu Nov 28 13:41:55 2024 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 756741A01BB for ; Tue, 1 Oct 2024 08:42:19 +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=1727772140; cv=none; b=RKBUrAzGlHuZvfTtyYaFiTUJder98cegJEhLcPV8TvI69m5o3dN664ryaSF5V4/pHyDpWxrToaeirfKdniqaapPWEB1AARJqgH4yIg6gpTYFsAVbfy1ltL6bVNxVT8zdecc0yqaf5NBdX0DV47Xl7KjqozmaWQd2+Mv/TDTPpgo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772140; c=relaxed/simple; bh=7By1IOqtAz0qQuAvri3T+IbplWyDq5Mz/sllGuVO58s=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=nXMb6JZrzWPGZnrAiaMP/DdnNuxGCdhNrI4MC41mzlf/mW96isaatSawNOgBH+nsFlgz9FMwbEWgujqb9ZqggO1o5ZagwaFwUx78ChidWb8R4m57z3MW+UeKzNcnqF7OlvhxKkQhVBra31qA6aoUGBrw/fhapWs7jW7SZBED/Vg= 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=w4LvXTfw; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=MsJewu4v; 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="w4LvXTfw"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="MsJewu4v" Message-ID: <20241001083836.031093453@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772138; 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=3NEHXqw0CJuJv6ubSFkvIXA+dP8+q3XE+ki9sOO9HUY=; b=w4LvXTfw5iJrGrAR49pzUwAN0zFhOYGNZdFEGzW9kihBaRdNxH+0bQCUCMnK6P4w7p8yde A+1zGMPhY035jao/epOeKaCTMS2w4jNEU6GV2pAIjtDkRJuDp88ICFo1BtAIeruW1XnpOZ MMtFgN6RMR+MZqCaKoFQKl7tEqBqouGZbQER0r5oK8guJsOx9JvvIMxJkLjGSsBHzOzCmj UJyzDSA7dxegU7zrunr9wnFTfbuNlBnxKTb2mHp47iTfFp95cq+g8gRrvxCEP8b9laTjQa 4kMyk9anGR54/za2MvwKYYa6P8LqgKMelLr+lGR5HzuCaWrRWGXpBptECCmdLA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772138; 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=3NEHXqw0CJuJv6ubSFkvIXA+dP8+q3XE+ki9sOO9HUY=; b=MsJewu4vmqOPJGn0YjLUmnhVDgoYaKmnPIiDlicC9MIqSd5NOH2Ha8sfdbvv250jbA8muS b4Gu0rE2rww7mSAA== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 13/26] signal: Provide posixtimer_sigqueue_init() References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:17 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner To cure the SIG_IGN handling for posix interval timers, the preallocated sigqueue needs to be embedded into struct k_itimer to prevent life time races of all sorts. Provide a new function to initialize the embedded sigqueue to prepare for that. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- include/linux/posix-timers.h | 2 ++ kernel/signal.c | 11 +++++++++++ 2 files changed, 13 insertions(+) --- --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -12,6 +12,7 @@ =20 struct kernel_siginfo; struct task_struct; +struct sigqueue; struct k_itimer; =20 static inline clockid_t make_process_cpuclock(const unsigned int pid, @@ -106,6 +107,7 @@ static inline void posix_cputimers_rt_wa } =20 void posixtimer_rearm_itimer(struct task_struct *p); +bool posixtimer_init_sigqueue(struct sigqueue *q); bool posixtimer_deliver_signal(struct kernel_siginfo *info); void posixtimer_free_timer(struct k_itimer *timer); =20 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1907,6 +1907,17 @@ void flush_itimer_signals(void) __flush_itimer_signals(&tsk->signal->shared_pending); } =20 +bool posixtimer_init_sigqueue(struct sigqueue *q) +{ + struct ucounts *ucounts =3D sig_get_ucounts(current, -1, 0); + + if (!ucounts) + return false; + clear_siginfo(&q->info); + __sigqueue_init(q, ucounts, SIGQUEUE_PREALLOC); + return true; +} + struct sigqueue *sigqueue_alloc(void) { return __sigqueue_alloc(-1, current, GFP_KERNEL, 0, SIGQUEUE_PREALLOC); From nobody Thu Nov 28 13:41:55 2024 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 B7CFB1A0700 for ; Tue, 1 Oct 2024 08:42:20 +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=1727772142; cv=none; b=ODqPuka4m51zDFlKt+5lgloZZIpNfdfdakLSlWIR8oQNK9Hnh4nPAjpKXbRO6Iv6OPr03tAucbEElLbh37vyQcRmcpLBPMIiLTjmm0FxcIzlHTy7oe+lEl/spyCdqGgh3Ua3rXEtzIdLISm9Kv6ISoeITENLbCA9mJet3MQj4Tw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772142; c=relaxed/simple; bh=ZOV4sBmQvu5WnM1Ox4USoRdNSskissu0mQH2/ZcAE2E=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=GusL2AEdZdXFIjLIM42UGjdN5/rFAkJjp43SLSaBlclhANUolqUGdJLbVvXCg16szAfyncY0n300UyI6kz9m/Fe1jarlfkG3Nbl3zTDtKdKfxdGc9xaorOwk8/IC+TGboLRxWkpnQ1VS0jOvCPxbUo3GxAnrOkFlwcJGZHNmK5A= 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=LwUJbF3x; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=KxnKYeXg; 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="LwUJbF3x"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="KxnKYeXg" Message-ID: <20241001083836.102492808@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772139; 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=dGpTGihaHQGMy4+Fr4d/sm3whGWzpjpkdI/lg0ei2aI=; b=LwUJbF3xtlQQzb1PhsyBNTR+J2CoFyfxmPgMtrJd/ct8xYP5c0N8b+2J54b8W9XauvPyXk u5maMfZvbC54cDOgSCj5tRgaKTLmO6ZFpZlYzGJh20p7BS7SPWWSgtyH7s/DtLjCT6+eqp yKJgZYteqinZlbrMPS4JFuzeGpIpWChyK7OHc/MulY9sKTnKmyVbMsDiLUdCzYhiHmQQjR /T9lSFcuQPJq8fV1ldLfgE16J3YHlyDaK22ExB4ODqtF3bFb2WivM4+ZR7mpBJ1k88exs1 8DyLzRZL7agjZspP+OZYcNlNbM+sz9uIheUq7mfcIVFlPFv4Umm4GSq+sFgJ4g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772139; 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=dGpTGihaHQGMy4+Fr4d/sm3whGWzpjpkdI/lg0ei2aI=; b=KxnKYeXglxAAaf1ipPB/tzVRdwI6N5+o+H/WJyRqtmp/cDcFFVEHiH/rECYnJ2f6Bl7G5n MExewqUvnidwhiAw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 14/26] posix-timers: Store PID type in the timer References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:19 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner instead of re-evaluating the signal delivery mode everywhere. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- include/linux/posix-timers.h | 2 ++ kernel/time/posix-timers.c | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) --- --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -180,6 +181,7 @@ struct k_itimer { s64 it_overrun_last; unsigned int it_signal_seq; int it_sigev_notify; + enum pid_type it_pid_type; ktime_t it_interval; struct signal_struct *it_signal; union { --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -297,7 +297,6 @@ bool posixtimer_deliver_signal(struct ke int posix_timer_queue_signal(struct k_itimer *timr) { enum posix_timer_state state =3D POSIX_TIMER_DISARMED; - enum pid_type type; int ret; =20 lockdep_assert_held(&timr->it_lock); @@ -308,8 +307,7 @@ int posix_timer_queue_signal(struct k_it } timr->it_status =3D state; =20 - type =3D !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDT= YPE_PID; - ret =3D send_sigqueue(timr->sigq, timr->it_pid, type, timr->it_signal_seq= ); + ret =3D send_sigqueue(timr->sigq, timr->it_pid, timr->it_pid_type, timr->= it_signal_seq); /* If we failed to send the signal the timer stops. */ return ret > 0; } @@ -503,6 +501,11 @@ static int do_timer_create(clockid_t whi new_timer->it_pid =3D get_pid(task_tgid(current)); } =20 + if (new_timer->it_sigev_notify & SIGEV_THREAD_ID) + new_timer->it_pid_type =3D PIDTYPE_PID; + else + new_timer->it_pid_type =3D PIDTYPE_TGID; + new_timer->sigq->info.si_tid =3D new_timer->it_id; new_timer->sigq->info.si_code =3D SI_TIMER; From nobody Thu Nov 28 13:41:55 2024 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 3DBFD1A0733 for ; Tue, 1 Oct 2024 08:42:22 +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=1727772143; cv=none; b=HquyoVQ5QpVPKlP32AlD5tavRlazM1vq8L3o0oA1pktqS2Te+LxdpxLRWoJLz97hsXuu3X/8opsPBoA+57jyqynCg63wcbzgfdm9235pPWYp4iq6AlxAiG3asrXlXdeFxLXnbbBEttdIkLgs4Y+GnwxKjgALsnfFMMrsqw78RMU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772143; c=relaxed/simple; bh=WyFuk/suctmcv8BFM7PlM005FryHmRRohpHmHbyRo5Q=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=GHLxwRd9nQpd3TEBpa3x/ieuSibfFxQcvEpxmnEe9QJYKyCoBRmOymDWNT6+O7/b72zYkScUOlLqe+zxamqHzh8nGiY4rlnfGVJ6429g1rqo4nS2fIjozMOPthgSfWjlSdWREikJ4I6wrVtSJazTAdbt5PLyxUOnLzqwyjNuDvk= 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=bzSJkuZq; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=AsozLfrk; 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="bzSJkuZq"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="AsozLfrk" Message-ID: <20241001083836.160940133@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772140; 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=PxocNGfHSoOJ5DIOApgPE8UuY1gORaIild4LzEnBwws=; b=bzSJkuZqKzH+cdbQ7haNI15owB00znJO3A5eMeEIdmHEr/bLDI11ikac3yRnHlXWekpYXV racANEMCrFVfdXlAQLhtBSAwBSZx9Fl/YZ3jX0B+jqn0PeyRgxGbJUnNS/y+SsnSxJwC2l 26gVC/rkRHw/eDKAxD25AADLofktG/9y+KTaQUNFEkrY/wooyp7qlYZCAuVkRKQyQUw86d 1Z9zNsDneOHQOx+LCgbh9EB7/KDHCDdMGJmmKEu0R7T8aUU/uWrF3Lqwtb0V04fhbx0E/a t2Bt+8O0r6RJE1r887VHx8iZx8AvQ6x6+TkTylbwHUlE6ygVLeXSvk9EB8iaGQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772140; 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=PxocNGfHSoOJ5DIOApgPE8UuY1gORaIild4LzEnBwws=; b=AsozLfrkf8xwcq16MeE8YdrzCP9yICmfEkmlImYtLztPY5MHFoosqA/VFtGR3nlEqazk3J PVyI00hA5ADMpfBA== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 15/26] signal: Refactor send_sigqueue() References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:20 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner To handle posix timers which have their signal ignored via SIG_IGN properly it is required to requeue a ignored signal for delivery when SIG_IGN is lifted so the timer gets rearmed. Split the required code out of send_sigqueue() so it can be reused in context of sigaction(). While at it rename send_sigqueue() to posixtimer_send_sigqueue() so its clear what this is about. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- include/linux/posix-timers.h | 1=20 include/linux/sched/signal.h | 1=20 kernel/signal.c | 82 +++++++++++++++++++++++---------------= ----- kernel/time/posix-timers.c | 2 - 4 files changed, 47 insertions(+), 39 deletions(-) --- --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -109,6 +109,7 @@ static inline void posix_cputimers_rt_wa =20 void posixtimer_rearm_itimer(struct task_struct *p); bool posixtimer_init_sigqueue(struct sigqueue *q); +int posixtimer_send_sigqueue(struct k_itimer *tmr); bool posixtimer_deliver_signal(struct kernel_siginfo *info); void posixtimer_free_timer(struct k_itimer *timer); =20 --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -340,7 +340,6 @@ extern int send_sig(int, struct task_str extern int zap_other_threads(struct task_struct *p); extern struct sigqueue *sigqueue_alloc(void); extern void sigqueue_free(struct sigqueue *); -extern int send_sigqueue(struct sigqueue *, struct pid *, enum pid_type, i= nt si_private); extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *); =20 static inline void clear_notify_signal(void) --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1949,40 +1949,54 @@ void sigqueue_free(struct sigqueue *q) __sigqueue_free(q); } =20 -int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type,= int si_private) +static void posixtimer_queue_sigqueue(struct sigqueue *q, struct task_stru= ct *t, enum pid_type type) { - int sig =3D q->info.si_signo; struct sigpending *pending; + int sig =3D q->info.si_signo; + + signalfd_notify(t, sig); + pending =3D (type !=3D PIDTYPE_PID) ? &t->signal->shared_pending : &t->pe= nding; + list_add_tail(&q->list, &pending->list); + sigaddset(&pending->signal, sig); + complete_signal(sig, t, type); +} + +/* + * This function is used by POSIX timers to deliver a timer signal. + * Where type is PIDTYPE_PID (such as for timers with SIGEV_THREAD_ID + * set), the signal must be delivered to the specific thread (queues + * into t->pending). + * + * Where type is not PIDTYPE_PID, signals must be delivered to the + * process. In this case, prefer to deliver to current if it is in + * the same thread group as the target process, which avoids + * unnecessarily waking up a potentially idle task. + */ +static inline struct task_struct *posixtimer_get_target(struct k_itimer *t= mr) +{ + struct task_struct *t =3D pid_task(tmr->it_pid, tmr->it_pid_type); + + if (t && tmr->it_pid_type !=3D PIDTYPE_PID && same_thread_group(t, curren= t)) + t =3D current; + return t; +} + +int posixtimer_send_sigqueue(struct k_itimer *tmr) +{ + struct sigqueue *q =3D tmr->sigq; + int sig =3D q->info.si_signo; struct task_struct *t; unsigned long flags; int ret, result; =20 - if (WARN_ON_ONCE(!(q->flags & SIGQUEUE_PREALLOC))) - return 0; - if (WARN_ON_ONCE(q->info.si_code !=3D SI_TIMER)) - return 0; + guard(rcu)(); =20 - ret =3D -1; - rcu_read_lock(); - - /* - * This function is used by POSIX timers to deliver a timer signal. - * Where type is PIDTYPE_PID (such as for timers with SIGEV_THREAD_ID - * set), the signal must be delivered to the specific thread (queues - * into t->pending). - * - * Where type is not PIDTYPE_PID, signals must be delivered to the - * process. In this case, prefer to deliver to current if it is in - * the same thread group as the target process, which avoids - * unnecessarily waking up a potentially idle task. - */ - t =3D pid_task(pid, type); + t =3D posixtimer_get_target(tmr); if (!t) - goto ret; - if (type !=3D PIDTYPE_PID && same_thread_group(t, current)) - t =3D current; + return -1; + if (!likely(lock_task_sighand(t, &flags))) - goto ret; + return -1; =20 /* * Update @q::info::si_sys_private for posix timer signals with @@ -1990,7 +2004,7 @@ int send_sigqueue(struct sigqueue *q, st * decides based on si_sys_private whether to invoke * posixtimer_rearm() or not. */ - q->info.si_sys_private =3D si_private; + q->info.si_sys_private =3D tmr->it_signal_seq; =20 /* * Set the overrun count to zero unconditionally. The posix timer @@ -2021,27 +2035,21 @@ int send_sigqueue(struct sigqueue *q, st q->info.si_overrun =3D 0; =20 ret =3D 1; /* the signal is ignored */ - result =3D TRACE_SIGNAL_IGNORED; - if (!prepare_signal(sig, t, false)) + if (!prepare_signal(sig, t, false)) { + result =3D TRACE_SIGNAL_IGNORED; goto out; + } =20 ret =3D 0; if (unlikely(!list_empty(&q->list))) { result =3D TRACE_SIGNAL_ALREADY_PENDING; goto out; } - - signalfd_notify(t, sig); - pending =3D (type !=3D PIDTYPE_PID) ? &t->signal->shared_pending : &t->pe= nding; - list_add_tail(&q->list, &pending->list); - sigaddset(&pending->signal, sig); - complete_signal(sig, t, type); + posixtimer_queue_sigqueue(q, t, tmr->it_pid_type); result =3D TRACE_SIGNAL_DELIVERED; out: - trace_signal_generate(sig, &q->info, t, type !=3D PIDTYPE_PID, result); + trace_signal_generate(sig, &q->info, t, tmr->it_pid_type !=3D PIDTYPE_PID= , result); unlock_task_sighand(t, &flags); -ret: - rcu_read_unlock(); return ret; } #endif /* CONFIG_POSIX_TIMERS */ --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -307,7 +307,7 @@ int posix_timer_queue_signal(struct k_it } timr->it_status =3D state; =20 - ret =3D send_sigqueue(timr->sigq, timr->it_pid, timr->it_pid_type, timr->= it_signal_seq); + ret =3D posixtimer_send_sigqueue(timr); /* If we failed to send the signal the timer stops. */ return ret > 0; } From nobody Thu Nov 28 13:41:55 2024 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 D804A1A08D7 for ; Tue, 1 Oct 2024 08:42:23 +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=1727772145; cv=none; b=jgc3ZG70tBbXmHs/rU8JCI8253/mNaSDC7thX2aU8H96860hpq+wnubV79/NLJm008NjHHRHB6Aa5XOpu7WJbjxd/F8wPCchQ4sCqnX8WdD5Si69cYQ8nGhX16cBv1czSPMNfnrv9E5RfgFLwsqgsozng0FahFiQcdEFBKM+Z3g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772145; c=relaxed/simple; bh=jmiVBba5EyouQAyF/JsSn4pK9BZLxqcgvMNhqRj+3UE=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=O33LLLkx7LTOfmOJRfx6JcYlx1djrQ1vTvQR2Y6Jtvw3JrpsJ4ElGVg+glVDWhuUpVRoPGFl0L11WyjOdw0uyvKjlN6yUQ3CXt/Ywig5M0wlIIynQZCBjy/ReLK5oFcofEBHfSS4N8iJK+CkyiPi8+AeMuD5VwSecUYtTu4nj+c= 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=Zix3ZTsb; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ehsTybQh; 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="Zix3ZTsb"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ehsTybQh" Message-ID: <20241001083836.220867629@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772142; 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=+v67TyAZ9r1vym76xiHeHNazqzzBfBwFWzGymjqyf/8=; b=Zix3ZTsbxfrW2gQYsm/MTDceotkBNH3mep9SqXUVZnUNZ9XXCm4Q2mgGxpSxPDATQE/DOx G2uP1rRmb1/+WMW6GoSjJhtVvHk1gafpqROswOy+pwyI2WtKIwHmWWC6/X6mgpsO87kmVF NxGCbdpkTkPd5XRnkbvdjhu1BLcFKZ/0xSj6tPnYKP+f3ESmfrP/HnthEkcl8f2WlMOibq FYNxJ1qp0XkZ9aGENL2g9t51dGCClY7+pf8OM3jLj0ucMYXHxcdLZRAoaJXaGc04Jz3YeN 6trzaRqihIA6srrwO2vjp/rt1cc9uF1YIvtsliYWNUpycTERNh8WGWCo4VZ2jA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772142; 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=+v67TyAZ9r1vym76xiHeHNazqzzBfBwFWzGymjqyf/8=; b=ehsTybQhcOZGpgyBNzQxJCncOoP7xrgIMbuSx4Sn7ScE41dVXzbjZRE4HKppOBBLoSXORd dGGvpNhhCEU51eDA== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 16/26] signal: Replace resched_timer logic References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:21 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for handling ignored posix timer signals correctly and embedding the sigqueue struct into struct k_itimer, hand down a pointer to the sigqueue struct into posix_timer_deliver_signal() instead of just having a boolean flag. No functional change. Suggested-by: Eric W. Biederman Signed-off-by: Thomas Gleixner Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- V5: New patch --- include/linux/posix-timers.h | 5 +++-- kernel/signal.c | 24 ++++++++++++------------ kernel/time/posix-timers.c | 2 +- 3 files changed, 16 insertions(+), 15 deletions(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -110,7 +110,7 @@ static inline void posix_cputimers_rt_wa void posixtimer_rearm_itimer(struct task_struct *p); bool posixtimer_init_sigqueue(struct sigqueue *q); int posixtimer_send_sigqueue(struct k_itimer *tmr); -bool posixtimer_deliver_signal(struct kernel_siginfo *info); +bool posixtimer_deliver_signal(struct kernel_siginfo *info, struct sigqueu= e *timer_sigq); void posixtimer_free_timer(struct k_itimer *timer); =20 /* Init task static initializer */ @@ -135,7 +135,8 @@ static inline void posix_cputimers_init( static inline void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit) { } static inline void posixtimer_rearm_itimer(struct task_struct *p) { } -static inline bool posixtimer_deliver_signal(struct kernel_siginfo *info) = { return false; } +static inline bool posixtimer_deliver_signal(struct kernel_siginfo *info, + struct sigqueue *timer_sigq) { return false; } static inline void posixtimer_free_timer(struct k_itimer *timer) { } #endif =20 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -545,7 +545,7 @@ bool unhandled_signal(struct task_struct } =20 static void collect_signal(int sig, struct sigpending *list, kernel_siginf= o_t *info, - bool *resched_timer) + struct sigqueue **timer_sigq) { struct sigqueue *q, *first =3D NULL; =20 @@ -568,10 +568,10 @@ static void collect_signal(int sig, stru list_del_init(&first->list); copy_siginfo(info, &first->info); =20 - *resched_timer =3D (first->flags & SIGQUEUE_PREALLOC) && - (info->si_code =3D=3D SI_TIMER); - - __sigqueue_free(first); + if (unlikely((first->flags & SIGQUEUE_PREALLOC) && (info->si_code =3D=3D= SI_TIMER))) + *timer_sigq =3D first; + else + __sigqueue_free(first); } else { /* * Ok, it wasn't in the queue. This must be @@ -588,12 +588,12 @@ static void collect_signal(int sig, stru } =20 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask, - kernel_siginfo_t *info, bool *resched_timer) + kernel_siginfo_t *info, struct sigqueue **timer_sigq) { int sig =3D next_signal(pending, mask); =20 if (sig) - collect_signal(sig, pending, info, resched_timer); + collect_signal(sig, pending, info, timer_sigq); return sig; } =20 @@ -604,19 +604,19 @@ static int __dequeue_signal(struct sigpe */ int dequeue_signal(sigset_t *mask, kernel_siginfo_t *info, enum pid_type *= type) { + struct sigqueue *timer_sigq =3D NULL; struct task_struct *tsk =3D current; - bool resched_timer =3D false; int signr; =20 lockdep_assert_held(&tsk->sighand->siglock); =20 again: *type =3D PIDTYPE_PID; - signr =3D __dequeue_signal(&tsk->pending, mask, info, &resched_timer); + signr =3D __dequeue_signal(&tsk->pending, mask, info, &timer_sigq); if (!signr) { *type =3D PIDTYPE_TGID; signr =3D __dequeue_signal(&tsk->signal->shared_pending, - mask, info, &resched_timer); + mask, info, &timer_sigq); =20 if (unlikely(signr =3D=3D SIGALRM)) posixtimer_rearm_itimer(tsk); @@ -642,8 +642,8 @@ int dequeue_signal(sigset_t *mask, kerne current->jobctl |=3D JOBCTL_STOP_DEQUEUED; } =20 - if (IS_ENABLED(CONFIG_POSIX_TIMERS) && unlikely(resched_timer)) { - if (!posixtimer_deliver_signal(info)) + if (IS_ENABLED(CONFIG_POSIX_TIMERS) && unlikely(timer_sigq)) { + if (!posixtimer_deliver_signal(info, timer_sigq)) goto again; } =20 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -253,7 +253,7 @@ static void common_hrtimer_rearm(struct * This function is called from the signal delivery code. It decides * whether the signal should be dropped and rearms interval timers. */ -bool posixtimer_deliver_signal(struct kernel_siginfo *info) +bool posixtimer_deliver_signal(struct kernel_siginfo *info, struct sigqueu= e *timer_sigq) { struct k_itimer *timr; unsigned long flags; From nobody Thu Nov 28 13:41:55 2024 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 E4B891A0B02 for ; Tue, 1 Oct 2024 08:42:24 +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=1727772146; cv=none; b=XkzQ3tHDWLwWhHMJBgA/yvPkAFRRTbNtaF423oeaQgJR61TQ/LwS0JRZf5mV5wYC2jBIe3HjQf1c7FV5JWphhBTjGzIYPw6pcas0Hn/JYf4Sl5tlCVEhO4DRN9fxXF6k3QypQ72HZEQq2GJ0JCo78mz/Z8Ds9qHEs9vOAtLyUvc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772146; c=relaxed/simple; bh=EDt+z1boq+EZazzRjQU7EEniOFKAT4WDIlnswMH1CwU=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=c0wZS4W2KytFUXAdSkmBtEvcVJ52gZkRwEhg/fEE+l6F4DSGkHQ2xeGKTIMMWAW3M2XAVSXXOnQsenYmSU/zedg0icM5GNUCviERoMqlppOBQpIxxh8XZaj3wCvM55rFG85tdbPQtkR7E7ROH4H4Wxk1snTO9+b3kOtTDo445YQ= 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=KziddKtL; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=CixxJh80; 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="KziddKtL"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="CixxJh80" Message-ID: <20241001083836.279523831@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772143; 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=e8jpo55ujy2wb57cUnYu/wrCBSAvC3muaE/2r60JFug=; b=KziddKtLwfMETUAICt+0bsxd8j6pygYkvMzB65hil1ogMxq+ypEeMD7NkMZDeL76Tzzu5n w5WVpgrychmaVmEa3Vh8zcUJdL2qZ9qUYjp5hiyUMyHE2RLsbJNzofBNqx3QBu7aJakvKA 24bCPiL3MwS2cE/QZNtarP+2aUaky9sJ0KQQX5vWj+1DpFxrBo/k1wmF7F2YaAK1bWpqIe 1b5iRfvhVLwXwoMc0K9dCV6fUyBfgegZFXjSnpqkVdAOLotq4/63y2GziJoJspR+yPUr4l lKuUZywvmzdTlImF1earCKq+AvCVFM7sbIXojjJMq0kLJl/CrtCFIPgBpGaSPQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772143; 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=e8jpo55ujy2wb57cUnYu/wrCBSAvC3muaE/2r60JFug=; b=CixxJh80Xhoq5mSQjj9vtjtsjz7KEg2LrDUGXWxLbI0G2SRpYkWQPp0HeGB84hzgyMFTp2 jmSS4Yrp7l8AtTAw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 17/26] posix-timers: Embed sigqueue in struct k_itimer References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:23 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner To cure the SIG_IGN handling for posix interval timers, the preallocated sigqueue needs to be embedded into struct k_itimer to prevent life time races of all sorts. Now that the prerequisites are in place, embed the sigqueue into struct k_itimer and fixup the relevant usage sites. Aside of preparing for proper SIG_IGN handling, this spares an extra allocation. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- fs/proc/base.c | 4 +-- include/linux/posix-timers.h | 23 +++++++++++++++-- kernel/signal.c | 9 +++++- kernel/time/posix-timers.c | 57 ++++++++++++++++++++++++++------------= ----- 4 files changed, 65 insertions(+), 28 deletions(-) --- --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -2553,8 +2553,8 @@ static int show_timer(struct seq_file *m =20 seq_printf(m, "ID: %d\n", timer->it_id); seq_printf(m, "signal: %d/%px\n", - timer->sigq->info.si_signo, - timer->sigq->info.si_value.sival_ptr); + timer->sigq.info.si_signo, + timer->sigq.info.si_value.sival_ptr); seq_printf(m, "notify: %s/%s.%d\n", nstr[notify & ~SIGEV_THREAD_ID], (notify & SIGEV_THREAD_ID) ? "tid" : "pid", --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -39,6 +39,8 @@ static inline int clockid_to_fd(const cl =20 #ifdef CONFIG_POSIX_TIMERS =20 +#include + /** * cpu_timer - Posix CPU timer representation for k_itimer * @node: timerqueue node to queue in the task/sig @@ -166,7 +168,7 @@ static inline void posix_cputimers_init_ * @it_pid: The pid of the process/task targeted by the signal * @it_process: The task to wakeup on clock_nanosleep (CPU timers) * @rcuref: Reference count for life time management - * @sigq: Pointer to preallocated sigqueue + * @sigq: Embedded sigqueue * @it: Union representing the various posix timer type * internals. * @rcu: RCU head for freeing the timer. @@ -190,7 +192,7 @@ struct k_itimer { struct pid *it_pid; struct task_struct *it_process; }; - struct sigqueue *sigq; + struct sigqueue sigq; rcuref_t rcuref; union { struct { @@ -218,6 +220,23 @@ static inline void posixtimer_putref(str if (rcuref_put(&tmr->rcuref)) posixtimer_free_timer(tmr); } + +static inline void posixtimer_sigqueue_getref(struct sigqueue *q) +{ + struct k_itimer *tmr =3D container_of(q, struct k_itimer, sigq); + + WARN_ON_ONCE(!rcuref_get(&tmr->rcuref)); +} + +static inline void posixtimer_sigqueue_putref(struct sigqueue *q) +{ + struct k_itimer *tmr =3D container_of(q, struct k_itimer, sigq); + + posixtimer_putref(tmr); +} +#else /* CONFIG_POSIX_TIMERS */ +static inline void posixtimer_sigqueue_getref(struct sigqueue *q) { } +static inline void posixtimer_sigqueue_putref(struct sigqueue *q) { } #endif /* !CONFIG_POSIX_TIMERS */ =20 #endif --- a/kernel/signal.c +++ b/kernel/signal.c @@ -460,8 +460,10 @@ static struct sigqueue *__sigqueue_alloc =20 static void __sigqueue_free(struct sigqueue *q) { - if (q->flags & SIGQUEUE_PREALLOC) + if (q->flags & SIGQUEUE_PREALLOC) { + posixtimer_sigqueue_putref(q); return; + } if (q->ucounts) { dec_rlimit_put_ucounts(q->ucounts, UCOUNT_RLIMIT_SIGPENDING); q->ucounts =3D NULL; @@ -1981,7 +1983,7 @@ static inline struct task_struct *posixt =20 int posixtimer_send_sigqueue(struct k_itimer *tmr) { - struct sigqueue *q =3D tmr->sigq; + struct sigqueue *q =3D &tmr->sigq; int sig =3D q->info.si_signo; struct task_struct *t; unsigned long flags; @@ -2040,9 +2042,12 @@ int posixtimer_send_sigqueue(struct k_it =20 ret =3D 0; if (unlikely(!list_empty(&q->list))) { + /* This holds a reference count already */ result =3D TRACE_SIGNAL_ALREADY_PENDING; goto out; } + + posixtimer_sigqueue_getref(q); posixtimer_queue_sigqueue(q, t, tmr->it_pid_type); result =3D TRACE_SIGNAL_DELIVERED; out: --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -251,12 +251,13 @@ static void common_hrtimer_rearm(struct =20 /* * This function is called from the signal delivery code. It decides - * whether the signal should be dropped and rearms interval timers. + * whether the signal should be dropped and rearms interval timers. The + * timer can be unconditionally accessed as there is a reference held on + * it. */ bool posixtimer_deliver_signal(struct kernel_siginfo *info, struct sigqueu= e *timer_sigq) { - struct k_itimer *timr; - unsigned long flags; + struct k_itimer *timr =3D container_of(timer_sigq, struct k_itimer, sigq); bool ret =3D false; =20 /* @@ -264,12 +265,14 @@ bool posixtimer_deliver_signal(struct ke * timr::it_lock. Keep interrupts disabled. */ spin_unlock(¤t->sighand->siglock); + spin_lock(&timr->it_lock); =20 - timr =3D lock_timer(info->si_tid, &flags); - if (!timr) - goto out; - - if (timr->it_signal_seq !=3D info->si_sys_private) + /* + * Check if the timer is still alive or whether it got modified + * since the signal was queued. In either case, don't rearm and + * drop the signal. + */ + if (!timr->it_signal || timr->it_signal_seq !=3D info->si_sys_private) goto out_unlock; =20 if (timr->it_interval && timr->it_status =3D=3D POSIX_TIMER_REQUEUE_PENDI= NG) { @@ -285,8 +288,10 @@ bool posixtimer_deliver_signal(struct ke ret =3D true; =20 out_unlock: - unlock_timer(timr, flags); -out: + spin_unlock(&timr->it_lock); + /* Drop the reference which was acquired when the signal was queued */ + posixtimer_putref(timr); + spin_lock(¤t->sighand->siglock); =20 /* Don't expose the si_sys_private value to userspace */ @@ -404,17 +409,17 @@ static struct pid *good_sigevent(sigeven } } =20 -static struct k_itimer * alloc_posix_timer(void) +static struct k_itimer *alloc_posix_timer(void) { struct k_itimer *tmr =3D kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL= ); =20 if (!tmr) return tmr; - if (unlikely(!(tmr->sigq =3D sigqueue_alloc()))) { + + if (unlikely(!posixtimer_init_sigqueue(&tmr->sigq))) { kmem_cache_free(posix_timers_cache, tmr); return NULL; } - clear_siginfo(&tmr->sigq->info); rcuref_init(&tmr->rcuref, 1); return tmr; } @@ -429,7 +434,8 @@ static void k_itimer_rcu_free(struct rcu void posixtimer_free_timer(struct k_itimer *tmr) { put_pid(tmr->it_pid); - sigqueue_free(tmr->sigq); + if (tmr->sigq.ucounts) + dec_rlimit_put_ucounts(tmr->sigq.ucounts, UCOUNT_RLIMIT_SIGPENDING); call_rcu(&tmr->rcu, k_itimer_rcu_free); } =20 @@ -491,13 +497,13 @@ static int do_timer_create(clockid_t whi goto out; } new_timer->it_sigev_notify =3D event->sigev_notify; - new_timer->sigq->info.si_signo =3D event->sigev_signo; - new_timer->sigq->info.si_value =3D event->sigev_value; + new_timer->sigq.info.si_signo =3D event->sigev_signo; + new_timer->sigq.info.si_value =3D event->sigev_value; } else { new_timer->it_sigev_notify =3D SIGEV_SIGNAL; - new_timer->sigq->info.si_signo =3D SIGALRM; - memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t)); - new_timer->sigq->info.si_value.sival_int =3D new_timer->it_id; + new_timer->sigq.info.si_signo =3D SIGALRM; + memset(&new_timer->sigq.info.si_value, 0, sizeof(sigval_t)); + new_timer->sigq.info.si_value.sival_int =3D new_timer->it_id; new_timer->it_pid =3D get_pid(task_tgid(current)); } =20 @@ -506,8 +512,8 @@ static int do_timer_create(clockid_t whi else new_timer->it_pid_type =3D PIDTYPE_TGID; =20 - new_timer->sigq->info.si_tid =3D new_timer->it_id; - new_timer->sigq->info.si_code =3D SI_TIMER; + new_timer->sigq.info.si_tid =3D new_timer->it_id; + new_timer->sigq.info.si_code =3D SI_TIMER; =20 if (copy_to_user(created_timer_id, &new_timer_id, sizeof (new_timer_id)))= { error =3D -EFAULT; @@ -591,7 +597,14 @@ static struct k_itimer *__lock_timer(tim * 1) Set timr::it_signal to NULL with timr::it_lock held * 2) Release timr::it_lock * 3) Remove from the hash under hash_lock - * 4) Call RCU for removal after the grace period + * 4) Put the reference count. + * + * The reference count might not drop to zero if timr::sigq is + * queued. In that case the signal delivery or flush will put the + * last reference count. + * + * When the reference count reaches zero, the timer is scheduled + * for RCU removal after the grace period. * * Holding rcu_read_lock() accross the lookup ensures that * the timer cannot be freed. From nobody Thu Nov 28 13:41:55 2024 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 C558819D07E for ; Tue, 1 Oct 2024 08:42:26 +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=1727772148; cv=none; b=KMZsS/yGuCM07X/eRNvHfE4aVtBaztfi1DUKk08lnGyfFx13qngT8g6yDfVTc6/abFcbgyKNcp8oSBaP5v5HM6+8waOqNGlp9AANF50n6DUFRxtnutt1zKRyP9s6nR9TMNUFE9W6/qPihweifd1OcN2c6kVBnx6dbUNUJ3sPF7E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772148; c=relaxed/simple; bh=Q+5fQp4CRLf1u9EedtTHivVyOa9GnzN5PjVJvF5qTRI=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=X5gxw03k26JdKfgzaqVF7ossDbHgzZ1f1cTxUCmsC/BBuVUNw2hIybyHotbQWP9X9ht6WymmvyTQwfOVSaxz4SHaFIoFctXBxEVqv/3vC0Q0t8/THVwGWiOJ4lQuEcIyYqvFQoyVHXtla/Y8uHLDT2XrDkUSqGC4P4D+OdP0HzY= 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=pSTPWEcL; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=CmqgI1dH; 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="pSTPWEcL"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="CmqgI1dH" Message-ID: <20241001083836.337735372@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772145; 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=PbQIyx0qvrA9J8zJavAPM3ec78SoPgbfNXoubPfDKSw=; b=pSTPWEcL+ADwdAgMjB6f/1eTL95tkilgbmMaEunoaIvQvLZKEVemdviAo19eYrWDJOyvSC lOAeT4OnRiATFEdHvMpvXvefkDdkcdQYDDO7/cGCsoVlLMclOUUoSPASRIyIlu8X6z4nqX yaaT47oOe88IlulqmlSHaEGBCiSQGMF/s+uoHn/HE4uKpbwNbXfsq9L39GlBTVVdeWG2EI 3cNbDlsFnIv/Q8ble8yqBvT2Pk+oI0f9nvciDiczDJp/USt1M/A4++FHyUanIn0/wRimrX dV7z0Ut8Jmm3yT+GW5sDhv+DMkdDU90RnLP93xOVpfHLwtE1ZQY4oaOpATiqOA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772145; 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=PbQIyx0qvrA9J8zJavAPM3ec78SoPgbfNXoubPfDKSw=; b=CmqgI1dHS6I/a9u9YKr3li8HQSOh6xkZ1ck6654sGpl5P0O0YPszavZAXLqGnmx+BhiIZi 5kpsB1PSIe3mYqBQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 18/26] signal: Cleanup unused posix-timer leftovers References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:24 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner Remove the leftovers of sigqueue preallocation as it's not longer used. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- include/linux/sched/signal.h | 2 -- kernel/signal.c | 39 ++++----------------------------------- 2 files changed, 4 insertions(+), 37 deletions(-) --- --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -338,8 +338,6 @@ extern void force_fatal_sig(int); extern void force_exit_sig(int); extern int send_sig(int, struct task_struct *, int); extern int zap_other_threads(struct task_struct *p); -extern struct sigqueue *sigqueue_alloc(void); -extern void sigqueue_free(struct sigqueue *); extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *); =20 static inline void clear_notify_signal(void) --- a/kernel/signal.c +++ b/kernel/signal.c @@ -439,8 +439,8 @@ static void __sigqueue_init(struct sigqu * - this may be called without locks if and only if t =3D=3D current, oth= erwise an * appropriate lock must be held to stop the target task from exiting */ -static struct sigqueue *__sigqueue_alloc(int sig, struct task_struct *t, g= fp_t gfp_flags, - int override_rlimit, const unsigned int sigqueue_flags) +static struct sigqueue *sigqueue_alloc(int sig, struct task_struct *t, gfp= _t gfp_flags, + int override_rlimit) { struct ucounts *ucounts =3D sig_get_ucounts(t, sig, override_rlimit); struct sigqueue *q; @@ -454,7 +454,7 @@ static struct sigqueue *__sigqueue_alloc return NULL; } =20 - __sigqueue_init(q, ucounts, sigqueue_flags); + __sigqueue_init(q, ucounts, 0); return q; } =20 @@ -1062,7 +1062,7 @@ static int __send_signal_locked(int sig, else override_rlimit =3D 0; =20 - q =3D __sigqueue_alloc(sig, t, GFP_ATOMIC, override_rlimit, 0); + q =3D sigqueue_alloc(sig, t, GFP_ATOMIC, override_rlimit); =20 if (q) { list_add_tail(&q->list, &pending->list); @@ -1918,37 +1918,6 @@ bool posixtimer_init_sigqueue(struct sig return true; } =20 -struct sigqueue *sigqueue_alloc(void) -{ - return __sigqueue_alloc(-1, current, GFP_KERNEL, 0, SIGQUEUE_PREALLOC); -} - -void sigqueue_free(struct sigqueue *q) -{ - spinlock_t *lock =3D ¤t->sighand->siglock; - unsigned long flags; - - if (WARN_ON_ONCE(!(q->flags & SIGQUEUE_PREALLOC))) - return; - /* - * We must hold ->siglock while testing q->list - * to serialize with collect_signal() or with - * __exit_signal()->flush_sigqueue(). - */ - spin_lock_irqsave(lock, flags); - q->flags &=3D ~SIGQUEUE_PREALLOC; - /* - * If it is queued it will be freed when dequeued, - * like the "regular" sigqueue. - */ - if (!list_empty(&q->list)) - q =3D NULL; - spin_unlock_irqrestore(lock, flags); - - if (q) - __sigqueue_free(q); -} - static void posixtimer_queue_sigqueue(struct sigqueue *q, struct task_stru= ct *t, enum pid_type type) { struct sigpending *pending; From nobody Thu Nov 28 13:41:55 2024 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 F272E19D07B for ; Tue, 1 Oct 2024 08:42:27 +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=1727772149; cv=none; b=kxVQp5O53X1eMZ2KMx55TqKPTEMsUEK0PeEvAEGiNUXuwVvbzSZJgTVQvqpMrCZdIsAq/i/EiRHacRN/T5wAndUX3rpCU1QHYUqdfoRk2eWKM6J7xzAXBAq6wrqXi7KbhiCC/YAkFcoPFRd/WOnDWcvAZKf2YEV7S6apU3ntyo8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772149; c=relaxed/simple; bh=KuUukowE5tPhVZKOtVjcWUVBluppHCcX64h5AI5MFX8=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=rnyJkc6mX0ghfXjpvcIHVueadsraxqxL2t0d1QIOmwULmrsl3LGIFQm+CN1Qg7PUKzwrdb5/6Quw9TztAycOEWiPrdWyUou3Uk9zkGKVmUu/rHcrcu5ZDKch4msIeeOdNR2QywcKDGaztFjAwyumoMF2xjGf2U799m2Nhcer4t4= 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=NMycND5F; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=n4XpTcG6; 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="NMycND5F"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="n4XpTcG6" Message-ID: <20241001083836.396279339@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772146; 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=fiicOvJThNrOacm9GJVlSNcZNMkcKU4EAK0p4+HOCH0=; b=NMycND5F+wojaVM9glSL/sTKWdxuXIt4s0HQQi2YxKVj3bjJFFN2SJ5tPsXoYA24cD6Pw9 fajFLELvOOFLpxKHoy4Tu3D+c/KyTh9rgItagmOmi+3WoiUCCWvdiBlHPldWfGgI4w6vYW 0oNMvWToUYyTXV1dbJQHqQyd89C2m93VH94VtcqU76nbP98dPKvOeDNFjiQBdjuJdzgrjk 5e2PxbwEmggVdOrH+MHrHArAe3auxvSo259+PSAjqR0an8qigIswk7bfgDpqCY7heIBzZF VjOURfk1YBaw46FdHDM/mxfnIblbs246Gmk5WGxbYYyC9QdLEb5/j00TmP4nhw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772146; 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=fiicOvJThNrOacm9GJVlSNcZNMkcKU4EAK0p4+HOCH0=; b=n4XpTcG6klTjeJLRfRM1M9+khCCnmmQCDOwyTR+PdtzXd8h6mNwl3DzewligBCWJK5mQOX CzfH+AkKMUhO3wDw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 19/26] posix-timers: Move sequence logic into struct k_itimer References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:26 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The posix timer signal handling uses siginfo::si_sys_private for handling the sequence counter check. That indirection is not longer required and the sequence count value at signal queueing time can be stored in struct k_itimer itself. This removes the requirement of treating siginfo::si_sys_private special as it's now always zero as the kernel does not touch it anymore. Suggested-by: Eric W. Biederman Signed-off-by: Thomas Gleixner Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- V5: New patch --- include/linux/posix-timers.h | 2 ++ kernel/signal.c | 8 +++----- kernel/time/posix-timers.c | 5 +---- 3 files changed, 6 insertions(+), 9 deletions(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -162,6 +162,7 @@ static inline void posix_cputimers_init_ * @it_overrun: The overrun counter for pending signals * @it_overrun_last: The overrun at the time of the last delivered signal * @it_signal_seq: Sequence count to control signal delivery + * @it_sigqueue_seq: The sequence count at the point where the signal was = queued * @it_sigev_notify: The notify word of sigevent struct for signal delivery * @it_interval: The interval for periodic timers * @it_signal: Pointer to the creators signal struct @@ -184,6 +185,7 @@ struct k_itimer { s64 it_overrun; s64 it_overrun_last; unsigned int it_signal_seq; + unsigned int it_sigqueue_seq; int it_sigev_notify; enum pid_type it_pid_type; ktime_t it_interval; --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1968,12 +1968,10 @@ int posixtimer_send_sigqueue(struct k_it return -1; =20 /* - * Update @q::info::si_sys_private for posix timer signals with - * sighand locked to prevent a race against dequeue_signal() which - * decides based on si_sys_private whether to invoke - * posixtimer_rearm() or not. + * Update @tmr::sigqueue_seq for posix timer signals with sighand + * locked to prevent a race against dequeue_signal(). */ - q->info.si_sys_private =3D tmr->it_signal_seq; + tmr->it_sigqueue_seq =3D tmr->it_signal_seq; =20 /* * Set the overrun count to zero unconditionally. The posix timer --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -272,7 +272,7 @@ bool posixtimer_deliver_signal(struct ke * since the signal was queued. In either case, don't rearm and * drop the signal. */ - if (!timr->it_signal || timr->it_signal_seq !=3D info->si_sys_private) + if (!timr->it_signal || timr->it_signal_seq !=3D timr->it_sigqueue_seq) goto out_unlock; =20 if (timr->it_interval && timr->it_status =3D=3D POSIX_TIMER_REQUEUE_PENDI= NG) { @@ -293,9 +293,6 @@ bool posixtimer_deliver_signal(struct ke posixtimer_putref(timr); =20 spin_lock(¤t->sighand->siglock); - - /* Don't expose the si_sys_private value to userspace */ - info->si_sys_private =3D 0; return ret; } From nobody Thu Nov 28 13:41:55 2024 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 B4E4C1BE23B for ; Tue, 1 Oct 2024 08:42:29 +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=1727772151; cv=none; b=ei+nROypK+tpcwDPFD7ReZFdkS0miNpaq8Y8otZaw6GTALWP56DloappuzNzv/BGxUlhHaBuNLgatXC6l5uS/imiB5IWKt0wFYG8s8U8688Nyz297F2fhrp0UILhW/Yzc88WCF/3bMVOjTQymHs2635eNyW+WKtkwctuTu8mJkE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772151; c=relaxed/simple; bh=3MAwcXstmu0gnWTYcr5kZDdejdNgPxaQb2XQWXWsj2o=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=SzpzZlNKXWoHjkC6N7Vb7DODANE5ibgXf/gXxbA5lXNjuYYUhRlwCTFNqIWcwlCtz1QiPNYTxiT7+SvNhVz0EpDR7ewlloQnDrJze8iTwnsgEhmfTHAQfceeBgnKd3qi1G2ixUGAaP6V3aGZm5ni4SOALumThKh4zBkTgpY2PTI= 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=4y0njSCC; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=lKMiFW8m; 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="4y0njSCC"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="lKMiFW8m" Message-ID: <20241001083836.455949409@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772148; 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=AJWNttDVPF/rmQe1Q1kyokOqlDn+dzynriewRiuGxO4=; b=4y0njSCCoazPD8CrhnKnH2PbIk5H5e5qqXeym3mIge9ZMK1LPOpnv00QOiU7/ezo82FfFF y4q67MUbWltCG726PQWD68Baa4bt4tvvbwATWnhedi/CbQ9dI5XpYMwMlMN6/sxgdFPa2I tg6FUQmHuYQdwMkl14d0ORcQy48IO2170O2HyYzBV/kDzvq02igVcYusLPsl5VPfNNZ8N5 aTRrHtNkL/MJ7+E8fO84ZJH2x4yl6vt30GbMLMkrwaIv6mvSyH17e9bi4ma75YYpG4Gl/x DB3b1K3mxs3YLA7K84jUpHcmC6ODsZ3vygbr+xdju4qpfscrMcdSgFS7bEfLsg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772148; 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=AJWNttDVPF/rmQe1Q1kyokOqlDn+dzynriewRiuGxO4=; b=lKMiFW8mIRTk3zjMM92oVXYwmjdzDnA1NdDkoePLWHfIXQFuqZ/xk5F+wgE8S7KiC0b4nl UnR9JesUDF6ijADw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 20/26] signal: Provide ignored_posix_timers list References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:27 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner To prepare for handling posix timer signals on sigaction(SIG_IGN) properly, add a list to task::signal. This list will be used to queue posix timers so their signal can be requeued when SIG_IGN is lifted later. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" Reviewed-by: Frederic Weisbecker --- include/linux/sched/signal.h | 1 + init/init_task.c | 5 +++-- kernel/fork.c | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) --- --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -138,6 +138,7 @@ struct signal_struct { /* POSIX.1b Interval Timers */ unsigned int next_posix_timer_id; struct hlist_head posix_timers; + struct hlist_head ignored_posix_timers; =20 /* ITIMER_REAL timer for the process */ struct hrtimer real_timer; --- a/init/init_task.c +++ b/init/init_task.c @@ -30,8 +30,9 @@ static struct signal_struct init_signals .cred_guard_mutex =3D __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), .exec_update_lock =3D __RWSEM_INITIALIZER(init_signals.exec_update_lock), #ifdef CONFIG_POSIX_TIMERS - .posix_timers =3D HLIST_HEAD_INIT, - .cputimer =3D { + .posix_timers =3D HLIST_HEAD_INIT, + .ignored_posix_timers =3D HLIST_HEAD_INIT, + .cputimer =3D { .cputime_atomic =3D INIT_CPUTIME_ATOMIC, }, #endif --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1864,6 +1864,7 @@ static int copy_signal(unsigned long clo =20 #ifdef CONFIG_POSIX_TIMERS INIT_HLIST_HEAD(&sig->posix_timers); + INIT_HLIST_HEAD(&sig->ignored_posix_timers); hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); sig->real_timer.function =3D it_real_fn; #endif From nobody Thu Nov 28 13:41:55 2024 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 41BC41BE87C for ; Tue, 1 Oct 2024 08:42:30 +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=1727772155; cv=none; b=G0rLHXOWJPY66LP3LTZABIkKZMYrM/uryRT7FdoVpxTqKe64k9OgYq5Sjfe2FvDskGONECvxke1ht4qCwDWzygoTzOi8LnedsKS3gy2uP5vZVOo63W11YlggDlAEMH/LtJvLPCn21G16TOT1Cgeo7NrFkiJYe4dRkKSPUjYZhmE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772155; c=relaxed/simple; bh=ugm1bDvC3jut/vMuR/KbvcyMOYDJMmXjOVhnIPLEVhY=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=EBre4hZngpiY27S0eNKZZ9SYLIrUFBGrgPZmtS8pRALV85lze3vIePPtgWKAJDGG3vBGdpOVgz7g3BT1b7A0jiKmdodVOax0iBPVp7eGMWWR1kHBF2RlVVeK5OVNLMAK4tw/nsGBsvI/uwTmWup5cbZ4GUg/y4yqFvnHDkcqYyo= 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=hKYz36kI; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=LGvqb0J/; 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="hKYz36kI"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="LGvqb0J/" Message-ID: <20241001083836.514196709@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772149; 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=c6Y96xN0JlBMa7/gJ/CnZwn5sY28IT7kPPBfheQu2l4=; b=hKYz36kI2iyfXQ3c1cMeN4pPdtTLIZR87pn4Fo9u30PmcmzeaOHZOb+hP3TT98DhnvypOv xKEnTnFsbabOWPOWrFLX87FUtu6wroeaIM73Pyre8wtthy68nEGexQlOBsjRjvotsIUq7R DWOzB8OySBNz3GI7g1NqsQmd2jXwnBMwgMSsdiv/4mHwErzXV89By5hR9jfd3/iO9rVSqO 10QH4UZb5rFpt54/AkB+9JxYyRhODLHEf1bELTDq3X77c1YbruqmDSY4Iobeqegm5fBPNm N+2sFEl0tmeW1SBIAfQIGBX6wor3VPkbgB7kGYzQZP+1in9DzLdpQLz6uJe5+w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772149; 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=c6Y96xN0JlBMa7/gJ/CnZwn5sY28IT7kPPBfheQu2l4=; b=LGvqb0J/ea7/g7G34YLpcs07FMEivcCoRFm6d4pAFGTYsW3IEJpZkgTdS5tmB8rw82S1ek QLQcRzJXCP4D4LCw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 21/26] posix-timers: Handle ignored list on delete and exit References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:29 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner To handle posix timer signals on sigaction(SIG_IGN) properly, the timers will be queued on a separate ignored list. Add the necessary cleanup code for timer_delete() and exit_itimers(). Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" --- include/linux/posix-timers.h | 4 +++- kernel/time/posix-timers.c | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) --- --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -152,7 +152,8 @@ static inline void posix_cputimers_init_ =20 /** * struct k_itimer - POSIX.1b interval timer structure. - * @list: List head for binding the timer to signals->posix_timers + * @list: List node for binding the timer to tsk::signal::posix_timers + * @ignored_list: List node for tracking ignored timers in tsk::signal::ig= nored_posix_timers * @t_hash: Entry in the posix timer hash table * @it_lock: Lock protecting the timer * @kclock: Pointer to the k_clock struct handling this timer @@ -176,6 +177,7 @@ static inline void posix_cputimers_init_ */ struct k_itimer { struct hlist_node list; + struct hlist_node ignored_list; struct hlist_node t_hash; spinlock_t it_lock; const struct k_clock *kclock; --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -1031,6 +1031,18 @@ int common_timer_del(struct k_itimer *ti return 0; } =20 +/* + * If the deleted timer is on the ignored list, remove it and + * drop the associated reference. + */ +static inline void posix_timer_cleanup_ignored(struct k_itimer *tmr) +{ + if (!hlist_unhashed(&tmr->ignored_list)) { + hlist_del_init(&tmr->ignored_list); + posixtimer_putref(tmr); + } +} + static inline int timer_delete_hook(struct k_itimer *timer) { const struct k_clock *kc =3D timer->kclock; @@ -1063,6 +1075,7 @@ SYSCALL_DEFINE1(timer_delete, timer_t, t =20 spin_lock(¤t->sighand->siglock); hlist_del(&timer->list); + posix_timer_cleanup_ignored(timer); spin_unlock(¤t->sighand->siglock); /* * A concurrent lookup could check timer::it_signal lockless. It @@ -1114,6 +1127,8 @@ static void itimer_delete(struct k_itime } hlist_del(&timer->list); =20 + posix_timer_cleanup_ignored(timer); + /* * Setting timer::it_signal to NULL is technically not required * here as nothing can access the timer anymore legitimately via @@ -1146,6 +1161,11 @@ void exit_itimers(struct task_struct *ts /* The timers are not longer accessible via tsk::signal */ while (!hlist_empty(&timers)) itimer_delete(hlist_entry(timers.first, struct k_itimer, list)); + + /* Mop up timers which are on the ignored list */ + hlist_move_list(&tsk->signal->ignored_posix_timers, &timers); + while (!hlist_empty(&timers)) + posix_timer_cleanup_ignored(hlist_entry(timers.first, struct k_itimer, l= ist)); } =20 SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock, From nobody Thu Nov 28 13:41:55 2024 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 3BC7D1BE878 for ; Tue, 1 Oct 2024 08:42:32 +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=1727772153; cv=none; b=ucPITCORIcb9l5h/eKgOd763fCbe3fHS5wvWdORONNvSzDqhHnF+NwiNrkw8hdTYwjaYbQvz+HWgpuDIUeQrZP+87GIg5n+BXKsb+lhV9rrv8vkZy1wsfmVbG6cLBhplX+oQ6OJQmZP/fkiJ9kggLtdXCk5L65R1RjVJwH/G7yI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772153; c=relaxed/simple; bh=M677pHX34QOCyjxhMRo1zE6Z6Vi2BL9+felSGOBSgjE=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=LNJiK4P5O4dFhjMkSDRiEBpcDItOEj1oxgtfWgj9SE/So+lmwcKx/SpqIHV1vxT3hlCCezsh/sKzorRNhl0XoDexMnZ4LDfIcB8vqj9W0shxDBe/5gaJBXHrPgIoYIfma83PuD3B2BtUuPJjPUQx9YJkZ3RcSD3RXISbLtiHvhg= 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=XVYx/vGa; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=PkUKbGDP; 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="XVYx/vGa"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="PkUKbGDP" Message-ID: <20241001083836.572425978@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772150; 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=XK5UMo6tOwtpxK/g5DT8lDLUeDF/9ZRZjd7s5geMFrQ=; b=XVYx/vGaxUxdCWKw8yMOa+Thf8pykwPtD9d5ErqaKs0k88XarLZHEB5An5y2oDqu8P5NLW 2HP5zM2m0Gg0mK/hquNuPICPpckDcgli0LaUz3efyK2E5xb5tjDxsqBZKCaSH+jXPbEtXM 1udUftuv4cTav0vzZ49ftqi137JzfE+vuVV6fcybgj8qBsp/8rLwNmsfSA3+VTQMGaKNhQ YS5Hb5BUsrsBDx2WnVU3YuIu05NsfwgDoZSjlOtWT3G7xLffAM+C92TopkxMxtvGql2Byk u6cNbUjBa73Hw26Xk9qtM7UriLfrdMLPAdIxXn/kHbigJhYfLBm03UMr0aTkpA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772150; 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=XK5UMo6tOwtpxK/g5DT8lDLUeDF/9ZRZjd7s5geMFrQ=; b=PkUKbGDPLk9sQpWz7syY9xPfD4eHb8OzKktKONSPjj+KCBvqjhejKlbyEU2/crVhFSIs6w XFU+C+21V+sxmsBw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 22/26] signal: Handle ignored signals in do_sigaction(action != SIG_IGN) References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:30 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner When a real handler (including SIG_DFL) is installed for a signal, which had previously SIG_IGN set, then the list of ignored posix timers has to be checked for timers which are affected by this change. Add a list walk function which checks for the matching signal number and if found requeues the timers signal, so the timer is rearmed on signal delivery. Rearming the timer right away is not possible because that requires to drop sighand lock. No functional change as the counter part which queues the timers on the ignored list is still missing. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" --- kernel/signal.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++= +- 1 file changed, 52 insertions(+), 1 deletion(-) --- --- a/kernel/signal.c +++ b/kernel/signal.c @@ -2022,7 +2022,54 @@ int posixtimer_send_sigqueue(struct k_it unlock_task_sighand(t, &flags); return ret; } -#endif /* CONFIG_POSIX_TIMERS */ + +static void posixtimer_sig_unignore(struct task_struct *tsk, int sig) +{ + struct hlist_head *head =3D &tsk->signal->ignored_posix_timers; + struct hlist_node *tmp; + struct k_itimer *tmr; + + if (likely(hlist_empty(head))) + return; + + /* + * Rearming a timer with sighand lock held is not possible due to + * lock ordering vs. tmr::it_lock. Just stick the sigqueue back and + * let the signal delivery path deal with it whether it needs to be + * rearmed or not. This cannot be decided here w/o dropping sighand + * lock and creating a loop retry horror show. + */ + hlist_for_each_entry_safe(tmr, tmp , head, ignored_list) { + struct task_struct *target; + + /* + * tmr::sigq.info.si_signo is immutable, so accessing it + * without holding tmr::it_lock is safe. + */ + if (tmr->sigq.info.si_signo !=3D sig) + continue; + + hlist_del_init(&tmr->ignored_list); + + /* This should never happen and leaks a reference count */ + if (WARN_ON_ONCE(!list_empty(&tmr->sigq.list))) + continue; + + /* + * Get the target for the signal. If target is a thread and + * has exited by now, drop the reference count. + */ + guard(rcu)(); + target =3D posixtimer_get_target(tmr); + if (target) + posixtimer_queue_sigqueue(&tmr->sigq, target, tmr->it_pid_type); + else + posixtimer_putref(tmr); + } +} +#else /* CONFIG_POSIX_TIMERS */ +static inline void posixtimer_sig_unignore(struct task_struct *tsk, int si= g) { } +#endif /* !CONFIG_POSIX_TIMERS */ =20 void do_notify_pidfd(struct task_struct *task) { @@ -4200,6 +4247,8 @@ int do_sigaction(int sig, struct k_sigac sigaction_compat_abi(act, oact); =20 if (act) { + bool was_ignored =3D k->sa.sa_handler =3D=3D SIG_IGN; + sigdelsetmask(&act->sa.sa_mask, sigmask(SIGKILL) | sigmask(SIGSTOP)); *k =3D *act; @@ -4220,6 +4269,8 @@ int do_sigaction(int sig, struct k_sigac flush_sigqueue_mask(p, &mask, &p->signal->shared_pending); for_each_thread(p, t) flush_sigqueue_mask(p, &mask, &t->pending); + } else if (was_ignored) { + posixtimer_sig_unignore(p, sig); } } From nobody Thu Nov 28 13:41:55 2024 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 DB7BB1BF333 for ; Tue, 1 Oct 2024 08:42:33 +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=1727772155; cv=none; b=O9aydtSkn6umT0wM4RC/jq7Eai9v3awj769p8rEN/gRJncU8IdmUsJLjAGd/Yh9NMtKcEj9M9xm0LiYhTF8Z0ljmbXCdLJ4eEGismLy9T4u1QTqOv9mhJRSAFoXLIGD0QRQgCfpEYiCOhkQ5P5SZ0nrKQH7+7Dh9+RSPSpEzRFg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772155; c=relaxed/simple; bh=wdZ59129PILYRWjjVFMBE8pu7bJMfTyI28JUVeoeRaA=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=PUD3+uZxo8YxCd3+ikAfBUVuSDx/eDBo7KLIzZlzH3OxZ6gjhLt6IFREWvekBlOEwn1PNH+heQN/d7omYia5d8bn5edFmBEa1DCLY+A9v5b9VPbJmbCFTFGM/wTCxx3e8/d+bHN8VBb5sjRzliSeKCMYD+Z57wjkH4LBXZ+563c= 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=ZMfuzTSZ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=xnJ451YM; 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="ZMfuzTSZ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="xnJ451YM" Message-ID: <20241001083836.631221131@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772152; 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=6WJ5wulr4rlassEh+UEKQljccgKAlhW7SpWTOZzEt2M=; b=ZMfuzTSZIyWOd8X1f1gbHp/StNmdyQ9p201nWQRxdXbx4TwnTsBlrUv5nJJxIh0+55F4Ak tAWbPy7pXaNpj3IKs8Q3cc1eaT50GbBsDPjSecZ30oQVsBn3FhmLn8zby8VakEtqNlQPza RLlskq8e1bopq9pBUfryXwS/wrR1fL7xJH9y+7Np7sMtHQIbcIS4ttqrYNepbrwpMvGl3S PRCt63Xh3J4iVc4XpHfKJd5dAToskJKSv1ecLppRILSGmaY3EO1X2+afK1v3HlIhnzkPke elNAjw6OBbLCzQpyAXFPzEMkE+T4bx6Xbb5RHcH22ozqj5bMAlBNSpnYp4bX6A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772152; 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=6WJ5wulr4rlassEh+UEKQljccgKAlhW7SpWTOZzEt2M=; b=xnJ451YMtSI14+QiMo/ze6LLJnkoWT4bOaPjSZD2ZNZaoocrDF4PIipjw+vCmzZn2USEJg yMdqQlny7aMlpsDQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 23/26] signal: Queue ignored posixtimers on ignore list References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:32 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner Queue posixtimers which have their signal ignored on the ignored list: 1) When the timer fires and the signal has SIG_IGN set 2) When SIG_IGN is installed via sigaction() and a timer signal is already queued This completes the SIG_IGN handling and such timers are not longer self rearmed which avoids pointless wakeups. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" --- kernel/signal.c | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) --- --- a/kernel/signal.c +++ b/kernel/signal.c @@ -723,6 +723,16 @@ void signal_wake_up_state(struct task_st kick_process(t); } =20 +static inline void posixtimer_sig_ignore(struct task_struct *tsk, struct s= igqueue *q); + +static void sigqueue_free_ignored(struct task_struct *tsk, struct sigqueue= *q) +{ + if (likely(!(q->flags & SIGQUEUE_PREALLOC) || q->info.si_code !=3D SI_TIM= ER)) + __sigqueue_free(q); + else + posixtimer_sig_ignore(tsk, q); +} + /* Remove signals in mask from the pending set and queue. */ static void flush_sigqueue_mask(struct task_struct *p, sigset_t *mask, str= uct sigpending *s) { @@ -739,7 +749,7 @@ static void flush_sigqueue_mask(struct t list_for_each_entry_safe(q, n, &s->list, list) { if (sigismember(mask, q->info.si_signo)) { list_del_init(&q->list); - __sigqueue_free(q); + sigqueue_free_ignored(p, q); } } } @@ -1956,7 +1966,7 @@ int posixtimer_send_sigqueue(struct k_it int sig =3D q->info.si_signo; struct task_struct *t; unsigned long flags; - int ret, result; + int result; =20 guard(rcu)(); =20 @@ -2001,13 +2011,24 @@ int posixtimer_send_sigqueue(struct k_it */ q->info.si_overrun =3D 0; =20 - ret =3D 1; /* the signal is ignored */ if (!prepare_signal(sig, t, false)) { result =3D TRACE_SIGNAL_IGNORED; + + /* Paranoia check. Try to survive. */ + if (WARN_ON_ONCE(!list_empty(&q->list))) + goto out; + + if (hlist_unhashed(&tmr->ignored_list)) { + hlist_add_head(&tmr->ignored_list, &t->signal->ignored_posix_timers); + posixtimer_sigqueue_getref(q); + } goto out; } =20 - ret =3D 0; + /* This should never happen and leaks a reference count */ + if (WARN_ON_ONCE(!hlist_unhashed(&tmr->ignored_list))) + hlist_del_init(&tmr->ignored_list); + if (unlikely(!list_empty(&q->list))) { /* This holds a reference count already */ result =3D TRACE_SIGNAL_ALREADY_PENDING; @@ -2020,7 +2041,14 @@ int posixtimer_send_sigqueue(struct k_it out: trace_signal_generate(sig, &q->info, t, tmr->it_pid_type !=3D PIDTYPE_PID= , result); unlock_task_sighand(t, &flags); - return ret; + return 0; +} + +static inline void posixtimer_sig_ignore(struct task_struct *tsk, struct s= igqueue *q) +{ + struct k_itimer *tmr =3D container_of(q, struct k_itimer, sigq); + + hlist_add_head(&tmr->ignored_list, &tsk->signal->ignored_posix_timers); } =20 static void posixtimer_sig_unignore(struct task_struct *tsk, int sig) @@ -2068,6 +2096,7 @@ static void posixtimer_sig_unignore(stru } } #else /* CONFIG_POSIX_TIMERS */ +static inline void posixtimer_sig_ignore(struct task_struct *tsk, struct s= igqueue *q) { } static inline void posixtimer_sig_unignore(struct task_struct *tsk, int si= g) { } #endif /* !CONFIG_POSIX_TIMERS */ From nobody Thu Nov 28 13:41:55 2024 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 E10E21BF7F7 for ; Tue, 1 Oct 2024 08:42:35 +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=1727772157; cv=none; b=RItY+FX5ptYi7IYAy5cPqu5OFrts8eK7ZWaIOCmGWAClRqZGgoIQHtwU9bCGZy63lDFI9GhYP7h2gp+soTP7TsXV+MTUhEZWMWxDzZypdZv6C3gB9L117f2dNHWV1dwymCHwbE0Slg3tGw2dbuy9jTEpnM3rv1WIih8lYdgpdpA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772157; c=relaxed/simple; bh=duwzop1iEbg9uPOprlWbl3ogB4LJHXnaXlE9RCZU/EE=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=R/ELk+9byGyw0QuwHW/QSVAUG7eiIftUU/z5CL44YHcnDKZHuzqfQeUHh3MqHK/nd+mnD++cgbxJklCNGrOOcphp54xopbmXpaVDPJYtIMUmO6tws40Onz/yBBf1IIpoVWHX/BTogX2H27w1IiHqZWt4bGuqADJbX9LsZHVZd6Q= 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=M7TTJhzy; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=GCM63nuB; 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="M7TTJhzy"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="GCM63nuB" Message-ID: <20241001083836.690455381@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772153; 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=S7FOT/j0yjGCehM4hefRLoKmGu2M8mx1wfWixJm8yqg=; b=M7TTJhzyhXAS05Su167mV7bEm4pAnMpbZXT5B5hcVSJJuIxuZxzHUnkh5knYMpgy6rVNMz RKqdjuVSz0i3kFbuS2NLOuOOH1HXaUfbFRyBctzbVF3hOMUrutlAPGaUle9b47uabsJVAg 2GaHvBxyA9Q1s2OaanAq8lFAxZBWlfG4P6uEKSjiTWSDNQrz3d9rbxw9j0wmZyEdm7MOMP zWcOKXmEboxTVvrnWj76VMfhWy7l2LumrTUv4sprsSO9B1xJpB29WnVi9x2O0q+MRnqYJr NZryFM4py+PWX+UK5Zu3FHueDGErcPv0Tba8w0edo6FQWkIWj0XjY7XjYi8/WA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772153; 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=S7FOT/j0yjGCehM4hefRLoKmGu2M8mx1wfWixJm8yqg=; b=GCM63nuBmzcl4Hc4eH5Ojm71ZjB5F5oKmsobiLODy63WbBoO/JgUAoXDsqDc0HO8KboA7a qlqUx4NS9znzKWCw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 24/26] posix-timers: Cleanup SIG_IGN workaround leftovers References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:33 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner Now that ignored posix timer signals are requeued and the timers are rearmed on signal delivery the workaround to keep such timers alive and self rearm them is not longer required. Remove the relevant hacks and the not longer required return values from the related functions. The alarm timer workarounds will be cleaned up in a separate step. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" --- include/linux/posix-timers.h | 2 - kernel/signal.c | 7 +--- kernel/time/alarmtimer.c | 47 +++++------------------------ kernel/time/posix-cpu-timers.c | 18 ++--------- kernel/time/posix-timers.c | 65 +++---------------------------------= ----- kernel/time/posix-timers.h | 2 - 6 files changed, 23 insertions(+), 118 deletions(-) --- --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -111,7 +111,7 @@ static inline void posix_cputimers_rt_wa =20 void posixtimer_rearm_itimer(struct task_struct *p); bool posixtimer_init_sigqueue(struct sigqueue *q); -int posixtimer_send_sigqueue(struct k_itimer *tmr); +void posixtimer_send_sigqueue(struct k_itimer *tmr); bool posixtimer_deliver_signal(struct kernel_siginfo *info, struct sigqueu= e *timer_sigq); void posixtimer_free_timer(struct k_itimer *timer); =20 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1960,7 +1960,7 @@ static inline struct task_struct *posixt return t; } =20 -int posixtimer_send_sigqueue(struct k_itimer *tmr) +void posixtimer_send_sigqueue(struct k_itimer *tmr) { struct sigqueue *q =3D &tmr->sigq; int sig =3D q->info.si_signo; @@ -1972,10 +1972,10 @@ int posixtimer_send_sigqueue(struct k_it =20 t =3D posixtimer_get_target(tmr); if (!t) - return -1; + return; =20 if (!likely(lock_task_sighand(t, &flags))) - return -1; + return; =20 /* * Update @tmr::sigqueue_seq for posix timer signals with sighand @@ -2041,7 +2041,6 @@ int posixtimer_send_sigqueue(struct k_it out: trace_signal_generate(sig, &q->info, t, tmr->it_pid_type !=3D PIDTYPE_PID= , result); unlock_task_sighand(t, &flags); - return 0; } =20 static inline void posixtimer_sig_ignore(struct task_struct *tsk, struct s= igqueue *q) --- a/kernel/time/alarmtimer.c +++ b/kernel/time/alarmtimer.c @@ -197,28 +197,15 @@ static enum hrtimer_restart alarmtimer_f { struct alarm *alarm =3D container_of(timer, struct alarm, timer); struct alarm_base *base =3D &alarm_bases[alarm->type]; - unsigned long flags; - int ret =3D HRTIMER_NORESTART; - int restart =3D ALARMTIMER_NORESTART; =20 - spin_lock_irqsave(&base->lock, flags); - alarmtimer_dequeue(base, alarm); - spin_unlock_irqrestore(&base->lock, flags); + scoped_guard (spinlock_irqsave, &base->lock) + alarmtimer_dequeue(base, alarm); =20 if (alarm->function) - restart =3D alarm->function(alarm, base->get_ktime()); - - spin_lock_irqsave(&base->lock, flags); - if (restart !=3D ALARMTIMER_NORESTART) { - hrtimer_set_expires(&alarm->timer, alarm->node.expires); - alarmtimer_enqueue(base, alarm); - ret =3D HRTIMER_RESTART; - } - spin_unlock_irqrestore(&base->lock, flags); + alarm->function(alarm, base->get_ktime()); =20 trace_alarmtimer_fired(alarm, base->get_ktime()); - return ret; - + return HRTIMER_NORESTART; } =20 ktime_t alarm_expires_remaining(const struct alarm *alarm) @@ -567,30 +554,14 @@ static enum alarmtimer_type clock2alarm( * * Return: whether the timer is to be restarted */ -static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm, - ktime_t now) +static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm, kti= me_t now) { - struct k_itimer *ptr =3D container_of(alarm, struct k_itimer, - it.alarm.alarmtimer); - enum alarmtimer_restart result =3D ALARMTIMER_NORESTART; - unsigned long flags; - - spin_lock_irqsave(&ptr->it_lock, flags); + struct k_itimer *ptr =3D container_of(alarm, struct k_itimer, it.alarm.al= armtimer); =20 - if (posix_timer_queue_signal(ptr) && ptr->it_interval) { - /* - * Handle ignored signals and rearm the timer. This will go - * away once we handle ignored signals proper. Ensure that - * small intervals cannot starve the system. - */ - ptr->it_overrun +=3D __alarm_forward_now(alarm, ptr->it_interval, true); - ++ptr->it_signal_seq; - ptr->it_status =3D POSIX_TIMER_ARMED; - result =3D ALARMTIMER_RESTART; - } - spin_unlock_irqrestore(&ptr->it_lock, flags); + guard(spinlock_irqsave)(&ptr->it_lock); + posix_timer_queue_signal(ptr); =20 - return result; + return ALARMTIMER_NORESTART; } =20 /** --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -595,21 +595,11 @@ static void cpu_timer_fire(struct k_itim */ wake_up_process(timer->it_process); cpu_timer_setexpires(ctmr, 0); - } else if (!timer->it_interval) { - /* - * One-shot timer. Clear it as soon as it's fired. - */ + } else { posix_timer_queue_signal(timer); - cpu_timer_setexpires(ctmr, 0); - } else if (posix_timer_queue_signal(timer)) { - /* - * The signal did not get queued because the signal - * was ignored, so we won't get any callback to - * reload the timer. But we need to keep it - * ticking in case the signal is deliverable next time. - */ - posix_cpu_timer_rearm(timer); - ++timer->it_signal_seq; + /* Disable oneshot timers */ + if (!timer->it_interval) + cpu_timer_setexpires(ctmr, 0); } } =20 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -296,10 +296,9 @@ bool posixtimer_deliver_signal(struct ke return ret; } =20 -int posix_timer_queue_signal(struct k_itimer *timr) +void posix_timer_queue_signal(struct k_itimer *timr) { enum posix_timer_state state =3D POSIX_TIMER_DISARMED; - int ret; =20 lockdep_assert_held(&timr->it_lock); =20 @@ -309,9 +308,7 @@ int posix_timer_queue_signal(struct k_it } timr->it_status =3D state; =20 - ret =3D posixtimer_send_sigqueue(timr); - /* If we failed to send the signal the timer stops. */ - return ret > 0; + posixtimer_send_sigqueue(timr); } =20 /* @@ -324,62 +321,10 @@ int posix_timer_queue_signal(struct k_it static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer) { struct k_itimer *timr =3D container_of(timer, struct k_itimer, it.real.ti= mer); - enum hrtimer_restart ret =3D HRTIMER_NORESTART; - unsigned long flags; =20 - spin_lock_irqsave(&timr->it_lock, flags); - - if (posix_timer_queue_signal(timr)) { - /* - * The signal was not queued due to SIG_IGN. As a - * consequence the timer is not going to be rearmed from - * the signal delivery path. But as a real signal handler - * can be installed later the timer must be rearmed here. - */ - if (timr->it_interval !=3D 0) { - ktime_t now =3D hrtimer_cb_get_time(timer); - - /* - * FIXME: What we really want, is to stop this - * timer completely and restart it in case the - * SIG_IGN is removed. This is a non trivial - * change to the signal handling code. - * - * For now let timers with an interval less than a - * jiffy expire every jiffy and recheck for a - * valid signal handler. - * - * This avoids interrupt starvation in case of a - * very small interval, which would expire the - * timer immediately again. - * - * Moving now ahead of time by one jiffy tricks - * hrtimer_forward() to expire the timer later, - * while it still maintains the overrun accuracy - * for the price of a slight inconsistency in the - * timer_gettime() case. This is at least better - * than a timer storm. - * - * Only required when high resolution timers are - * enabled as the periodic tick based timers are - * automatically aligned to the next tick. - */ - if (IS_ENABLED(CONFIG_HIGH_RES_TIMERS)) { - ktime_t kj =3D TICK_NSEC; - - if (timr->it_interval < kj) - now =3D ktime_add(now, kj); - } - - timr->it_overrun +=3D hrtimer_forward(timer, now, timr->it_interval); - ret =3D HRTIMER_RESTART; - ++timr->it_signal_seq; - timr->it_status =3D POSIX_TIMER_ARMED; - } - } - - unlock_timer(timr, flags); - return ret; + guard(spinlock_irqsave)(&timr->it_lock); + posix_timer_queue_signal(timr); + return HRTIMER_NORESTART; } =20 static struct pid *good_sigevent(sigevent_t * event) --- a/kernel/time/posix-timers.h +++ b/kernel/time/posix-timers.h @@ -42,7 +42,7 @@ extern const struct k_clock clock_proces extern const struct k_clock clock_thread; extern const struct k_clock alarm_clock; =20 -int posix_timer_queue_signal(struct k_itimer *timr); +void posix_timer_queue_signal(struct k_itimer *timr); =20 void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_sett= ing); int common_timer_set(struct k_itimer *timr, int flags, From nobody Thu Nov 28 13:41:55 2024 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 EA0BA1BF814 for ; Tue, 1 Oct 2024 08:42:36 +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=1727772158; cv=none; b=uSeZI4ePji6IapovjsSGv5gZjtB3KBHfETecqH7UV/octpPoiBnMMCq24Yfu1uzLxkjfWd0qL3VYuhKEsPDclS1BrPUG27wMhcabkd6llksyH1XPn4y1czCpGOf5l5Zy1GFi0RpDjDnmV6tOvaiukHEci4uURvVumyueWAz4KTU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772158; c=relaxed/simple; bh=A8egy+nUGYQ0pNfWcftUljyWgjT+dpcfC5EDeO3tOqU=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=HNvSoIHecctYqm44NAoqYEZ8bhPG5AGEhfRNI1ZGnbBj0bMoXbhz4V15o09NFmRKVVKAHJOZ5jN5b/M2glqq+UsI8SFUIbzX20UlmWgzZX/fBS0gpSnFCJ74CZlkhRY05NYjrtGbcT4ToO4gGLwjN4745ekGOryg+RVqOVPMFts= 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=dDWDZw6l; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=KxvG1Imb; 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="dDWDZw6l"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="KxvG1Imb" Message-ID: <20241001083836.749282631@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772155; 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=+GGMvIt5z7I9VJm4esvwiz4Q8v92uRiYrO25Sjx4v2Q=; b=dDWDZw6lCkcvVw+BnBRxKDL4kZOJLAyMFFiObRxUAP3+JTaGL7VSG62Ua0XUF8cB3iO82U tSPv7DRIVmKegOuXtm3+1Q6qxW8T1m6r95Cia5EgjdmRX+nC28syCyFmKxMUEYx7fSFghO Pe844BjEJtCtk0Bc6X+azwsGsiE4zwfuehRVjcCElhZQueFWIeuBFhORg13KiBqJ9zvk8C 6Nvd1Pzg9kUG2RO78HhNqsUQ58c5hxFFc8VRhWxy1TYW9lfdeQ9fLHWLIhOR0MJxHkSSCV JZ/eG5X3J3eDYG+XM0Z43RZu8IWIUAiTNaHXUtVeueKJ3qPfm0md2qq/Qa758A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772155; 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=+GGMvIt5z7I9VJm4esvwiz4Q8v92uRiYrO25Sjx4v2Q=; b=KxvG1ImbWY2fyselSDsiI0EA/2L1AWuPgsM16FhncUlSjGfG1h22JwBdt5FGk2tKNgOD91 sZjPN31UgXY3RQBA== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 25/26] alarmtimers: Remove the throttle mechanism from alarm_forward_now() References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:34 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner Now that ignored posix timer signals are requeued and the timers are rearmed on signal delivery the workaround to keep such timers alive and self rearm them is not longer required. Remove the unused alarm timer parts. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" --- kernel/time/alarmtimer.c | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) --- --- a/kernel/time/alarmtimer.c +++ b/kernel/time/alarmtimer.c @@ -467,35 +467,11 @@ u64 alarm_forward(struct alarm *alarm, k } EXPORT_SYMBOL_GPL(alarm_forward); =20 -static u64 __alarm_forward_now(struct alarm *alarm, ktime_t interval, bool= throttle) +u64 alarm_forward_now(struct alarm *alarm, ktime_t interval) { struct alarm_base *base =3D &alarm_bases[alarm->type]; - ktime_t now =3D base->get_ktime(); - - if (IS_ENABLED(CONFIG_HIGH_RES_TIMERS) && throttle) { - /* - * Same issue as with posix_timer_fn(). Timers which are - * periodic but the signal is ignored can starve the system - * with a very small interval. The real fix which was - * promised in the context of posix_timer_fn() never - * materialized, but someone should really work on it. - * - * To prevent DOS fake @now to be 1 jiffy out which keeps - * the overrun accounting correct but creates an - * inconsistency vs. timer_gettime(2). - */ - ktime_t kj =3D NSEC_PER_SEC / HZ; =20 - if (interval < kj) - now =3D ktime_add(now, kj); - } - - return alarm_forward(alarm, now, interval); -} - -u64 alarm_forward_now(struct alarm *alarm, ktime_t interval) -{ - return __alarm_forward_now(alarm, interval, false); + return alarm_forward(alarm, base->get_ktime(), interval); } EXPORT_SYMBOL_GPL(alarm_forward_now); From nobody Thu Nov 28 13:41:55 2024 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 220341BFDF0 for ; Tue, 1 Oct 2024 08:42:38 +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=1727772159; cv=none; b=qqNXo6CXdrX3mb380xCNOaUxpEVH42oHp7MlyNskMsfwRxjllprdc7FZDKet12tWOgS3XuUf+TiCNzm2XNGqhtGC/mcIH9nxNG6dUuWJiJo57DD5IcLy1TC+vG7AYB1WJXUIJrD8Q6imprCXXARppVrn6WuFQAbN1GCpLf3lLrI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727772159; c=relaxed/simple; bh=TXC00ABcpsro3GpUOOhEK+ef2ngcIT2CMLF5uo2E3Tk=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=T/PR59jd1jldMeBxmQ8uFwQNePchQeNUi5059WmTscN+Mv9D5CZHpQjYP4DHK7kMUTMEzW4Dj5q2xSGFd7VLZE5thMYmQxaxDA8rSm4R+319U7+X4eQi55ulskMRl5koty1sr+LXWvS7VeG2xBspu0TIsyXlCgA5ifT2J3NuTDU= 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=TDiP57ZN; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=4UMk282i; 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="TDiP57ZN"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="4UMk282i" Message-ID: <20241001083836.808354550@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727772156; 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=GrZAvaBiWhIaEOQKVTEMW4jdMpei4mRp8VCjSB5qNKk=; b=TDiP57ZNkdzuB15E/pk5LQOu1si+ZNZAsPqwzrwd1JVNR44rIQ6N9iu07ulxaLvjD6lg2P aGbBw0dlUAY/xywl7D1QJr8eXgKGVHmUdhBf7l0COil7jsUiP6qUwtUXy/97ramD6bVfrW znRRKkdbyeBifrx2xnJEU86ZFpesnFSg2eKIPm51rcqzAv2lawjy8r1ZHX5pJz4VOKMRtV tki3eaEtotxkt5ZHFJNJAzYvfvxEmJYZIdt+GVe3Fq10d8cWGNgvc2X66e07Jw43n7VoiT 9GEFnQGiVWqPWlfZgnYq0FLaBx6BFOQl0EGJMMgQ5dts0dmMtypyE4Bps5y2AQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727772156; 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=GrZAvaBiWhIaEOQKVTEMW4jdMpei4mRp8VCjSB5qNKk=; b=4UMk282iQdokxFbAndQDPgj0pqKSShicbexsxULVi5UE3Tm0rzcJ7QjdWZeJQP+U1+742j u9m97c27RDMWOmCw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V5 26/26] alarmtimers: Remove return value from alarm functions References: <20241001083138.922192481@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 1 Oct 2024 10:42:36 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner Now that the SIG_IGN problem is solved in the core code, the alarmtimer callbacks do not require a return value anymore. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Acked-by: "Eric W. Biederman" --- drivers/power/supply/charger-manager.c | 3 +-- fs/timerfd.c | 4 +--- include/linux/alarmtimer.h | 10 ++-------- kernel/time/alarmtimer.c | 16 +++++----------- net/netfilter/xt_IDLETIMER.c | 4 +--- 5 files changed, 10 insertions(+), 27 deletions(-) --- --- a/drivers/power/supply/charger-manager.c +++ b/drivers/power/supply/charger-manager.c @@ -1412,10 +1412,9 @@ static inline struct charger_desc *cm_ge return dev_get_platdata(&pdev->dev); } =20 -static enum alarmtimer_restart cm_timer_func(struct alarm *alarm, ktime_t = now) +static void cm_timer_func(struct alarm *alarm, ktime_t now) { cm_timer_set =3D false; - return ALARMTIMER_NORESTART; } =20 static int charger_manager_probe(struct platform_device *pdev) --- a/fs/timerfd.c +++ b/fs/timerfd.c @@ -79,13 +79,11 @@ static enum hrtimer_restart timerfd_tmrp return HRTIMER_NORESTART; } =20 -static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm, - ktime_t now) +static void timerfd_alarmproc(struct alarm *alarm, ktime_t now) { struct timerfd_ctx *ctx =3D container_of(alarm, struct timerfd_ctx, t.alarm); timerfd_triggered(ctx); - return ALARMTIMER_NORESTART; } =20 /* --- a/include/linux/alarmtimer.h +++ b/include/linux/alarmtimer.h @@ -20,12 +20,6 @@ enum alarmtimer_type { ALARM_BOOTTIME_FREEZER, }; =20 -enum alarmtimer_restart { - ALARMTIMER_NORESTART, - ALARMTIMER_RESTART, -}; - - #define ALARMTIMER_STATE_INACTIVE 0x00 #define ALARMTIMER_STATE_ENQUEUED 0x01 =20 @@ -42,14 +36,14 @@ enum alarmtimer_restart { struct alarm { struct timerqueue_node node; struct hrtimer timer; - enum alarmtimer_restart (*function)(struct alarm *, ktime_t now); + void (*function)(struct alarm *, ktime_t now); enum alarmtimer_type type; int state; void *data; }; =20 void alarm_init(struct alarm *alarm, enum alarmtimer_type type, - enum alarmtimer_restart (*function)(struct alarm *, ktime_t)); + void (*function)(struct alarm *, ktime_t)); void alarm_start(struct alarm *alarm, ktime_t start); void alarm_start_relative(struct alarm *alarm, ktime_t start); void alarm_restart(struct alarm *alarm); --- a/kernel/time/alarmtimer.c +++ b/kernel/time/alarmtimer.c @@ -321,7 +321,7 @@ static int alarmtimer_resume(struct devi =20 static void __alarm_init(struct alarm *alarm, enum alarmtimer_type type, - enum alarmtimer_restart (*function)(struct alarm *, ktime_t)) + void (*function)(struct alarm *, ktime_t)) { timerqueue_init(&alarm->node); alarm->timer.function =3D alarmtimer_fired; @@ -337,7 +337,7 @@ static void * @function: callback that is run when the alarm fires */ void alarm_init(struct alarm *alarm, enum alarmtimer_type type, - enum alarmtimer_restart (*function)(struct alarm *, ktime_t)) + void (*function)(struct alarm *, ktime_t)) { hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid, HRTIMER_MODE_ABS); @@ -530,14 +530,12 @@ static enum alarmtimer_type clock2alarm( * * Return: whether the timer is to be restarted */ -static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm, kti= me_t now) +static void alarm_handle_timer(struct alarm *alarm, ktime_t now) { struct k_itimer *ptr =3D container_of(alarm, struct k_itimer, it.alarm.al= armtimer); =20 guard(spinlock_irqsave)(&ptr->it_lock); posix_timer_queue_signal(ptr); - - return ALARMTIMER_NORESTART; } =20 /** @@ -698,18 +696,14 @@ static int alarm_timer_create(struct k_i * @now: time at the timer expiration * * Wakes up the task that set the alarmtimer - * - * Return: ALARMTIMER_NORESTART */ -static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alar= m, - ktime_t now) +static void alarmtimer_nsleep_wakeup(struct alarm *alarm, ktime_t now) { struct task_struct *task =3D alarm->data; =20 alarm->data =3D NULL; if (task) wake_up_process(task); - return ALARMTIMER_NORESTART; } =20 /** @@ -761,7 +755,7 @@ static int alarmtimer_do_nsleep(struct a =20 static void alarm_init_on_stack(struct alarm *alarm, enum alarmtimer_type type, - enum alarmtimer_restart (*function)(struct alarm *, ktime_t)) + void (*function)(struct alarm *, ktime_t)) { hrtimer_init_on_stack(&alarm->timer, alarm_bases[type].base_clockid, HRTIMER_MODE_ABS); --- a/net/netfilter/xt_IDLETIMER.c +++ b/net/netfilter/xt_IDLETIMER.c @@ -107,14 +107,12 @@ static void idletimer_tg_expired(struct schedule_work(&timer->work); } =20 -static enum alarmtimer_restart idletimer_tg_alarmproc(struct alarm *alarm, - ktime_t now) +static void idletimer_tg_alarmproc(struct alarm *alarm, ktime_t now) { struct idletimer_tg *timer =3D alarm->data; =20 pr_debug("alarm %s expired\n", timer->attr.attr.name); schedule_work(&timer->work); - return ALARMTIMER_NORESTART; } =20 static int idletimer_check_sysfs_name(const char *name, unsigned int size)