Received: by 2002:ab2:1347:0:b0:1f4:ac9d:b246 with SMTP id g7csp67881lqg; Wed, 10 Apr 2024 15:57:43 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXP1kVtj72/iWHpRCMxODttb6JB5hTb4dxx29vCs/D1G3EeqRSL+JHZEiT5BzqBBLDk4aqCGsACKL3SklL4gTrvgWBYSPMMc4aW3Kc1uw== X-Google-Smtp-Source: AGHT+IHUnTFi609oUfaMkcH3IWlCOaaehAtlSkbmcDU1syZUl044PVYI55UNrUK2jlWBpe5bk1vZ X-Received: by 2002:a05:6a20:1014:b0:1a7:878f:e9a3 with SMTP id gs20-20020a056a20101400b001a7878fe9a3mr3685043pzc.22.1712789863089; Wed, 10 Apr 2024 15:57:43 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712789863; cv=pass; d=google.com; s=arc-20160816; b=ttarn/zrzDp02qt2i+w15Uvh4it7GOCX5dhiKRUqrm9rM4TaUY154hbQ+XKOM933v6 7WfKzQKjR/CqsheXNl70uromfnKpLL2p3jPJNqdF8Lnefy4+E7+rhfSYf77dxQJBXpTw oXiehOtI0aF7hkthBlhg64p1wT3j3MxqpIjzNb0LM+xWIygNk1mZ68cBREZRpzyrpUTM apAMh5Hke3oOmbpJlKpiZbWcu5RZ2VUOrQzVgoWzAi4JFneBoNQdX7HWGoJgymPpgo/3 KZ6No1lwUnhksLhoctDogwKZNhWG/KSP8V/pJyq3+jX3zP/HEkAA7G7gvbNxQGqVIIoD dQ8w== 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=pDW2UUZDnP/mksmKSp58tEw1w9QqpWE3E22mpO9GuFY=; fh=joBs/8L+orz77ylyCy6wqNwwi35f7sYhLR+/TQjQjhI=; b=XDst8XwFagtSkVjj9rWUMja2vKjzJR1pFr91Ttg9/ArNF0XoAUgr7Xe7uolRqT1XyL qgNCwewLJfm91a6RpzQDw4aJ70oLsfLY5B7srEz3TUC2HjNDQ53h696SM/dfmYttUx3x 24iMCwfP5F1dmbf7IrtPj15SjA7HYj9cOFsJqJw/5MgrwxlosbkVS9Rz8Wn/4Z87oXWL HqUnV8Ioi/J0/LVamghppfWhaQumyo1iJGxaOthtFdJf4BoLKfYdRqNkZlq9mI5MTUJu QoqytZrX/PvUrQasSm8hXgr73zyQQsh2ayw3ykq6+rWCZ9U6ep+OHJF0k8JMQcYBCUFK Chdg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=e6M7Aftp; 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-139484-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-139484-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id q36-20020a631f64000000b005dc3e746573si94817pgm.826.2024.04.10.15.57.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 15:57:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-139484-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=e6M7Aftp; 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-139484-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-139484-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 15AD3B23E8D for ; Wed, 10 Apr 2024 22:52:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8D3127F476; Wed, 10 Apr 2024 22:47:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="e6M7Aftp"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="jw13IChK" 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 C5E913F9D8 for ; Wed, 10 Apr 2024 22:47: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=1712789230; cv=none; b=EgTld/xCqfSEdJeYH7LjdZxNYkW5qDj8e9uH9hl/MhA0yYX5Ry6qHfS+2DZCPPU4nBF1m3B9TbvdI32haig2pHXV+e6lHV+4J4dN4z84zNMvq+u8/Z4WzMOPGQ5LhurxUOmli59HkbbiPfJzxWcAQaa7fbAg4OILYgn6beDHanM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789230; c=relaxed/simple; bh=FlJJagnR9HUnZRLrJ3Canm8+IlguEM9dtcq4WQlXOec=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=pCUyZHqym/U6ACTX2zlM3ggTWKv7UMTU2UPGw4XBWl8ENwRSkFL9Qswr6BWvjRLpxmSpL/B0qg2e8GQcF3lxtHSwag2S/vUlCRLf6sMZWAZxOTxxxX7FMb8Qx/+/bw1Kc8hSJQtIy1tmYwPbCDRFnEtH5ezdEKAgjwpJZsJ2Rwc= 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=e6M7Aftp; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=jw13IChK; 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: <20240410165552.895516106@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789227; 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=pDW2UUZDnP/mksmKSp58tEw1w9QqpWE3E22mpO9GuFY=; b=e6M7AftpQ0urAko2VwlWDIAiusTSFI6tvLhkXGkOoLUkQrNj4EL1CGF3lUs5sOPKbARhZQ bNtad1Z7zS10vLJRsU7kkDvqPnh3QnhUy5WpZWIjwk6fHMJWai2v82s1IlJJL4jRGf24Fg TsjFlIuJy7infhHu+QNc2N6GIbktKBK13JgUYDccROxNSHOKpKaVXScqT19xk5dWM8FdBR s8lWyLHy1MwiNhRB2iM5xmu9sJSOzFJGERtQHQLlCIZ14FSAXfNkljIvN7rZ9TZ+WLWVe0 Jh3YUbyQrdoCNDGt81byOxSpbYyQnG1YHkSHl7/47vAyAgIMQNfiUNG1PtsJQA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789227; 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=pDW2UUZDnP/mksmKSp58tEw1w9QqpWE3E22mpO9GuFY=; b=jw13IChKRaZMqJY/aLk4WKPRXMFxstNkqAdNLG4bn7sC+M4CwkKHTj6bNiaKdUukyRL/B3 pGUtxRk6XpR2+SAQ== 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 V2 31/50] posix-timers: Add proper state tracking References: <20240410164558.316665885@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: Thu, 11 Apr 2024 00:47:06 +0200 (CEST) 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 --- 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(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -147,7 +147,7 @@ static inline void posix_cputimers_init_ * @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; --- a/kernel/time/alarmtimer.c +++ b/kernel/time/alarmtimer.c @@ -585,7 +585,7 @@ static enum alarmtimer_restart alarm_han */ ptr->it_overrun += __alarm_forward_now(alarm, ptr->it_interval, true); ++ptr->it_signal_seq; - ptr->it_active = 1; + ptr->it_status = POSIX_TIMER_ARMED; result = ALARMTIMER_RESTART; } spin_unlock_irqrestore(&ptr->it_lock, flags); --- 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 struct cpu_timer *ctmr = &timer->it.cpu; struct posix_cputimer_base *base; - timer->it_active = 0; if (!cpu_timer_dequeue(ctmr)) return; @@ -494,11 +493,12 @@ static int posix_cpu_timer_del(struct k_ */ WARN_ON_ONCE(ctmr->head || timerqueue_node_queued(&ctmr->node)); } else { - if (timer->it.cpu.firing) + if (timer->it.cpu.firing) { ret = TIMER_RETRY; - else + } else { disarm_timer(timer, p); - + timer->it_status = POSIX_TIMER_DISARMED; + } unlock_task_sighand(p, &flags); } @@ -560,7 +560,7 @@ static void arm_timer(struct k_itimer *t struct cpu_timer *ctmr = &timer->it.cpu; u64 newexp = cpu_timer_getexpires(ctmr); - timer->it_active = 1; + timer->it_status = POSIX_TIMER_ARMED; if (!cpu_timer_enqueue(&base->tqhead, ctmr)) return; @@ -586,7 +586,8 @@ static void cpu_timer_fire(struct k_itim { struct cpu_timer *ctmr = &timer->it.cpu; - timer->it_active = 0; + timer->it_status = POSIX_TIMER_DISARMED; + if (unlikely(timer->sigq == NULL)) { /* * This a special case for clock_nanosleep, @@ -671,7 +672,7 @@ static int posix_cpu_timer_set(struct k_ ret = TIMER_RETRY; } else { cpu_timer_dequeue(ctmr); - timer->it_active = 0; + timer->it_status = POSIX_TIMER_DISARMED; } /* --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -272,7 +272,7 @@ bool posixtimer_deliver_signal(struct ke if (timr->it_interval && timr->it_signal_seq == info->si_sys_private) { timr->kclock->timer_rearm(timr); - timr->it_active = 1; + timr->it_status = POSIX_TIMER_ARMED; timr->it_overrun_last = timr->it_overrun; timr->it_overrun = -1LL; ++timr->it_signal_seq; @@ -292,14 +292,17 @@ bool posixtimer_deliver_signal(struct ke int posix_timer_queue_signal(struct k_itimer *timr) { + enum posix_timer_state state = POSIX_TIMER_DISARMED; int ret, si_private = 0; enum pid_type type; lockdep_assert_held(&timr->it_lock); - timr->it_active = 0; - if (timr->it_interval) + if (timr->it_interval) { + state = POSIX_TIMER_REQUEUE_PENDING; si_private = ++timr->it_signal_seq; + } + timr->it_status = state; type = !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDTYPE_PID; ret = send_sigqueue(timr->sigq, timr->it_pid, type, si_private); @@ -367,7 +370,7 @@ static enum hrtimer_restart posix_timer_ timr->it_overrun += hrtimer_forward(timer, now, timr->it_interval); ret = HRTIMER_RESTART; ++timr->it_signal_seq; - timr->it_active = 1; + timr->it_status = POSIX_TIMER_ARMED; } } @@ -647,10 +650,10 @@ void common_timer_get(struct k_itimer *t /* interval timer ? */ if (iv) { cur_setting->it_interval = ktime_to_timespec64(iv); - } else if (!timr->it_active) { + } else if (timr->it_status == 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 *ti if (kc->timer_try_to_cancel(timr) < 0) return TIMER_RETRY; - timr->it_active = 0; + timr->it_status = POSIX_TIMER_DISARMED; posix_timer_set_common(timr, new_setting); /* Keep timer disarmed when it_value is zero */ @@ -908,7 +911,8 @@ int common_timer_set(struct k_itimer *ti sigev_none = timr->it_sigev_notify == SIGEV_NONE; kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none); - timr->it_active = !sigev_none; + if (!sigev_none) + timr->it_status = POSIX_TIMER_ARMED; return 0; } @@ -1007,7 +1011,7 @@ int common_timer_del(struct k_itimer *ti timer->it_interval = 0; if (kc->timer_try_to_cancel(timer) < 0) return TIMER_RETRY; - timer->it_active = 0; + timer->it_status = POSIX_TIMER_DISARMED; return 0; } --- 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 +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);