Re: [PATCH 3/3] sched/preempt: Check preempt counts in notrace helpers
From: Hongyan Xia
Date: Wed Nov 25 2026 - 08:26:40 EST
On 11/16/2026 3:54 PM, Karl Mehltretter wrote:
> The notrace preemption macros use raw count updates even with
> CONFIG_DEBUG_PREEMPT enabled. They therefore skip the bounds checks
> which ordinary preemption operations provide.
Nit: Somehow my brain cannot parse 'bounds checks' before reading the
whole patch. Hopefully it's just me.
>
> Add checked noinstr preempt-count helpers and use them in
> preempt_disable_notrace(), preempt_enable_notrace() and
> preempt_enable_no_resched_notrace(). Export the out-of-line helpers
> for module callers. Keep raw updates when CONFIG_DEBUG_PREEMPT is
> disabled.
>
> Share the validation with preempt_count_add() and preempt_count_sub(),
> while leaving their preemption-latency tracing and ordering unchanged.
> DEBUG_LOCKS_WARN_ON() brackets its diagnostic work with
> instrumentation_begin() and instrumentation_end(), but evaluates its
> condition before that region. Branch profiling would therefore call
> ftrace_likely_update() from noinstr text.
>
> Compile core.o with branch profiling disabled when DEBUG_PREEMPT and
> TRACE_BRANCH_PROFILING are both enabled, as the scheduler Makefile already
> does for build_policy.o and build_utility.o. This keeps valid notrace count
> updates free of profiler calls while retaining the existing warning path.
> The tradeoff is loss of branch-profile data for the rest of core.o in that
> configuration; scheduler tracepoints and function tracing are unchanged.
>
> Suggested-by: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> Link: https://lore.kernel.org/r/20261029093219.GH1779021@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Signed-off-by: Karl Mehltretter <kmehltretter@xxxxxxxxx>
> ---
> include/linux/preempt.h | 22 ++++++++++++++++++----
> kernel/sched/Makefile | 3 +++
> kernel/sched/core.c | 40 ++++++++++++++++++++++++++++++++++++----
> 3 files changed, 57 insertions(+), 8 deletions(-)
>
> diff --git a/include/linux/preempt.h b/include/linux/preempt.h
> index 06ff44a4b8b6a..42c585fd91d45 100644
> --- a/include/linux/preempt.h
> +++ b/include/linux/preempt.h
> @@ -217,6 +217,17 @@ static __always_inline unsigned char interrupt_context_level(void)
> */
> #define in_atomic_preempt_off() (preempt_count() != PREEMPT_DISABLE_OFFSET)
>
> +#ifdef CONFIG_DEBUG_PREEMPT
> +void preempt_count_add_notrace(int val);
> +void preempt_count_sub_notrace(int val);
> +#define preempt_count_dec_and_test_notrace() \
> + ({ preempt_count_sub_notrace(1); should_resched(0); })
> +#else
> +#define preempt_count_add_notrace(val) __preempt_count_add(val)
> +#define preempt_count_sub_notrace(val) __preempt_count_sub(val)
> +#define preempt_count_dec_and_test_notrace() __preempt_count_dec_and_test()
> +#endif
> +
> #if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_TRACE_PREEMPT_TOGGLE)
> extern void preempt_count_add(int val);
> extern void preempt_count_sub(int val);
> @@ -231,6 +242,9 @@ extern void preempt_count_sub(int val);
> #define __preempt_count_inc() __preempt_count_add(1)
> #define __preempt_count_dec() __preempt_count_sub(1)
>
> +#define preempt_count_inc_notrace() preempt_count_add_notrace(1)
> +#define preempt_count_dec_notrace() preempt_count_sub_notrace(1)
> +
> #define preempt_count_inc() preempt_count_add(1)
> #define preempt_count_dec() preempt_count_sub(1)
>
> @@ -263,7 +277,7 @@ do { \
> #define preempt_enable_notrace() \
> do { \
> barrier(); \
> - if (unlikely(__preempt_count_dec_and_test())) \
> + if (unlikely(preempt_count_dec_and_test_notrace())) \
> __preempt_schedule_notrace(); \
> } while (0)
>
> @@ -283,7 +297,7 @@ do { \
> #define preempt_enable_notrace() \
> do { \
> barrier(); \
> - __preempt_count_dec(); \
> + preempt_count_dec_notrace(); \
> } while (0)
>
> #define preempt_check_resched() do { } while (0)
No need for this hunk as you are in !CONFIG_PREEMPTION without any
checks anyway, but for symmetry I guess it doesn't hurt.
But if you decide to keep this change, then we can always use your new
macros and remove __preempt_count_{inc/dec}(). They no longer have any
users anyway?
> @@ -291,14 +305,14 @@ do { \
>
> #define preempt_disable_notrace() \
> do { \
> - __preempt_count_inc(); \
> + preempt_count_inc_notrace(); \
> barrier(); \
> } while (0)
>
> #define preempt_enable_no_resched_notrace() \
> do { \
> barrier(); \
> - __preempt_count_dec(); \
> + preempt_count_dec_notrace(); \
> } while (0)
>
> #else /* !CONFIG_PREEMPT_COUNT */
> diff --git a/kernel/sched/Makefile b/kernel/sched/Makefile
> index b1f1a367034ff..d87f1bd3c28c3 100644
> --- a/kernel/sched/Makefile
> +++ b/kernel/sched/Makefile
> @@ -27,6 +27,9 @@ endif
>
> # Branch profiling isn't noinstr-safe
> ifdef CONFIG_TRACE_BRANCH_PROFILING
> +ifdef CONFIG_DEBUG_PREEMPT
> +CFLAGS_core.o += -DDISABLE_BRANCH_PROFILING
> +endif
> CFLAGS_build_policy.o += -DDISABLE_BRANCH_PROFILING
> CFLAGS_build_utility.o += -DDISABLE_BRANCH_PROFILING
> endif
This exclusion looks massive for a whole core.c file. When someone
profiles branches, does he or she really care about branch statistics in
DEBUG_LOCKS_WARN_ON()? Can we just switch to unlikely_notrace() in that
macro globally?
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index 1a088d4576f59..98ca93bc60887 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -6040,7 +6040,7 @@ static inline void preempt_latency_start(int val)
> }
> }
>
> -void preempt_count_add(int val)
> +static __always_inline bool __preempt_count_add_checked(int val)
> {
> #ifdef CONFIG_DEBUG_PREEMPT
> /*
> @@ -6052,7 +6052,7 @@ void preempt_count_add(int val)
> */
> if (!IS_ENABLED(CONFIG_HAS_SEPARATE_PREEMPT_RESCHED_BITS) &&
> DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
> - return;
> + return false;
> #endif
> __preempt_count_add(val);
> #ifdef CONFIG_DEBUG_PREEMPT
> @@ -6062,6 +6062,21 @@ void preempt_count_add(int val)
> DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
> PREEMPT_MASK - 10);
> #endif
> + return true;
> +}
> +
> +#ifdef CONFIG_DEBUG_PREEMPT
> +void noinstr preempt_count_add_notrace(int val)
> +{
> + __preempt_count_add_checked(val);
> +}
> +EXPORT_SYMBOL(preempt_count_add_notrace);
> +#endif
> +
> +void preempt_count_add(int val)
> +{
> + if (!__preempt_count_add_checked(val))
> + return;
> preempt_latency_start(val);
> }
> EXPORT_SYMBOL(preempt_count_add);
> @@ -6077,7 +6092,7 @@ static inline void preempt_latency_stop(int val)
> trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
> }
>
> -void preempt_count_sub(int val)
> +static __always_inline bool preempt_count_sub_debug_checks(int val)
> {
> #ifdef CONFIG_DEBUG_PREEMPT
> /*
> @@ -6087,15 +6102,32 @@ void preempt_count_sub(int val)
> unsigned int pc = preempt_count();
>
> if (DEBUG_LOCKS_WARN_ON(pc - uval > pc))
> - return;
> + return false;
> /*
> * Is the spinlock portion underflowing?
> */
> if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
> !(preempt_count() & PREEMPT_MASK)))
> + return false;
> +#endif
> + return true;
> +}
> +
> +#ifdef CONFIG_DEBUG_PREEMPT
> +void noinstr preempt_count_sub_notrace(int val)
> +{
> + if (!preempt_count_sub_debug_checks(val))
> return;
> + __preempt_count_sub(val);
> +}
> +EXPORT_SYMBOL(preempt_count_sub_notrace);
> #endif
>
> +void preempt_count_sub(int val)
> +{
> + if (!preempt_count_sub_debug_checks(val))
> + return;
> +
> preempt_latency_stop(val);
> __preempt_count_sub(val);
> }
The rest LGTM.