From nobody Sat Feb 7 12:11:18 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 CFF22128399 for ; Wed, 24 Jan 2024 17:05:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706115942; cv=none; b=U6jAA5H9kzoaCbaUt6TGGiqxmz/BnknmbnQUEu4mHFG0+7BL+gcaIwq8ITZsIBVgYR6FEqW5J4aK6taJlpSUO/NGbsHBtrnzd0JNvf8RcRM+VU7Ixq01KM3GMeV4WZV7QlfCh1fwHhMyijd177AU0PzD7Cz+Cxe+nY8VbzupYW4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706115942; c=relaxed/simple; bh=eSXm80QnH1JhZdpRmNd1upMtxM0h6BzQ1w/ftFOWWPc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Q/uyLFzjyV0ub05OamzbTyijWRtzFJzdy0o7MCrieyxfbHA4lRTm6eraBormJ6BhSc4UQ3Hz7YBge4xum8I9UiWG5DYM8ZL/Kh0nEBW1uGpdnbFXajmKW50yklB+alue8DVVHGJgXzIjFa2TfNX6AOCGb0LlLRnj+18KcjZL4ro= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kojVIWxt; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kojVIWxt" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 75272C433F1; Wed, 24 Jan 2024 17:05:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706115942; bh=eSXm80QnH1JhZdpRmNd1upMtxM0h6BzQ1w/ftFOWWPc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kojVIWxtXJKhyTSyng1Lga8n+MLK0Pk421TnVKDDBf1CabhJTJB9wm+to4PFy2/DK OBBiPcozPTTB9A9z7GcjUmeOpJbZkxPFGKBNyZ8IRLKb/VuNwKEpve4V2QqyawwbVP vK6GrAf8ug64HuewPiNYHfQDkaa1SG6r0geZ6OL0ONVgzU0lYTF/64ETDSa3oTQp6u V3UFL+K/B7o29Rx5BQxRxfbar7iCQ4g0lVfaWWBuiMRsZ/CnuSbufT8WM1ITdDnTbq AXgUfk4z/MmAbD0j34ZaEkTEboJ9CDkdWqBLK7r/AyqE32Q5aeYU48+2OQeTjOWU/w 4BfZ60jSoqb6g== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , Thomas Gleixner , Ingo Molnar , Anna-Maria Behnsen , Peng Liu , Joel Fernandes Subject: [PATCH 14/15] tick: Shut down low-res tick from dying CPU Date: Wed, 24 Jan 2024 18:04:58 +0100 Message-ID: <20240124170459.24850-15-frederic@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240124170459.24850-1-frederic@kernel.org> References: <20240124170459.24850-1-frederic@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The timekeeping duty is handed over from the outgoing CPU within stop machine. This works well if CONFIG_NO_HZ_COMMON=3Dn or the tick is in high-res mode. However in low-res dynticks mode, the tick isn't cancelled until the clockevent is shut down, which can happen later. The tick may therefore fire again once IRQs are re-enabled on stop machine and until IRQs are disabled for good upon the last call to idle. That's so many opportunities for a timekeeper to go idle and the outgoing CPU to take over that duty. This is why tick_nohz_idle_stop_tick() is called one last time on idle if the CPU is seen offline: so that the timekeeping duty is handed over again in case the CPU has re-taken the duty. This means there are two timekeeping handovers on CPU down hotplug with different undocumented constraints and purposes: 1) A handover on stop machine for !dynticks || highres. All online CPUs are guaranteed to be non-idle and the timekeeping duty can be safely handed-over. The hrtimer tick is cancelled so it is guaranteed that in dynticks mode the outgoing CPU won't take again the duty. 2) A handover on last idle call for dynticks && lowres. Setting the duty to TICK_DO_TIMER_NONE makes sure that a CPU will take over the timekeeping. Prepare for consolidating the handover to a single place (the first one) with shutting down the low-res tick as well from tick_cancel_sched_timer() as well. This will simplify the handover and unify the tick cancellation between high-res and low-res. Signed-off-by: Frederic Weisbecker Reviewed-by: Thomas Gleixner --- kernel/time/tick-common.c | 3 ++- kernel/time/tick-sched.c | 32 +++++++++++++++++++++++++------- kernel/time/tick-sched.h | 4 ++-- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/kernel/time/tick-common.c b/kernel/time/tick-common.c index 522414089c0d..9cd09eea06d6 100644 --- a/kernel/time/tick-common.c +++ b/kernel/time/tick-common.c @@ -410,7 +410,8 @@ int tick_cpu_dying(unsigned int dying_cpu) if (tick_do_timer_cpu =3D=3D dying_cpu) tick_do_timer_cpu =3D cpumask_first(cpu_online_mask); =20 - tick_cancel_sched_timer(dying_cpu); + /* Make sure the CPU won't try to retake the timekeeping duty */ + tick_sched_timer_dying(dying_cpu); =20 /* Remove CPU from timer broadcasting */ tick_offline_cpu(dying_cpu); diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index 274ac5941b16..5e7fe19b9977 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -309,6 +309,14 @@ static enum hrtimer_restart tick_nohz_highres_handler(= struct hrtimer *timer) return HRTIMER_RESTART; } =20 +static void tick_sched_timer_cancel(struct tick_sched *ts) +{ + if (tick_sched_flag_test(ts, TS_FLAG_HIGHRES)) + hrtimer_cancel(&ts->sched_timer); + else if (tick_sched_flag_test(ts, TS_FLAG_NOHZ)) + tick_program_event(KTIME_MAX, 1); +} + #ifdef CONFIG_NO_HZ_FULL cpumask_var_t tick_nohz_full_mask; EXPORT_SYMBOL_GPL(tick_nohz_full_mask); @@ -998,10 +1006,7 @@ static void tick_nohz_stop_tick(struct tick_sched *ts= , int cpu) * the tick timer. */ if (unlikely(expires =3D=3D KTIME_MAX)) { - if (tick_sched_flag_test(ts, TS_FLAG_HIGHRES)) - hrtimer_cancel(&ts->sched_timer); - else - tick_program_event(KTIME_MAX, 1); + tick_sched_timer_cancel(ts); return; } =20 @@ -1563,13 +1568,26 @@ void tick_setup_sched_timer(bool hrtimer) tick_nohz_activate(ts); } =20 -void tick_cancel_sched_timer(int cpu) +/* + * Shut down the tick and make sure the CPU won't try to retake the timeke= eping + * duty before disabling IRQs in idle for the last time. + */ +void tick_sched_timer_dying(int cpu) { + struct tick_device *td =3D &per_cpu(tick_cpu_device, cpu); struct tick_sched *ts =3D &per_cpu(tick_cpu_sched, cpu); + struct clock_event_device *dev =3D td->evtdev; ktime_t idle_sleeptime, iowait_sleeptime; =20 - if (tick_sched_flag_test(ts, TS_FLAG_HIGHRES)) - hrtimer_cancel(&ts->sched_timer); + /* This must happen before hrtimers are migrated! */ + tick_sched_timer_cancel(ts); + + /* + * If the clockevents doesn't support CLOCK_EVT_STATE_ONESHOT_STOPPED, + * make sure not to call low-res tick handler. + */ + if (tick_sched_flag_test(ts, TS_FLAG_NOHZ)) + dev->event_handler =3D clockevents_handle_noop; =20 idle_sleeptime =3D ts->idle_sleeptime; iowait_sleeptime =3D ts->iowait_sleeptime; diff --git a/kernel/time/tick-sched.h b/kernel/time/tick-sched.h index bbe72a078985..58d8d1c49dd3 100644 --- a/kernel/time/tick-sched.h +++ b/kernel/time/tick-sched.h @@ -106,9 +106,9 @@ extern struct tick_sched *tick_get_tick_sched(int cpu); =20 extern void tick_setup_sched_timer(bool hrtimer); #if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS -extern void tick_cancel_sched_timer(int cpu); +extern void tick_sched_timer_dying(int cpu); #else -static inline void tick_cancel_sched_timer(int cpu) { } +static inline void tick_sched_timer_dying(int cpu) { } #endif =20 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST --=20 2.43.0