Re: [PATCH] preempt: add in_serving_irq() and apply to rcutiny and vsprintf

From: Changbin Du
Date: Thu Aug 19 2021 - 19:00:55 EST


On Thu, Aug 19, 2021 at 09:56:45AM +0800, Boqun Feng wrote:
> [Cc Thomas and Frederic since they contributed the clean-up to these
> macros recently]
>
> Background for discussion:
>
> https://lore.kernel.org/lkml/20210814014234.51395-1-changbin.du@xxxxxxxxx/
>
> On Thu, Aug 19, 2021 at 07:59:16AM +0800, Changbin Du wrote:
> > On Tue, Aug 17, 2021 at 12:03:16AM +0800, Boqun Feng wrote:
> > > On Sat, Aug 14, 2021 at 09:42:34AM +0800, Changbin Du wrote:
> > > > At some places we need to determine whether we're in nmi, hardirq or
> > > > softirq context. This adds a macro in_serving_irq() as a shortcut for
> > > > that.
> > > >
> > > > Meanwhile, apply this new macro to existing code in rcutiny and vsprintf.
> > > >
> > > > Signed-off-by: Changbin Du <changbin.du@xxxxxxxxx>
> > > > ---
> > > > include/linux/preempt.h | 4 +++-
> > > > include/linux/rcutiny.h | 3 +--
> > > > lib/vsprintf.c | 2 +-
> > > > 3 files changed, 5 insertions(+), 4 deletions(-)
> > > >
> > > > diff --git a/include/linux/preempt.h b/include/linux/preempt.h
> > > > index 9881eac0698f..9a1c924e2c6c 100644
> > > > --- a/include/linux/preempt.h
> > > > +++ b/include/linux/preempt.h
> > > > @@ -92,12 +92,14 @@
> > > > * in_nmi() - We're in NMI context
> > > > * in_hardirq() - We're in hard IRQ context
> > > > * in_serving_softirq() - We're in softirq context
> > > > + * in_serving_irq() - We're in nmi, hardirq or softirq context
> > > > * in_task() - We're in task context
> > > > */
> > > > #define in_nmi() (nmi_count())
> > > > #define in_hardirq() (hardirq_count())
> > > > #define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
> > > > -#define in_task() (!(in_nmi() | in_hardirq() | in_serving_softirq()))
> > > > +#define in_serving_irq() (in_nmi() | in_hardirq() | in_serving_softirq())
> > > > +#define in_task() (!in_serving_irq())
> > > >
> > >
> > > So in_serving_irq() is !in_task(), right? If so, why not...
> > >
> > Adding in_serving_irq() is to reflect the real purpose so improve readability.
> > And can we preserve that !in_task() means in serving irq context in future? I don't know.
> >
>
> Sure, no one could predict the future. But if a third context (other
> than thread context and {hard,soft}irq context) comes up, which I think
> is highly unlikely, we could (and should) audit all callsites of
> in_task() for necessary adjustment. And introducing in_serving_irq()
> won't help us in that case, because we will still need to audit usage of
> in_serving_irq(), for example, let's say rcu_is_idle_cpu() for RCU_TINY
> is defined as
>
> #define rcu_is_idle_cpu(cpu) (is_idle_task(current) && !in_serving_irq())
>
> and we have a new type of context, and we can use in_other() to test
> whether we are in it. Now even with in_serving_irq() introduced, we
> still need to make sure the correct version of rcu_is_idle_cpu() is
> either
>
> (is_idle_task(current) && (!in_serving_irq() && !in_other()))
>
> or
>
> (is_idle_task(current) && !in_serving_irq())
>
> Therefore, I don't see the point of introducing in_serving_irq().
>
ok, as in_serving_irq() is only used in two places, it is not common to judge if
it is in serving irq context. So this new macro doesn't help much.

> Regards,
> Boqun
>
> > --
> > Cheers,
> > Changbin Du

--
Cheers,
Changbin Du