diff mbox series

[1/3] srcu: Warn when NMI-unsafe API is used in NMI

Message ID 20221013172244.1099010-2-frederic@kernel.org (mailing list archive)
State Accepted
Commit 6558b914fc4ee221891c9f32753626ebd4acb86c
Headers show
Series srcu: A few NMI-safe debugging updates | expand

Commit Message

Frederic Weisbecker Oct. 13, 2022, 5:22 p.m. UTC
Using the NMI-unsafe reader API from within NMIs is very likely to be
buggy for three reasons:

1) NMIs aren't strictly re-entrant (a pending nested NMI will execute
   at the end of the current one) so it should be fine to use a
   non-atomic increment here. However breakpoints can still interrupt
   NMIs and if a breakpoint callback has a reader on that same ssp, a
   racy increment can happen.

2) If the only reader site for a given ssp is in an NMI, RCU is definetly
   a better choice over SRCU.

3) Because of the previous reason (2), an ssp having an SRCU read side
   critical section in an NMI is likely to have another one from a task
   context.

For all these reasons, warn if an nmi unsafe reader API is used from an
NMI.

Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
---
 kernel/rcu/srcutree.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Joel Fernandes Oct. 14, 2022, 10:45 p.m. UTC | #1
On Thu, Oct 13, 2022 at 07:22:42PM +0200, Frederic Weisbecker wrote:
> Using the NMI-unsafe reader API from within NMIs is very likely to be
> buggy for three reasons:
> 
> 1) NMIs aren't strictly re-entrant (a pending nested NMI will execute
>    at the end of the current one) so it should be fine to use a
>    non-atomic increment here. However breakpoints can still interrupt
>    NMIs and if a breakpoint callback has a reader on that same ssp, a
>    racy increment can happen.
> 
> 2) If the only reader site for a given ssp is in an NMI, RCU is definetly
								  definitely
>    a better choice over SRCU.

Just checking - because NMI are by definition not-preemptibe, so SRCU over
RCU doesn't make much sense right?

Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>

thanks,

 - Joel

> 
> 3) Because of the previous reason (2), an ssp having an SRCU read side
>    critical section in an NMI is likely to have another one from a task
>    context.
> 
> For all these reasons, warn if an nmi unsafe reader API is used from an
> NMI.
> 
> Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
> ---
>  kernel/rcu/srcutree.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/kernel/rcu/srcutree.c b/kernel/rcu/srcutree.c
> index c54142374793..8b7ef1031d89 100644
> --- a/kernel/rcu/srcutree.c
> +++ b/kernel/rcu/srcutree.c
> @@ -642,6 +642,8 @@ static void srcu_check_nmi_safety(struct srcu_struct *ssp, bool nmi_safe)
>  
>  	if (!IS_ENABLED(CONFIG_PROVE_RCU))
>  		return;
> +	/* NMI-unsafe use in NMI is a bad sign */
> +	WARN_ON_ONCE(!nmi_safe && in_nmi());
>  	sdp = raw_cpu_ptr(ssp->sda);
>  	old_nmi_safe_mask = READ_ONCE(sdp->srcu_nmi_safety);
>  	if (!old_nmi_safe_mask) {
> -- 
> 2.25.1
>
Paul E. McKenney Oct. 20, 2022, 10:16 p.m. UTC | #2
On Fri, Oct 14, 2022 at 10:45:04PM +0000, Joel Fernandes wrote:
> On Thu, Oct 13, 2022 at 07:22:42PM +0200, Frederic Weisbecker wrote:
> > Using the NMI-unsafe reader API from within NMIs is very likely to be
> > buggy for three reasons:
> > 
> > 1) NMIs aren't strictly re-entrant (a pending nested NMI will execute
> >    at the end of the current one) so it should be fine to use a
> >    non-atomic increment here. However breakpoints can still interrupt
> >    NMIs and if a breakpoint callback has a reader on that same ssp, a
> >    racy increment can happen.
> > 
> > 2) If the only reader site for a given ssp is in an NMI, RCU is definetly
> 								  definitely
> >    a better choice over SRCU.
> 
> Just checking - because NMI are by definition not-preemptibe, so SRCU over
> RCU doesn't make much sense right?

Agreed.  But you never know...

> Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>

I will apply on the next rebase (after today's rebase), thank you!

							Thanx, Paul

> thanks,
> 
>  - Joel
> 
> > 
> > 3) Because of the previous reason (2), an ssp having an SRCU read side
> >    critical section in an NMI is likely to have another one from a task
> >    context.
> > 
> > For all these reasons, warn if an nmi unsafe reader API is used from an
> > NMI.
> > 
> > Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
> > ---
> >  kernel/rcu/srcutree.c | 2 ++
> >  1 file changed, 2 insertions(+)
> > 
> > diff --git a/kernel/rcu/srcutree.c b/kernel/rcu/srcutree.c
> > index c54142374793..8b7ef1031d89 100644
> > --- a/kernel/rcu/srcutree.c
> > +++ b/kernel/rcu/srcutree.c
> > @@ -642,6 +642,8 @@ static void srcu_check_nmi_safety(struct srcu_struct *ssp, bool nmi_safe)
> >  
> >  	if (!IS_ENABLED(CONFIG_PROVE_RCU))
> >  		return;
> > +	/* NMI-unsafe use in NMI is a bad sign */
> > +	WARN_ON_ONCE(!nmi_safe && in_nmi());
> >  	sdp = raw_cpu_ptr(ssp->sda);
> >  	old_nmi_safe_mask = READ_ONCE(sdp->srcu_nmi_safety);
> >  	if (!old_nmi_safe_mask) {
> > -- 
> > 2.25.1
> >
diff mbox series

Patch

diff --git a/kernel/rcu/srcutree.c b/kernel/rcu/srcutree.c
index c54142374793..8b7ef1031d89 100644
--- a/kernel/rcu/srcutree.c
+++ b/kernel/rcu/srcutree.c
@@ -642,6 +642,8 @@  static void srcu_check_nmi_safety(struct srcu_struct *ssp, bool nmi_safe)
 
 	if (!IS_ENABLED(CONFIG_PROVE_RCU))
 		return;
+	/* NMI-unsafe use in NMI is a bad sign */
+	WARN_ON_ONCE(!nmi_safe && in_nmi());
 	sdp = raw_cpu_ptr(ssp->sda);
 	old_nmi_safe_mask = READ_ONCE(sdp->srcu_nmi_safety);
 	if (!old_nmi_safe_mask) {