From nobody Fri Nov 29 00:58:19 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 8A395194138 for ; Fri, 27 Sep 2024 08:48:52 +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=1727426934; cv=none; b=a62o2fDrNYxFNOWBqlydztZRzns8/VbfbK5gB19wQAH6IVWumsywzXLn+e66odD17V0aPsOiWU3/NIpur7H0K0tH2iRy76fsJhQAEwuw3raJLUAYDkWsEkWf18ZYRTStgW57MlVIhgTkdmCp+cFW45q0jpBDW2IWBWA2/+9LFSI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727426934; c=relaxed/simple; bh=kcnbNFf0PqRgHjX+kLb8eKf2yoh61nkQKKITQrvCz+E=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=E+5VaLQZmE6bdqQWUkZ84QNQALNNNbQiMp/A5einkF964rTWsRcx8+XensNv4USx499gTvuW5TeCOT2dKstrr0FkCpTkVh2qXjo4sPyLowScAA9ZqWd+p1GNmrVbqh0wZVRpybV8dfPa+Y2u4f+gSXgHLRIEcdwawzy2LtM0Ads= 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=GxXjKeez; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=/tDPDNKL; 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="GxXjKeez"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="/tDPDNKL" Message-ID: <20240927084817.449464642@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1727426930; 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=S11ye5tLkZYHnPip+zXMdwlR8ANrj5PEcLf0WRbbImk=; b=GxXjKeezjSe0ico0FyVFThgbGB0cnTKGLO7xhu05KVXVqe8058B1m7dhE0CAm3Zp60Q81+ sBHuBGRXTYdGqVk2iBqM5ED2bJp2fcdbi4XXZYUph9R6d3wqJQ8X0bhfm/HlZ1L5/dbxW5 WsFShq4k/vYgj+Z+Ho/Ztv85IEeNu1ZJlfe+Og8WXSXNdsaaKmbWjc+Sr8iEDX4ZG2zFML t7zYbsZXq23LVkyiZLZpyREaHh2KGpUv1CWw0YQRQ8gfNthnRi4lZUYHIMJ6ZFhlQ1II9/ cxzelcwORfCbJ0oZNH8j2Uvkmaonwu7Tqz8GkKJcrXKcTzrbPWkjsWf2lWLWYA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1727426930; 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=S11ye5tLkZYHnPip+zXMdwlR8ANrj5PEcLf0WRbbImk=; b=/tDPDNKLJ122BdU3KJ8pjVgq8+73Z/G1zm4LwPzNDm9LLJzUnx6e6USgRPjXV4Zh9Mmi5F 06NzzYp11eQuVlAQ== 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 v4 09/27] posix-timers: Make signal delivery consistent References: <20240927083900.989915582@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Fri, 27 Sep 2024 10:48:50 +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. Change that function so it controls the actual signal delivery 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. Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) --- include/linux/posix-timers.h | 2 -- kernel/signal.c | 2 +- kernel/time/posix-cpu-timers.c | 2 +- kernel/time/posix-timers.c | 25 +++++++++++++++---------- 4 files changed, 17 insertions(+), 14 deletions(-) --- diff --git a/include/linux/posix-timers.h b/include/linux/posix-timers.h index 02afbb4da7f7..8c6d97412526 100644 --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -137,8 +137,6 @@ static inline void clear_posix_cputimers_work(struct ta= sk_struct *p) { } 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 diff --git a/kernel/signal.c b/kernel/signal.c index c35b6ff52767..a407724f1267 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -617,7 +617,7 @@ int dequeue_signal(sigset_t *mask, kernel_siginfo_t *in= fo, enum pid_type *type) } =20 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { - if (unlikely(info->si_code =3D=3D SI_TIMER && info->si_sys_private)) { + if (unlikely(info->si_code =3D=3D SI_TIMER)) { if (!posixtimer_deliver_signal(info)) goto again; } diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c index 12f828d704b1..bc2cd32b7a40 100644 --- 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 k_itimer *time= r, struct itimerspec64 *i * - 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); diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 6f0dacec25e0..1231efb7c30f 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -269,7 +269,10 @@ bool posixtimer_deliver_signal(struct kernel_siginfo *= info) 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 kernel_siginfo *i= nfo) } 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 kernel_siginfo = *info) 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 *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_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_itimer *timer, str= uct itimerspec64 *new_set 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 *timr, int flags, 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 timer_id, int tmr_f= lags, 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 *timer) { 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, timer_id) 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);