Received: by 2002:ab2:784b:0:b0:1fd:adc2:8405 with SMTP id m11csp485237lqp; Mon, 10 Jun 2024 09:47:48 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUduk7MZyiRdO0rRJBvcNDrjd+vv1HhJBdGTGFYY4mHItExSczD84MkvBsep+Ez66H6VwUY94dEYZ6dAsMEFVqYnDgPDkQAb8WqO8kjrw== X-Google-Smtp-Source: AGHT+IEx7p9ebBeO30pq0zMNpQREq+DtlgRmlRITzPhoqXuvajWzVhxaNYcntmBwV7N7tNuJ6R1G X-Received: by 2002:a05:620a:2b87:b0:795:532f:3a82 with SMTP id af79cd13be357-797c32ad636mr25702885a.32.1718038067762; Mon, 10 Jun 2024 09:47:47 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718038067; cv=pass; d=google.com; s=arc-20160816; b=aG1WTvKX62Z3gbeNMyShoF783ADiTWG4jKHv3Nl7I6SjK+TXonqztZbwxu1l2TBLoA RtBrBx+Dvam96eUmbvdDoIWW7YADD03E+0IyxsL1KxTwoVPUae37NtKzCH1Hn6XAbekW 6Rs/bMUeBOzKeQX8izJhhTxhqUpAMS9H4Zgck/WPivr6uLGknDdayY/peQCRV+bIgVjU CKciMyDD+8f8KWcOQ/WsEKbS2Fg5LDbkzZpGjAT5vq9Afiz7YxbTZx3z1miXcLX0tnq/ dJO0pAt/4IBHCRrCXoqt/pvcM7Y2IZw1ihQ/A4G0wiwwbevuB9D+fWM/1pB/bZs4zJbC 62NQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=date:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:references:subject:cc:to:from:dkim-signature :dkim-signature:message-id; bh=Zqea4ZCKHXjl0dJ4sa3tZPhdgHz3TXIy35qUKT56zVA=; fh=joBs/8L+orz77ylyCy6wqNwwi35f7sYhLR+/TQjQjhI=; b=kUiWwaEmTlV1yvkLedlp2rDAP3YWQM1Cb3RtEPqg2T4UP4eiTEgy2DaHQyvJP1tTX8 W7aWsq7y2eIaVrudN9k6V4nDZ1B/Njtfr7hdVoNMsjDVzJBOKohzP+hQzLL94vAnp63D vYa889sEbTfI1GSfkjuWBWkPvvs14YWPgGhzDbfA6yoNx4O37zIYcLas3SnPv0mQ5sHL sGDhbbnUuXXPNeWGsrm6rDpnotU2XgR/0inNE421i/Br1ysoyJdK9zv0QY0j6eqqQrra JsL0PJeJvPVHbO8T4F7i2zOg48biYIYIxh2PSl270qMUcBJP8b9TOZ4Ao6Q4AnY5xANx AXCw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=3ViNPDQ7; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-208590-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208590-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id af79cd13be357-795494c281esi759425085a.732.2024.06.10.09.47.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 09:47:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-208590-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=3ViNPDQ7; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-208590-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208590-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 6D6331C229CB for ; Mon, 10 Jun 2024 16:47:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3C235156249; Mon, 10 Jun 2024 16:42:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="3ViNPDQ7"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="j01WXWii" 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 0F95E155C81 for ; Mon, 10 Jun 2024 16: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=1718037761; cv=none; b=pIPhK/bkDDeSxoFxYrJ/0ePPESc6IEuUG4CznbxrQ6zbO4Q5ti7D4dQmFSTO+qHVhHscWNl9qRZ6aPYQEI8pn3lSYQDPTK5nmIqFefKiVKBT9/TTyWDn6QYyOJX5irnzaAZ+xhFcdk6z5Ew3tbDPZwWybrHpkE9TtRPVNkIfY/M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718037761; c=relaxed/simple; bh=TKJn1dWiLSzYuFWlYalQBj85JnAn82gG7J9kjYz+D/0=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=HKdYuDtmh5TWqX3e46tZN4xTFHFNnNUom6PikEPOiPvuDBZwpVg85PnSSIzXQfi1lc5lwwavBo8WGMXKC5nQ2aWfK9CJkYdWEnrf6lkLHhbidGmnMZygig1gm3DGPt3ahXxmMa4TiJasvjsduBZ/x+P4DZDZSHKAOu/T9utyOVo= 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=3ViNPDQ7; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=j01WXWii; 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 Message-ID: <20240610164027.054318056@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1718037756; 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=Zqea4ZCKHXjl0dJ4sa3tZPhdgHz3TXIy35qUKT56zVA=; b=3ViNPDQ7Bmrk/dW+D4b9bRx/AvSv6cqxdKRrii3s9F7Jj7ltv+IcXEshMb9OoZQsszgjhy xbB+gnuDvf4oyEJ+CqdeBLGpQR4ewIpfOSx5/q0cTewfGDFobfyYWDp8X9oxwz2aloLH7a +ssTfBDplIC0W2M8AGpqTdI89PaHybBCZvpp2nAFs6kceV1bREsSAsIpSgHn7wfUYUKgqK 4j9p54ej0F+hRjUlX7dhFMOuiOt96LuoSKC5XHzZjUpVOjAFexiMdrXa3vkUTDFqftmFYm 3AU31BCXYIWZUgZIIXYzhclKwSKerhieuo/owJobW0AEOoUH6im8FTt9ZcT9Ag== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1718037756; 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=Zqea4ZCKHXjl0dJ4sa3tZPhdgHz3TXIy35qUKT56zVA=; b=j01WXWiioGQ8dVPp7vOYwLMy3v76VKK1jQ3v4RTF7W3ULnbsUpp3Zdra8lkzGmPkL7S9hL AowioZ1CAKfM8QAg== 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 V3 25/51] signal: Confine POSIX_TIMERS properly References: <20240610163452.591699700@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Mon, 10 Jun 2024 18:42:36 +0200 (CEST) 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 --- include/linux/posix-timers.h | 5 + kernel/signal.c | 125 +++++++++++++++---------------------------- kernel/time/itimer.c | 22 +++++++ kernel/time/posix-timers.c | 15 ++++- 4 files changed, 82 insertions(+), 85 deletions(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -100,6 +100,8 @@ static inline void posix_cputimers_rt_wa { pct->bases[CPUCLOCK_SCHED].nextevt = runtime; } +void posixtimer_rearm_itimer(struct task_struct *p); +void posixtimer_rearm(struct kernel_siginfo *info); /* 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 #ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK @@ -196,5 +200,4 @@ void set_process_cpu_timer(struct task_s int update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new); -void posixtimer_rearm(struct kernel_siginfo *info); #endif --- a/kernel/signal.c +++ b/kernel/signal.c @@ -478,42 +478,6 @@ void flush_signals(struct task_struct *t } EXPORT_SYMBOL(flush_signals); -#ifdef CONFIG_POSIX_TIMERS -static void __flush_itimer_signals(struct sigpending *pending) -{ - sigset_t signal, retain; - struct sigqueue *q, *n; - - signal = pending->signal; - sigemptyset(&retain); - - list_for_each_entry_safe(q, n, &pending->list, list) { - int sig = q->info.si_signo; - - if (likely(q->info.si_code != 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 = 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, kerne *type = PIDTYPE_TGID; signr = __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 == SIGALRM)) { - struct hrtimer *tmr = &tsk->signal->real_timer; - if (!hrtimer_is_queued(tmr) && - tsk->signal->it_real_incr != 0) { - hrtimer_forward(tmr, tmr->base->get_time(), - tsk->signal->it_real_incr); - hrtimer_restart(tmr); - } - } -#endif + if (unlikely(signr == SIGALRM)) + posixtimer_rearm_itimer(tsk); } recalc_sigpending(); @@ -682,22 +624,12 @@ int dequeue_signal(sigset_t *mask, kerne */ current->jobctl |= 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); - /* Don't expose the si_sys_private value to userspace */ - info->si_sys_private = 0; + if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { + if (unlikely(resched_timer)) + posixtimer_rearm(info); } -#endif + return signr; } EXPORT_SYMBOL_GPL(dequeue_signal); @@ -1924,15 +1856,45 @@ int kill_pid(struct pid *pid, int sig, i } EXPORT_SYMBOL(kill_pid); +#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 = pending->signal; + sigemptyset(&retain); + + list_for_each_entry_safe(q, n, &pending->list, list) { + int sig = q->info.si_signo; + + if (likely(q->info.si_code != 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 = 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); +} + struct sigqueue *sigqueue_alloc(void) { return __sigqueue_alloc(-1, current, GFP_KERNEL, 0, SIGQUEUE_PREALLOC); @@ -2029,6 +1991,7 @@ int send_sigqueue(struct sigqueue *q, st rcu_read_unlock(); return ret; } +#endif /* CONFIG_POSIX_TIMERS */ void do_notify_pidfd(struct task_struct *task) { --- a/kernel/time/itimer.c +++ b/kernel/time/itimer.c @@ -151,7 +151,27 @@ COMPAT_SYSCALL_DEFINE2(getitimer, int, w #endif /* - * The timer is automagically restarted, when interval != 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 = &tsk->signal->real_timer; + + if (!hrtimer_is_queued(tmr) && tsk->signal->it_real_incr != 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) { --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -251,7 +251,7 @@ static void common_hrtimer_rearm(struct /* * 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_sigi struct k_itimer *timr; unsigned long flags; + /* + * Release siglock to ensure proper locking order versus + * timr::it_lock. Keep interrupts disabled. + */ + spin_unlock(¤t->sighand->siglock); + timr = lock_timer(info->si_tid, &flags); if (!timr) - return; + goto out; if (timr->it_interval && timr->it_requeue_pending == info->si_sys_private) { timr->kclock->timer_rearm(timr); @@ -275,6 +281,11 @@ void posixtimer_rearm(struct kernel_sigi } unlock_timer(timr, flags); +out: + spin_lock(¤t->sighand->siglock); + + /* Don't expose the si_sys_private value to userspace */ + info->si_sys_private = 0; } int posix_timer_queue_signal(struct k_itimer *timr)