From mboxrd@z Thu Jan 1 00:00:00 1970 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 6C0DF3A9017 for ; Tue, 24 Feb 2026 16:37:51 +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=1771951071; cv=none; b=VXBMPT0OMt5GBIEmIUetufLmoz8hPL0fOpjmukMntOOh8RMwN/Dy+5vtiUuh2Aop6fYc6pGesgGkys2MgyVWRkmElaFi+w4AyfBuiYQbey9pSFm8GtenJuxlhNznnOcFVWCYv/eQOtFg7yNhrP47GlV2seZOvWQ7za05bGKMC50= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771951071; c=relaxed/simple; bh=msLMmZOey6NGztL4eHEzxtT9NsHXzhH83BCFBuKtKsA=; h=Date:Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=g/e7GMseFkDbbMKtzegIClq2ASSh3qvr9g+cOy/p8WfjQvi5PkFN3mNr+ZRgZnyIdgiZmysFLmfAjg2K6Oc69PNfcwSDj6H1SQpC3DL/5pQVJrRyehzP7n4OIjH0l7eTW1YVMkLiUHka7lZb9+RLteqYbtg2kdSnOSQxSVO/29I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=V1ITk2tG; 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="V1ITk2tG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A784BC116D0; Tue, 24 Feb 2026 16:37:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771951071; bh=msLMmZOey6NGztL4eHEzxtT9NsHXzhH83BCFBuKtKsA=; h=Date:From:To:Cc:Subject:References:From; b=V1ITk2tG3AwGOyHN9wOK2YVihn9nASxYgXkAmC2Nwor1P6GYhNh5MMq1laGBMOobR /M5QI6RftMjr46pUUD8jb386/fu97xHgmyvchEU6UbMLvi05VXHc1We2vmNWHjudZg ScKP0DGxFPdcMnDmSuOm8oXRySlXsuqabJcoP4y5EbmM5hajTzlAEMBTYEZBHjefD1 ipWjdNlernUIIWZUxboK5E9OWY2+Tr/JHyM6oOFiV4qXUx0o3O/uE/XCOIo2C12xKj 8E1ktV2FvPT1saX1ul8qlQHdjnYKTnxtF91lLV65aitbghVQd39tQ75Fo53ZOzQ+tA ajRWm5amPCKyQ== Date: Tue, 24 Feb 2026 17:37:48 +0100 Message-ID: <20260224163430.870639266@kernel.org> User-Agent: quilt/0.68 From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , John Stultz , Stephen Boyd , Daniel Lezcano , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , x86@kernel.org, Peter Zijlstra , Frederic Weisbecker , Eric Dumazet Subject: [patch 32/48] hrtimer: Re-arrange hrtimer_interrupt() References: <20260224163022.795809588@kernel.org> 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 From: Peter Zijlstra Rework hrtimer_interrupt() such that reprogramming is split out into an independent function at the end of the interrupt. This prepares for reprogramming getting delayed beyond the end of hrtimer_interrupt(). Notably, this changes the hang handling to always wait 100ms instead of trying to keep it proportional to the actual delay. This simplifies the state, also this really shouldn't be happening. Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Thomas Gleixner --- tglx: Added the tracepoint and used a proper naming convention --- kernel/time/hrtimer.c | 93 +++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 49 deletions(-) --- a/kernel/time/hrtimer.c +++ b/kernel/time/hrtimer.c @@ -690,6 +690,12 @@ static inline int hrtimer_hres_active(st cpu_base->hres_active : 0; } +static inline void hrtimer_rearm_event(ktime_t expires_next, bool deferred) +{ + trace_hrtimer_rearm(expires_next, deferred); + tick_program_event(expires_next, 1); +} + static void __hrtimer_reprogram(struct hrtimer_cpu_base *cpu_base, struct hrtimer *next_timer, ktime_t expires_next) { @@ -715,7 +721,7 @@ static void __hrtimer_reprogram(struct h if (!hrtimer_hres_active(cpu_base) || cpu_base->hang_detected) return; - tick_program_event(expires_next, 1); + hrtimer_rearm_event(expires_next, false); } /* @@ -1939,6 +1945,28 @@ static __latent_entropy void hrtimer_run #ifdef CONFIG_HIGH_RES_TIMERS /* + * Very similar to hrtimer_force_reprogram(), except it deals with + * in_hrtirq and hang_detected. + */ +static void hrtimer_rearm(struct hrtimer_cpu_base *cpu_base, ktime_t now) +{ + ktime_t expires_next = hrtimer_update_next_event(cpu_base); + + cpu_base->expires_next = expires_next; + cpu_base->in_hrtirq = false; + + if (unlikely(cpu_base->hang_detected)) { + /* + * Give the system a chance to do something else than looping + * on hrtimer interrupts. + */ + expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC); + cpu_base->hang_detected = false; + } + hrtimer_rearm_event(expires_next, false); +} + +/* * High resolution timer interrupt * Called with interrupts disabled */ @@ -1973,63 +2001,30 @@ void hrtimer_interrupt(struct clock_even __hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD); - /* Reevaluate the clock bases for the [soft] next expiry */ - expires_next = hrtimer_update_next_event(cpu_base); - /* - * Store the new expiry value so the migration code can verify - * against it. - */ - cpu_base->expires_next = expires_next; - cpu_base->in_hrtirq = false; - raw_spin_unlock_irqrestore(&cpu_base->lock, flags); - - /* Reprogramming necessary ? */ - if (!tick_program_event(expires_next, 0)) { - cpu_base->hang_detected = false; - return; - } - /* * The next timer was already expired due to: * - tracing * - long lasting callbacks * - being scheduled away when running in a VM * - * We need to prevent that we loop forever in the hrtimer - * interrupt routine. We give it 3 attempts to avoid - * overreacting on some spurious event. - * - * Acquire base lock for updating the offsets and retrieving - * the current time. + * We need to prevent that we loop forever in the hrtiner interrupt + * routine. We give it 3 attempts to avoid overreacting on some + * spurious event. */ - raw_spin_lock_irqsave(&cpu_base->lock, flags); now = hrtimer_update_base(cpu_base); - cpu_base->nr_retries++; - if (++retries < 3) - goto retry; - /* - * Give the system a chance to do something else than looping - * here. We stored the entry time, so we know exactly how long - * we spent here. We schedule the next event this amount of - * time away. - */ - cpu_base->nr_hangs++; - cpu_base->hang_detected = true; - raw_spin_unlock_irqrestore(&cpu_base->lock, flags); + expires_next = hrtimer_update_next_event(cpu_base); + if (expires_next < now) { + if (++retries < 3) + goto retry; - delta = ktime_sub(now, entry_time); - if ((unsigned int)delta > cpu_base->max_hang_time) - cpu_base->max_hang_time = (unsigned int) delta; - /* - * Limit it to a sensible value as we enforce a longer - * delay. Give the CPU at least 100ms to catch up. - */ - if (delta > 100 * NSEC_PER_MSEC) - expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC); - else - expires_next = ktime_add(now, delta); - tick_program_event(expires_next, 1); - pr_warn_once("hrtimer: interrupt took %llu ns\n", ktime_to_ns(delta)); + delta = ktime_sub(now, entry_time); + cpu_base->max_hang_time = max_t(unsigned int, cpu_base->max_hang_time, delta); + cpu_base->nr_hangs++; + cpu_base->hang_detected = true; + } + + hrtimer_rearm(cpu_base, now); + raw_spin_unlock_irqrestore(&cpu_base->lock, flags); } #endif /* !CONFIG_HIGH_RES_TIMERS */