Linux Documentation
 help / color / mirror / Atom feed
From: Andrea Righi <arighi@nvidia.com>
To: Gabriele Monaco <gmonaco@redhat.com>
Cc: linux-kernel@vger.kernel.org,
	Steven Rostedt <rostedt@goodmis.org>,
	Jonathan Corbet <corbet@lwn.net>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	linux-trace-kernel@vger.kernel.org, linux-doc@vger.kernel.org,
	K Prateek Nayak <kprateek.nayak@amd.com>,
	Nam Cao <namcao@linutronix.de>
Subject: Re: [PATCH] rv: Adapt handle racing preemptions in nrp
Date: Fri, 4 Dec 2026 11:28:56 +0100	[thread overview]
Message-ID: <axKV6PhOz7eUZ4z9@gpd4> (raw)
In-Reply-To: <20261204080908.129943-1-gmonaco@redhat.com>

Hi Gabriele,

On Fri, Dec 04, 2026 at 09:09:07AM +0100, Gabriele Monaco wrote:
> As Prateek pointed out and Andrea validated, the schedule_entry event
> may be seen before the set_need_resched event that caused the
> preemption. This rare race can occur because the schedule_entry is
> emitted before taking the rq lock.
> The nrp model expects a strict order and catches this as a violation.
> 
> Add a branch to handle this inverted order. Essentially allowing a
> preemption provided the set_need_resched is visible before being done
> scheduling (switch out or sched_exit if there was no switch).
> 
> This weakens the model because it allows a preemption without
> need_resched where the flag is simply set later in an event unrelated to
> the preemption.
> 
> Guard this by transforming nrp in a hybrid automaton and also mandate
> the TIF is set when preemption occurs.
> This applies in all preemption cases besides a nested preemption,
> where the flag is cleared by a racing scheduler call.
> 
> Cc: K Prateek Nayak <kprateek.nayak@amd.com>
> Cc: Andrea Righi <arighi@nvidia.com>
> Cc: Nam Cao <namcao@linutronix.de>
> Signed-off-by: Gabriele Monaco <gmonaco@redhat.com>

In addition to the issues reported by sashiko, maybe we should also add:

Fixes: 53bc5c556b82 ("sched: Set TIF_NEED_RESCHED before calling __trace_set_need_resched()")

That commit moved the tracepoint after setting TIF_NEED_RESCHED, making
this inversion possible. Before it, the callback completed before the
flag became visible.

Thanks,
-Andrea

