Re: [PATCH v4 14/18] static_call: Add static_cond_call()
From: Peter Zijlstra
Date: Sun May 03 2020 - 08:59:04 EST
On Sat, May 02, 2020 at 03:08:00PM +0200, Rasmus Villemoes wrote:
> On 01/05/2020 22.29, Peter Zijlstra wrote:
> > Extend the static_call infrastructure to optimize the following common
> > pattern:
> >
> > if (func_ptr)
> > func_ptr(args...)
> >
> > +
> > #define static_call(name) __static_call(name)
> > +#define static_cond_call(name) (void)__static_call(name)
> >
> > +
> > #define static_call(name) __static_call(name)
> > +#define static_cond_call(name) (void)__static_call(name)
> >
>
> > +#define static_cond_call(name) \
> > + if (STATIC_CALL_KEY(name).func) \
> > + ((typeof(STATIC_CALL_TRAMP(name))*)(STATIC_CALL_KEY(name).func))
> > +
>
> This addresses neither the READ_ONCE issue nor the fact that,
> AFAICT,
> the semantics of
>
> static_cond_call(foo)(i++)
>
> will depend on CONFIG_HAVE_STATIC_CALL.
True.
So there is something utterly terrible we can do to address both:
void __static_call_nop(void)
{
}
#define __static_cond_call(name) \
({ \
void *func = READ_ONCE(STATIC_CALL_KEY(name).func); \
if (!func) \
func = &__static_call_nop; \
(typeof(STATIC_CALL_TRAMP(name))*)func; \
})
#define static_cond_call(name) (void)__static_cond_call(name)
This gets us into Undefined Behaviour territory, but it ought to work.
It adds the READ_ONCE(), and it cures the argument evaluation issue.
> Also, I'd have appreciated being
> cc'ed on new revisions instead of stumbling on it by chance.
Sorry, my bad, I forgot about that :-/. I rushed to repost, and simply
forgot a few things.