Linux Trace Kernel
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Gabriele Monaco" <gmonaco@redhat.com>,
	linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH] rv: Adapt handle racing preemptions in nrp
Date: Fri, 04 Dec 2026 08:31:21 +0000	[thread overview]
Message-ID: <sashiko-outbox-249397@kernel.org> (raw)
In-Reply-To: <20261204080908.129943-1-gmonaco@redhat.com>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [Medium] rv: racing_resched self-loop in nrp drops new reschedule on
  vain schedule
- [Medium] rv: nested preemption during racing preemption fails nrp
  guard check
- [Low] rv: incorrect any_thread_running self-loop labels in nrp
  documentation
- [Low] rv: need_resched guard on preempt_irq causes false positive at
  startup
- [Low] rv: nrp attaches unused sched_process_exit probe and extra
  synchronize_rcu()
- [Low] rv: missing Fixes trailer for nrp monitor introducing commit
--

commit a94703095d16688d6cf8c217dc6265f8e208b268
Author: Gabriele Monaco <gmonaco@redhat.com>

> rv: Adapt handle racing preemptions in nrp
>
> 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>

[Severity: Low]
This fixes false-positive violations in the nrp monitor introduced with
the monitor itself.  Should this include:

  Fixes: e8440a88e56b ("rv: Add nrp and sssw per-task monitors")

> diff --git a/Documentation/trace/rv/monitor_sched.rst b/Documentation/trace/rv/monitor_sched.rst
> index d3ba7edc202f6..c2cc8f25ac18c 100644
> --- a/Documentation/trace/rv/monitor_sched.rst
> +++ b/Documentation/trace/rv/monitor_sched.rst

[ ... ]

> +    schedule_entry   #=========================#
> +    schedule_exit    H                         H
> +    irq_entry        H                         H
> +  +----------------- H   any_thread_running    H

[Severity: Low]
Should this self-loop label use schedule_exit_vain instead of
schedule_exit and also list sched_switch_preempt?  Both nrp.dot and
automaton_nrp in nrp.h use schedule_exit_vain (there is no schedule_exit
event) and include sched_switch_preempt on that self-loop.

> diff --git a/kernel/trace/rv/monitors/nrp/nrp.c b/kernel/trace/rv/monitors/nrp/nrp.c
> index 4b5646a700946..be6f4a036079c 100644
> --- a/kernel/trace/rv/monitors/nrp/nrp.c
> +++ b/kernel/trace/rv/monitors/nrp/nrp.c

[ ... ]

> +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;

[Severity: Medium]
Can this guard trigger a false-positive error_env_nrp violation when an
IRQ nests inside preempt_schedule_common() during a racing preemption?

Because irq_entry self-loops in any_thread_running and racing_resched
always returns to any_thread_running on completion, the monitor loses
track of the nested preemption:

CPU0 (task T)                      CPU1
[state: any_thread_running]
                                   __resched_curr(rq0, TIF_NEED_RESCHED)
                                     set_nr_and_not_polling()
preempt_schedule_common()
  <hardirq>  (before preempt_disable_notrace())
    irq_entry -> any_thread_running
    preempt_schedule_irq()
      __schedule(SM_PREEMPT)
        schedule_entry_preempt -> racing_preempt
                                     trace_sched_set_need_resched_tp()
                                       sched_need_resched -> racing_resched
        clear_tsk_need_resched(T)
        schedule_exit_vain -> any_thread_running
  __schedule(SM_PREEMPT)
    schedule_entry_preempt  <-- need_resched == 0, error_env_nrp

When the outer __schedule(SM_PREEMPT) runs after the IRQ returns,
TIF_NEED_RESCHED is already cleared, so the guard fails and triggers a
spurious monitor reaction and reset.

> +	else if (curr_state == preempt_irq_nrp && event == schedule_entry_preempt_nrp)
> +		res = ha_get_env(ha_mon, need_resched_nrp, time_ns) == 1ull;

[Severity: Low]
Can this guard on the initial state preempt_irq cause a false-positive
error_env_nrp right after enabling or resetting the monitor?

