Re: [PATCH] sched_ext: Reject NMI calls to lock-taking kfuncs

From: Tejun Heo

Date: Tue Sep 01 2026 - 17:16:25 EST


Hello,

On Tue, Sep 01, 2026 at 05:56:52PM +0800, Wanwu Li wrote:
> + if (!scx_kfunc_nmi_safe(__func__, sch))
> + return -EBUSY;

The return value doesn't matter much as the scheduler is being terminated.
Whatever it is, use the same one everywhere. -EBUSY suggests retrying would
help. Let's use -EDEADLK which is what's being avoided.

> +/*
> + * sched_ext kfuncs that take scheduler locks are not NMI-safe: a
> + * BPF_PROG_TYPE_TRACING program can be attached to a function that runs in
> + * NMI, and scx_kfunc_context_filter() lets such a program call every kfunc in
> + * the any/cid/idle sets. Acquiring the rq, dsq or pshard raw spinlocks - or
> + * touching the irq-masking-only kick list - from NMI while the interrupted
> + * context on the same CPU already holds them deadlocks (or corrupts) it.
> + * scx_bpf_kick_cpu() was the first guard; route all of them through here.
> + *
> + * Returns true when the caller may proceed, false when running from NMI and
> + * the kfunc must bail without touching locks. scx_error() is NMI-safe (see the
> + * lock-free ->aborting claim).
> + */

Too long. Something like:

/*
* Tracing progs can call kfuncs from NMI. Kfuncs that take scheduler locks or
* touch the kick lists, which are only protected by irq masking, can't run
* there, so abort the scheduler instead. scx_error() is NMI-safe.
*/

> +static inline bool scx_kfunc_nmi_safe(const char *who, struct scx_sched *sch)

scx_kfunc_nmi_safe() reads as if it's testing a property of the kfunc. Let's
call it scx_kf_allowed_ctx() to match scx_kf_allowed(). Also, instead of
passing @who from each call site, make it a macro wrapper which passes
__func__ to the inline function.

Thanks.

--
tejun