[PATCH v3 1/5] tick/nohz: Re-evaluate the tick on return to user space

From: Marco Crivellari

Date: Thu Nov 12 2026 - 11:49:40 EST


On nohz_full CPUs the tick is re-evaluated on IRQ exit and on idle exit.

This is not enough on return to user space. If the tick is left running in
the kernel, for example because a tick dependency was cleared
(tick_nohz_dep_clear*() don't kick the CPU) or a timer that kept it
running was cancelled, it fires at least once in userspace before
tick_nohz_irq_exit() stops it.

Stop it from context tracking right before resuming userspace. When the
tick is already stopped, the common case, this costs a few flag tests on
each return to userspace.

Suggested-by: Frederic Weisbecker <frederic@xxxxxxxxxx>
Signed-off-by: Marco Crivellari <marco.crivellari@xxxxxxxx>
---
include/linux/tick.h | 8 ++++++++
kernel/context_tracking.c | 12 ++++++++++++
kernel/time/tick-sched.c | 22 ++++++++++++++++++++++
3 files changed, 42 insertions(+)

diff --git a/include/linux/tick.h b/include/linux/tick.h
index b121c5d53e30..8bfbb053f4fd 100644
--- a/include/linux/tick.h
+++ b/include/linux/tick.h
@@ -269,6 +269,7 @@ static inline void tick_dep_clear_signal(struct signal_struct *signal,
extern void tick_nohz_full_kick_cpu(int cpu);
extern void __tick_nohz_task_switch(void);
extern void __init tick_nohz_full_setup(cpumask_var_t cpumask);
+extern void __tick_nohz_user_enter(void);
#else
static inline bool tick_nohz_full_enabled(void) { return false; }
static inline bool tick_nohz_full_cpu(int cpu) { return false; }
@@ -294,6 +295,7 @@ static inline void tick_dep_clear_signal(struct signal_struct *signal,
static inline void tick_nohz_full_kick_cpu(int cpu) { }
static inline void __tick_nohz_task_switch(void) { }
static inline void tick_nohz_full_setup(cpumask_var_t cpumask) { }
+static inline void __tick_nohz_user_enter(void) { }
#endif

static inline void tick_nohz_task_switch(void)
@@ -302,6 +304,12 @@ static inline void tick_nohz_task_switch(void)
__tick_nohz_task_switch();
}

+static inline void tick_nohz_user_enter(void)
+{
+ if (tick_nohz_full_enabled())
+ __tick_nohz_user_enter();
+}
+
static inline void tick_nohz_user_enter_prepare(void)
{
if (tick_nohz_full_cpu(smp_processor_id()))
diff --git a/kernel/context_tracking.c b/kernel/context_tracking.c
index a743e7ffa6c0..8e898b34977d 100644
--- a/kernel/context_tracking.c
+++ b/kernel/context_tracking.c
@@ -24,6 +24,7 @@
#include <linux/export.h>
#include <linux/kprobes.h>
#include <trace/events/rcu.h>
+#include <linux/tick.h>


DEFINE_PER_CPU(struct context_tracking, context_tracking) = {
@@ -470,6 +471,17 @@ void noinstr __ct_user_enter(enum ctx_state state)
vtime_user_enter(current);
instrumentation_end();
}
+
+ /*
+ * On nohz_full CPUs the tick may have been left running in the
+ * kernel, e.g. kept by a tick dependency that is now gone.
+ * Stop it now rather than let it interrupt userspace once
+ * more before the next IRQ exit notices.
+ */
+ instrumentation_begin();
+ tick_nohz_user_enter();
+ instrumentation_end();
+
/*
* Other than generic entry implementation, we may be past the last
* rescheduling opportunity in the entry code. Trigger a self IPI
diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index c6d86e4da8a9..edaac8e7c669 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -1026,6 +1026,28 @@ static void tick_nohz_full_stop_tick(struct tick_sched *ts, int cpu)
else
tick_nohz_retain_tick(ts);
}
+
+void __tick_nohz_user_enter(void)
+{
+ struct tick_sched *ts;
+ int cpu;
+
+ lockdep_assert_irqs_disabled();
+
+ cpu = smp_processor_id();
+ if (!tick_nohz_full_cpu(cpu))
+ return;
+
+ ts = this_cpu_ptr(&tick_cpu_sched);
+ if (!tick_sched_flag_test(ts, TS_FLAG_NOHZ))
+ return;
+
+ if (tick_sched_flag_test(ts, TS_FLAG_STOPPED))
+ return;
+
+ if (can_stop_full_tick(cpu, ts))
+ tick_nohz_full_stop_tick(ts, cpu);
+}
#endif /* CONFIG_NO_HZ_FULL */

static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
--
2.56.0