handle_sched_need_resched() starts an untracked task in preempt_irq.  If
that happens while a nested preempt_schedule_irq() is already running on
another CPU, the inner schedule clears TIF_NEED_RESCHED before the outer
__schedule(SM_PREEMPT) runs:

CPU0 (task T)                      CPU1
[monitoring == 0]
                                   __resched_curr(rq0, TIF_NEED_RESCHED)
                                     set_nr_and_not_polling()
preempt_schedule_common()
  <hardirq>
    preempt_schedule_irq()
      __schedule(SM_PREEMPT)
        schedule_entry_preempt (ignored)
                                     trace_sched_set_need_resched_tp()
                                       -> starts in preempt_irq
        clear_tsk_need_resched(T)
        schedule_exit_vain -> preempt_irq
  __schedule(SM_PREEMPT)
    schedule_entry_preempt  <-- need_resched == 0, error_env_nrp

Because preempt_irq -> nested_preempt was previously unguarded, this
startup race did not trigger a spurious violation before.

[ ... ]

>  static int enable_nrp(void)
>  {
>  	int retval;
>  
> -	retval = da_monitor_init();
> +	retval = ha_monitor_init();

[Severity: Low]
Does switching to ha_monitor_init() without HA_SKIP_AUTO_CLEANUP attach an
unused sched_process_exit probe and add an extra synchronize_rcu()?

nrp leaves HA_TIMER_TYPE at HA_TIMER_NONE, so ha_cancel_timer_sync() is a
no-op, yet ha_monitor_enable_hook() still hooks sched_process_exit to run
memory barriers and reset task_struct->rv on every exiting task.

In addition, da_monitor_sync_hook() runs a redundant synchronize_rcu() in
disable_nrp() right after tracepoint_synchronize_unregister().  Defining
HA_SKIP_AUTO_CLEANUP here, or gating those hooks in ha_monitor.h on
HA_TIMER_TYPE != HA_TIMER_NONE, would avoid both costs.

> diff --git a/kernel/trace/rv/monitors/nrp/nrp.h b/kernel/trace/rv/monitors/nrp/nrp.h
> index 3270d4c0139fd..e2e71a940d326 100644
> --- a/kernel/trace/rv/monitors/nrp/nrp.h
> +++ b/kernel/trace/rv/monitors/nrp/nrp.h

[ ... ]

> +		{
> +			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,
> +		},

[Severity: Medium]
Does self-looping racing_resched_nrp on sched_need_resched_nrp drop a new
reschedule arriving before schedule_exit_vain_nrp and cause a spurious
violation on the next preemption loop iteration?

In __schedule(), when prev == next, clear_tsk_need_resched() and
raw_spin_rq_unlock_irq() run before trace_sched_exit_tp(false):

CPU0 (task T)                      CPU1
[state: racing_resched]
__schedule(SM_PREEMPT) [next == prev]
  clear_tsk_need_resched(T)
  raw_spin_rq_unlock_irq(rq)
                                   resched_curr(rq)
                                     set TIF_NEED_RESCHED
                                     sched_need_resched -> racing_resched
  trace_sched_exit_tp(false)
    schedule_exit_vain -> any_thread_running
preempt_schedule_common() loops (need_resched() is true)
  __schedule(SM_PREEMPT)
    schedule_entry_preempt -> racing_preempt
    sched_switch_preempt  <-- INVALID_STATE, error_nrp

Because TIF_NEED_RESCHED is already set, no further sched_need_resched
event is emitted for the second __schedule(SM_PREEMPT), leaving the
monitor in racing_preempt where completion hits INVALID_STATE.

Transitioning racing_resched_nrp -> rescheduling_nrp on
sched_need_resched_nrp (and updating nrp.dot to match) would preserve the
pending reschedule across schedule_exit_vain_nrp.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204080908.129943-1-gmonaco@redhat.com?part=1

      reply	other threads:[~2026-12-04  8:31 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  8:31 ` sashiko-bot [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=sashiko-outbox-249397@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=gmonaco@redhat.com \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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