> ---
>  Documentation/trace/rv/monitor_sched.rst | 45 +++++++++++++++++--
>  kernel/trace/rv/monitors/nrp/Kconfig     |  2 +-
>  kernel/trace/rv/monitors/nrp/nrp.c       | 57 ++++++++++++++++++++++--
>  kernel/trace/rv/monitors/nrp/nrp.h       | 48 +++++++++++++++++++-
>  kernel/trace/rv/monitors/nrp/nrp_trace.h |  4 ++
>  kernel/trace/rv/rv_trace.h               |  2 +-
>  tools/verification/models/sched/nrp.dot  | 21 ++++++---
>  7 files changed, 163 insertions(+), 16 deletions(-)
> 
> diff --git a/Documentation/trace/rv/monitor_sched.rst b/Documentation/trace/rv/monitor_sched.rst
> index d3ba7edc202f..c2cc8f25ac18 100644
> --- a/Documentation/trace/rv/monitor_sched.rst
> +++ b/Documentation/trace/rv/monitor_sched.rst
> @@ -225,8 +225,8 @@ occurred, as the situation above is hard to pinpoint::
>    |                           #===========================================#
>    | schedule_entry              |                       ^
>    | schedule_entry_preempt      | sched_need_resched    | schedule_entry
> -  |                             |                      schedule_entry_preempt
> -  |                             v                       |
> +  |                             |                      schedule_entry_preempt;
> +  |                             v                       |  need_resched == 1
>    |                           +----------------------+  |
>    |                      +--- |                      |  |
>    |   sched_need_resched |    |     rescheduling     | -+
> @@ -242,7 +242,8 @@ occurred, as the situation above is hard to pinpoint::
>    |                           |                      | <--+
>    |                           +----------------------+    |
>    |                             | schedule_entry          | sched_need_resched
> -  |                             | schedule_entry_preempt  |
> +  |                             | schedule_entry_preempt; |
> +  |                             |    need_resched == 1    |
>    |                             v                         |
>    |                           +-----------------------+   |
>    +-------------------------- |    nested_preempt     | --+
> @@ -250,6 +251,44 @@ occurred, as the situation above is hard to pinpoint::
>                                  ^ irq_entry         |
>                                  +-------------------+
>  
> +The ``schedule_entry`` events are emitted before taking the rq lock, this means
> +that they can race with the ``need_resched`` event. In practice this mean that,
> +although the ``need_resched`` event is what caused the reschedule to happen,
> +the monitor might see it *after* the ``schedule_entry_preempt`` event.
> +For this purpose, the model allows another branch, for simplicity represented
> +separately here, that allows a racing preemption provided it is followed by a
> +``need_resched`` before being done scheduling::
> +
> +    schedule_entry   #=========================#
> +    schedule_exit    H                         H
> +    irq_entry        H                         H
> +  +----------------- H   any_thread_running    H
> +  |                  H                         H
> +  +----------------> H                         H <+
> +                     #=========================#  |
> +                       |                          |
> +                       | schedule_entry_preempt;  |
> +                       |    need_resched == 1     |
> +                       v                          |
> +    irq_entry        +-------------------------+  |
> +  +----------------- |                         |  |
> +  |                  |     racing_preempt      |  | schedule_exit_vain
> +  +----------------> |                         |  | sched_switch_preempt
> +                     +-------------------------+  |
> +                       |                          |
> +                       | sched_need_resched       |
> +   sched_need_resched  v                          |
> +    irq_entry        +-------------------------+  |
> +  +----------------- |                         |  |
> +  |                  |     racing_resched      |  |
> +  +----------------> |                         | -+
> +                     +-------------------------+
> +
> +Excluding coming from ``nested_preempt``, all ``schedule_entry_preempt`` also
> +contain a guard for ``need_resched == 1``, which is especially necessary to
> +avoid false negatives in the backup branch where scheduling must be visible
> +before ``need_resched``.
> +
>  Due to how the ``need_resched`` flag on the preemption count works on arm64,
>  this monitor is unstable on that architecture, as it often records preemption
>  when the flag is not set, even in presence of the workaround above.
> diff --git a/kernel/trace/rv/monitors/nrp/Kconfig b/kernel/trace/rv/monitors/nrp/Kconfig
> index f5ec08f65535..61a5846aa0f5 100644
> --- a/kernel/trace/rv/monitors/nrp/Kconfig
> +++ b/kernel/trace/rv/monitors/nrp/Kconfig
> @@ -4,7 +4,7 @@ config RV_MON_NRP
>  	depends on RV
>  	depends on RV_MON_SCHED
>  	default y if !ARM64
> -	select DA_MON_EVENTS_ID
> +	select HA_MON_EVENTS_ID
>  	bool "nrp monitor"
>  	help
>  	  Monitor to ensure preemption requires need resched.
> diff --git a/kernel/trace/rv/monitors/nrp/nrp.c b/kernel/trace/rv/monitors/nrp/nrp.c
> index 4b5646a70094..be6f4a036079 100644
> --- a/kernel/trace/rv/monitors/nrp/nrp.c
> +++ b/kernel/trace/rv/monitors/nrp/nrp.c
> @@ -16,7 +16,39 @@
>  
>  #define RV_MON_TYPE RV_MON_PER_TASK
>  #include "nrp.h"
> -#include <rv/da_monitor.h>
> +#include <rv/ha_monitor.h>
> +
> +static u64 ha_get_env(struct ha_monitor *ha_mon, enum envs_nrp env, u64 time_ns)
> +{
> +	if (env == need_resched_nrp)
> +		return test_tsk_need_resched(ha_get_target(ha_mon));
> +	return ENV_INVALID_VALUE;
> +}
> +
> +static inline bool ha_verify_guards(struct ha_monitor *ha_mon,
> +				    enum states curr_state, enum events event,
> +				    enum states next_state, u64 time_ns)
> +{
> +	bool res = true;
> +
> +	if (curr_state == any_thread_running_nrp && event == schedule_entry_preempt_nrp)
> +		res = ha_get_env(ha_mon, need_resched_nrp, time_ns) == 1ull;
> +	else if (curr_state == preempt_irq_nrp && event == schedule_entry_preempt_nrp)
> +		res = ha_get_env(ha_mon, need_resched_nrp, time_ns) == 1ull;
> +	else if (curr_state == rescheduling_nrp && event == schedule_entry_preempt_nrp)
> +		res = ha_get_env(ha_mon, need_resched_nrp, time_ns) == 1ull;
> +	return res;
> +}
> +
> +static bool ha_verify_constraint(struct ha_monitor *ha_mon,
> +				 enum states curr_state, enum events event,
> +				 enum states next_state, u64 time_ns)
> +{
> +	if (!ha_verify_guards(ha_mon, curr_state, event, next_state, time_ns))
> +		return false;
> +
> +	return true;
> +}
>  
>  #ifdef CONFIG_X86_LOCAL_APIC
>  #include <asm/trace/irq_vectors.h>
> @@ -73,6 +105,15 @@ static void handle_sched_need_resched(void *data, struct task_struct *tsk,
>  		da_handle_start_event(tsk, sched_need_resched_nrp);
>  }
>  
> +static void handle_sched_switch(void *data, bool preempt,
> +				struct task_struct *prev,
> +				struct task_struct *next,
> +				unsigned int prev_state)
> +{
> +	if (preempt)
> +		da_handle_event(prev, sched_switch_preempt_nrp);
> +}
> +
>  static void handle_schedule_entry(void *data, bool preempt)
>  {
>  	if (preempt)
> @@ -81,17 +122,25 @@ static void handle_schedule_entry(void *data, bool preempt)
>  		da_handle_event(current, schedule_entry_nrp);
>  }
>  
> +static void handle_schedule_exit(void *data, bool is_switch)
> +{
> +	if (!is_switch)
> +		da_handle_event(current, schedule_exit_vain_nrp);
> +}
> +
>  static int enable_nrp(void)
>  {
>  	int retval;
>  
> -	retval = da_monitor_init();
> +	retval = ha_monitor_init();
>  	if (retval)
>  		return retval;
>  
>  	rv_attach_trace_probe("nrp", irq_handler_entry, handle_irq_entry);
>  	rv_attach_trace_probe("nrp", sched_set_need_resched_tp, handle_sched_need_resched);
> +	rv_attach_trace_probe("nrp", sched_switch, handle_sched_switch);
>  	rv_attach_trace_probe("nrp", sched_entry_tp, handle_schedule_entry);
> +	rv_attach_trace_probe("nrp", sched_exit_tp, handle_schedule_exit);
>  	attach_vector_irq();
>  
>  	return 0;
> @@ -103,10 +152,12 @@ static void disable_nrp(void)
>  
>  	rv_detach_trace_probe("nrp", irq_handler_entry, handle_irq_entry);
>  	rv_detach_trace_probe("nrp", sched_set_need_resched_tp, handle_sched_need_resched);
> +	rv_detach_trace_probe("nrp", sched_switch, handle_sched_switch);
>  	rv_detach_trace_probe("nrp", sched_entry_tp, handle_schedule_entry);
> +	rv_detach_trace_probe("nrp", sched_exit_tp, handle_schedule_exit);
>  	detach_vector_irq();
>  
> -	da_monitor_destroy();
> +	ha_monitor_destroy();
>  }
>  
>  static struct rv_monitor rv_this = {
> diff --git a/kernel/trace/rv/monitors/nrp/nrp.h b/kernel/trace/rv/monitors/nrp/nrp.h
> index 3270d4c0139f..e2e71a940d32 100644
> --- a/kernel/trace/rv/monitors/nrp/nrp.h
> +++ b/kernel/trace/rv/monitors/nrp/nrp.h
> @@ -11,6 +11,8 @@ enum states_nrp {
>  	preempt_irq_nrp,
>  	any_thread_running_nrp,
>  	nested_preempt_nrp,
> +	racing_preempt_nrp,
> +	racing_resched_nrp,
>  	rescheduling_nrp,
>  	state_max_nrp,
>  };
> @@ -20,14 +22,25 @@ enum states_nrp {
>  enum events_nrp {
>  	irq_entry_nrp,
>  	sched_need_resched_nrp,
> +	sched_switch_preempt_nrp,
>  	schedule_entry_nrp,
>  	schedule_entry_preempt_nrp,
> +	schedule_exit_vain_nrp,
>  	event_max_nrp,
>  };
>  
> +enum envs_nrp {
> +	need_resched_nrp,
> +	env_max_nrp,
> +	env_max_stored_nrp = need_resched_nrp,
> +};
> +
> +_Static_assert(env_max_stored_nrp <= MAX_HA_ENV_LEN, "Not enough slots");
> +
>  struct automaton_nrp {
>  	char *state_names[state_max_nrp];
>  	char *event_names[event_max_nrp];
> +	char *env_names[env_max_nrp];
>  	unsigned char function[state_max_nrp][event_max_nrp];
>  	unsigned char initial_state;
>  	bool final_states[state_max_nrp];
> @@ -38,40 +51,71 @@ static const struct automaton_nrp automaton_nrp = {
>  		"preempt_irq",
>  		"any_thread_running",
>  		"nested_preempt",
> +		"racing_preempt",
> +		"racing_resched",
>  		"rescheduling",
>  	},
>  	.event_names = {
>  		"irq_entry",
>  		"sched_need_resched",
> +		"sched_switch_preempt",
>  		"schedule_entry",
>  		"schedule_entry_preempt",
> +		"schedule_exit_vain",
> +	},
> +	.env_names = {
> +		"need_resched",
>  	},
>  	.function = {
>  		{
> +			preempt_irq_nrp,
>  			preempt_irq_nrp,
>  			preempt_irq_nrp,
>  			nested_preempt_nrp,
>  			nested_preempt_nrp,
> +			preempt_irq_nrp,
>  		},
>  		{
>  			any_thread_running_nrp,
>  			rescheduling_nrp,
>  			any_thread_running_nrp,
> -			INVALID_STATE,
> +			any_thread_running_nrp,
> +			racing_preempt_nrp,
> +			any_thread_running_nrp,
>  		},
>  		{
>  			nested_preempt_nrp,
>  			preempt_irq_nrp,
> +			nested_preempt_nrp,
>  			any_thread_running_nrp,
>  			any_thread_running_nrp,
> +			nested_preempt_nrp,
> +		},
> +		{
> +			racing_preempt_nrp,
> +			racing_resched_nrp,
> +			INVALID_STATE,
> +			INVALID_STATE,
> +			INVALID_STATE,
> +			INVALID_STATE,
> +		},
> +		{
> +			racing_resched_nrp,
> +			racing_resched_nrp,
> +			any_thread_running_nrp,
> +			INVALID_STATE,
> +			INVALID_STATE,
> +			any_thread_running_nrp,
>  		},
>  		{
>  			preempt_irq_nrp,
>  			rescheduling_nrp,
> +			rescheduling_nrp,
>  			any_thread_running_nrp,
>  			any_thread_running_nrp,
> +			rescheduling_nrp,
>  		},
>  	},
>  	.initial_state = preempt_irq_nrp,
> -	.final_states = { 0, 1, 0, 0 },
> +	.final_states = { 0, 1, 0, 0, 0, 0 },
>  };
> diff --git a/kernel/trace/rv/monitors/nrp/nrp_trace.h b/kernel/trace/rv/monitors/nrp/nrp_trace.h
> index 2e13497de3b6..70682a53261a 100644
> --- a/kernel/trace/rv/monitors/nrp/nrp_trace.h
> +++ b/kernel/trace/rv/monitors/nrp/nrp_trace.h
> @@ -12,4 +12,8 @@ DEFINE_EVENT(event_da_monitor_id, event_nrp,
>  DEFINE_EVENT(error_da_monitor_id, error_nrp,
>  	     TP_PROTO(int id, char *state, char *event),
>  	     TP_ARGS(id, state, event));
> +
> +DEFINE_EVENT(error_env_da_monitor_id, error_env_nrp,
> +	     TP_PROTO(int id, char *state, char *event, char *env),
> +	     TP_ARGS(id, state, event, env));
>  #endif /* CONFIG_RV_MON_NRP */
> diff --git a/kernel/trace/rv/rv_trace.h b/kernel/trace/rv/rv_trace.h
> index 2f8a932432c9..373e939d8b3c 100644
> --- a/kernel/trace/rv/rv_trace.h
> +++ b/kernel/trace/rv/rv_trace.h
> @@ -154,7 +154,6 @@ DECLARE_EVENT_CLASS(error_da_monitor_id,
>  
>  #include <monitors/wwnr/wwnr_trace.h>
>  #include <monitors/snroc/snroc_trace.h>
> -#include <monitors/nrp/nrp_trace.h>
>  #include <monitors/sssw/sssw_trace.h>
>  // Add new monitors based on CONFIG_DA_MON_EVENTS_ID here
>  
> @@ -189,6 +188,7 @@ DECLARE_EVENT_CLASS(error_env_da_monitor_id,
>  
>  #include <monitors/stall/stall_trace.h>
>  #include <monitors/nomiss/nomiss_trace.h>
> +#include <monitors/nrp/nrp_trace.h>
>  // Add new monitors based on CONFIG_HA_MON_EVENTS_ID here
>  
>  #endif
> diff --git a/tools/verification/models/sched/nrp.dot b/tools/verification/models/sched/nrp.dot
> index 77bb64669416..cbab8fec6c57 100644
> --- a/tools/verification/models/sched/nrp.dot
> +++ b/tools/verification/models/sched/nrp.dot
> @@ -6,22 +6,31 @@ digraph state_automaton {
>  	{node [shape = circle] "nested_preempt"};
>  	{node [shape = plaintext, style=invis, label=""] "__init_preempt_irq"};
>  	{node [shape = circle] "preempt_irq"};
> +	{node [shape = circle] "racing_preempt"};
> +	{node [shape = circle] "racing_resched"};
>  	{node [shape = circle] "rescheduling"};
>  	"__init_preempt_irq" -> "preempt_irq";
>  	"any_thread_running" [label = "any_thread_running", color = green3];
> -	"any_thread_running" -> "any_thread_running" [ label = "schedule_entry\nirq_entry" ];
> +	"any_thread_running" -> "any_thread_running" [ label = "schedule_entry\nschedule_exit_vain\nsched_switch_preempt\nirq_entry" ];
> +	"any_thread_running" -> "racing_preempt" [ label = "schedule_entry_preempt;need_resched == 1" ];
>  	"any_thread_running" -> "rescheduling" [ label = "sched_need_resched" ];
>  	"nested_preempt" [label = "nested_preempt"];
>  	"nested_preempt" -> "any_thread_running" [ label = "schedule_entry_preempt\nschedule_entry" ];
> -	"nested_preempt" -> "nested_preempt" [ label = "irq_entry" ];
> +	"nested_preempt" -> "nested_preempt" [ label = "irq_entry\nsched_switch_preempt\nschedule_exit_vain" ];
>  	"nested_preempt" -> "preempt_irq" [ label = "sched_need_resched" ];
>  	"preempt_irq" [label = "preempt_irq"];
> -	"preempt_irq" -> "nested_preempt" [ label = "schedule_entry_preempt\nschedule_entry" ];
> -	"preempt_irq" -> "preempt_irq" [ label = "irq_entry\nsched_need_resched" ];
> +	"preempt_irq" -> "nested_preempt" [ label = "schedule_entry_preempt;need_resched == 1\nschedule_entry" ];
> +	"preempt_irq" -> "preempt_irq" [ label = "irq_entry\nsched_switch_preempt\nschedule_exit_vain\nsched_need_resched" ];
> +	"racing_preempt" [label = "racing_preempt"];
> +	"racing_preempt" -> "racing_preempt" [ label = "irq_entry" ];
> +	"racing_preempt" -> "racing_resched" [ label = "sched_need_resched" ];
> +	"racing_resched" [label = "racing_resched"];
> +	"racing_resched" -> "any_thread_running" [ label = "sched_switch_preempt\nschedule_exit_vain" ];
> +	"racing_resched" -> "racing_resched" [ label = "irq_entry\nsched_need_resched" ];
>  	"rescheduling" [label = "rescheduling"];
> -	"rescheduling" -> "any_thread_running" [ label = "schedule_entry_preempt\nschedule_entry" ];
> +	"rescheduling" -> "any_thread_running" [ label = "schedule_entry_preempt;need_resched == 1\nschedule_entry" ];
>  	"rescheduling" -> "preempt_irq" [ label = "irq_entry" ];
> -	"rescheduling" -> "rescheduling" [ label = "sched_need_resched" ];
> +	"rescheduling" -> "rescheduling" [ label = "sched_need_resched\nsched_switch_preempt\nschedule_exit_vain" ];
>  	{ rank = min ;
>  		"__init_preempt_irq";
>  		"preempt_irq";
> 
> base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
> -- 
> 2.56.1
> 

      reply	other threads:[~2026-12-04 10:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  8:09 [PATCH] rv: Adapt handle racing preemptions in nrp Gabriele Monaco
2026-12-04 10:28 ` Andrea Righi [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=axKV6PhOz7eUZ4z9@gpd4 \
    --to=arighi@nvidia.com \
    --cc=corbet@lwn.net \
    --cc=gmonaco@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=namcao@linutronix.de \
    --cc=rostedt@goodmis.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