lkml.org 
[lkml]   [2020]   [Oct]   [30]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH 4/6] perf: Optimize get_recursion_context()
On Fri, 30 Oct 2020 18:11:38 +0100
Jesper Dangaard Brouer <brouer@redhat.com> wrote:

> On Fri, 30 Oct 2020 16:13:49 +0100
> Peter Zijlstra <peterz@infradead.org> wrote:
>
> > "Look ma, no branches!"
> >
> > Cc: Jesper Dangaard Brouer <brouer@redhat.com>
> > Cc: Steven Rostedt <rostedt@goodmis.org>
> > Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> > ---
>
> Cool trick! :-)
>
> Acked-by: Jesper Dangaard Brouer <brouer@redhat.com>
>
> > kernel/events/internal.h | 17 ++++++++---------
> > 1 file changed, 8 insertions(+), 9 deletions(-)
> >
> > --- a/kernel/events/internal.h
> > +++ b/kernel/events/internal.h
> > @@ -205,16 +205,15 @@ DEFINE_OUTPUT_COPY(__output_copy_user, a
> >
> > static inline int get_recursion_context(int *recursion)
> > {
> > - int rctx;
> > + unsigned int pc = preempt_count();
> > + unsigned int rctx = 0;
> >
> > - if (unlikely(in_nmi()))
> > - rctx = 3;
> > - else if (in_irq())
> > - rctx = 2;
> > - else if (in_serving_softirq())
> > - rctx = 1;
> > - else
> > - rctx = 0;
> > + if (pc & (NMI_MASK))
> > + rctx++;
> > + if (pc & (NMI_MASK | HARDIRQ_MASK))
> > + rctx++;
> > + if (pc & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET))
> > + rctx++;
> >
> > if (recursion[rctx])
> > return -1;
> >
> >

As this is something that ftrace recursion also does, perhaps we should
move this into interrupt.h so that anyone that needs a counter can get
it quickly, and not keep re-implementing it.

/*
* Quickly find what context you are in.
* 0 - normal
* 1 - softirq
* 2 - hard interrupt
* 3 - NMI
*/
static inline int irq_context()
{
unsigned int pc = preempt_count();
int rctx = 0;

if (pc & (NMI_MASK))
rctx++;
if (pc & (NMI_MASK | HARDIRQ_MASK))
rctx++;
if (pc & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET))
rctx++;

return rctx;
}

-- Steve

\
 
 \ /
  Last update: 2020-10-30 21:23    [W:0.428 / U:0.256 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site