Linux Trace Kernel
 help / color / mirror / Atom feed
From: Hongyan Xia <hongyan.xia@transsion.com>
To: Karl Mehltretter <kmehltretter@gmail.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Song Liu <song@kernel.org>, Juri Lelli <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
	Valentin Schneider <vschneid@redhat.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>,
	"linux-arm-kernel@lists.infradead.org"
	<linux-arm-kernel@lists.infradead.org>,
	"linux-trace-kernel@vger.kernel.org"
	<linux-trace-kernel@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v2 3/3] sched/preempt: Check preempt counts in notrace helpers
Date: Thu, 26 Nov 2026 02:55:58 +0000	[thread overview]
Message-ID: <8765db46-0240-43f9-aa29-15417beee708@transsion.com> (raw)
In-Reply-To: <87f882f6c8a66a3d293b6ec00612fd5f25cb2edd.1795631277.git.kmehltretter@gmail.com>

On 11/26/2026 10:30 AM, Karl Mehltretter wrote:
> The notrace preemption macros use raw count updates even with
> CONFIG_DEBUG_PREEMPT, bypassing the underflow and near-overflow checks
> used by ordinary operations.
> 
> Add checked noinstr helpers for notrace preemption-count updates 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 and retain raw updates when CONFIG_DEBUG_PREEMPT is
> disabled. Remove the now-unused __preempt_count_inc() and
> __preempt_count_dec() macros.
> 
> Share validation with preempt_count_add() and preempt_count_sub(),
> leaving their preemption-latency tracing and ordering unchanged.
> 
> DEBUG_LOCKS_WARN_ON() evaluates its condition before entering its
> instrumentation region. Branch profiling would therefore call
> ftrace_likely_update() from noinstr text. Make the Branch Profiling
> Kconfig choice depend on !DEBUG_PREEMPT, covering both
> PROFILE_ANNOTATED_BRANCHES and PROFILE_ALL_BRANCHES without changing
> non-debug configurations.
> 
> Suggested-by: Peter Zijlstra <peterz@infradead.org>
> Link: https://lore.kernel.org/r/20261029093219.GH1779021@noisy.programming.kicks-ass.net
> Signed-off-by: Karl Mehltretter <kmehltretter@gmail.com>
> ---
> Changes in v2:
> - Replace the kernel/sched/Makefile CFLAGS_core.o override with a
>    !DEBUG_PREEMPT dependency on the Branch Profiling Kconfig choice
>    (Steven Rostedt), covering both PROFILE_ANNOTATED_BRANCHES and
>    PROFILE_ALL_BRANCHES.
> - Remove unused __preempt_count_inc() and __preempt_count_dec() macros
>    and clarify the changelog wording (Hongyan Xia).
>   include/linux/preempt.h | 23 +++++++++++++++++------
>   kernel/sched/core.c     | 40 ++++++++++++++++++++++++++++++++++++----
>   kernel/trace/Kconfig    |  1 +
>   3 files changed, 54 insertions(+), 10 deletions(-)
> 
> diff --git a/include/linux/preempt.h b/include/linux/preempt.h
> index 06ff44a4b8b..b60fce55a35 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);
> @@ -228,8 +239,8 @@ extern void preempt_count_sub(int val);
>   #define preempt_count_dec_and_test() __preempt_count_dec_and_test()
>   #endif
>   
> -#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 +274,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 +294,7 @@ do { \
>   #define preempt_enable_notrace() \
>   do { \
>   	barrier(); \
> -	__preempt_count_dec(); \
> +	preempt_count_dec_notrace(); \
>   } while (0)
>   
>   #define preempt_check_resched() do { } while (0)
> @@ -291,14 +302,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/core.c b/kernel/sched/core.c
> index 515b9d0353c..017549d1f72 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);
>   }
> diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig
> index 35d043b1933..a940dbe551b 100644
> --- a/kernel/trace/Kconfig
> +++ b/kernel/trace/Kconfig
> @@ -663,6 +663,7 @@ config TRACE_BRANCH_PROFILING
>   
>   choice
>   	prompt "Branch Profiling"
> +	depends on !DEBUG_PREEMPT
>   	default BRANCH_PROFILE_NONE
>   	help
>   	 The branch profiling is a software profiler. It will add hooks

Still not a fan of the Kconfig handling, but I'm not an active user of 
branch profiling, so opinion from others should take precedence.

The rest LGTM.

Reviewed-by: Hongyan Xia <hongyan.xia@transsion.com>

      reply	other threads:[~2026-11-26  2:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  2:30 [PATCH v2 0/3] sched/preempt: Check notrace preempt-count updates Karl Mehltretter
2026-11-26  2:30 ` [PATCH v2 1/3] arm64: Always inline raw preempt count updates Karl Mehltretter
2026-11-26  2:48   ` sashiko-bot
2026-11-26  3:52   ` Li kunyu
2026-11-26  2:30 ` [PATCH v2 2/3] arm64: Always inline preempt count and reschedule checks Karl Mehltretter
2026-11-26  2:51   ` Hongyan Xia
2026-11-26  2:30 ` [PATCH v2 3/3] sched/preempt: Check preempt counts in notrace helpers Karl Mehltretter
2026-11-26  2:55   ` Hongyan Xia [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8765db46-0240-43f9-aa29-15417beee708@transsion.com \
    --to=hongyan.xia@transsion.com \
    --cc=bsegall@google.com \
    --cc=catalin.marinas@arm.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=juri.lelli@redhat.com \
    --cc=kmehltretter@gmail.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mgorman@suse.de \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=song@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox