Re: [PATCH 4/6] perf: Optimize get_recursion_context()

From: Peter Zijlstra
Date: Fri Oct 30 2020 - 19:02:33 EST


On Fri, Oct 30, 2020 at 04:22:48PM -0400, Steven Rostedt wrote:
> 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.

Works for me, however:

> /*
> * 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;

unsigned

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

otherwise you'll get an extra instruction to sign extend it, which is
daft (yes, i've been staring at GCC output far too much).

Also, gcc-9 does worse (like 1 byte iirc) with:

rctx += !!(pc & (NMI_MASK));
rctx += !!(pc & (NMI_MASK | HARDIRQ_MASK));
rctx += !!(pc & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET));

but gcc-10 doesn't seem to care.