mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [RFC PATCHSET sched_ext/for-7.4-fixes] sched_ext: sub: Don't run ops.sub_caps_updated() in an SCX op context
@ 2026-11-25 15:34 Kuba Piecuch
  2026-11-25 15:34 ` [RFC PATCH 1/2] sched_ext: sub: Defer ops.sub_caps_updated() out of SCX op contexts Kuba Piecuch
  2026-11-25 15:34 ` [RFC PATCH 2/2] selftests/sched_ext: Test that ops.sub_caps_updated() runs unlocked Kuba Piecuch
  0 siblings, 2 replies; 3+ messages in thread
From: Kuba Piecuch @ 2026-11-25 15:34 UTC (permalink / raw)
  To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min
  Cc: Kuba Piecuch, sched-ext, linux-kernel

scx_bpf_sub_grant() and scx_bpf_sub_revoke() are in scx_kfunc_ids_any, so
a parent scheduler can call them from any op. Before returning, they
deliver ops.sub_caps_updated() to the affected children synchronously:

  caps_updated_deliver()
    SCX_CALL_OP(sch, sub_caps_updated, NULL, cu->cmask_arena_out, caps);

__SCX_CALL_OP() leaves scx_locked_rq_state untouched when locked_rq is
NULL. Unlike other SCX ops, which either always run unlocked or always
hold a known rq lock, ops.sub_caps_updated() inherits whatever context its
parent happened to call grant or revoke from:

1) Inconsistent locking context across calls. When the parent calls
   grant/revoke from ops.sub_attach() or a BPF timer, scx_locked_rq() is
   NULL; when it calls them from an rq-locked op, scx_locked_rq() still
   points at the parent's rq. For example, scx_qmap nested three deep runs
   a grandchild's ops.sub_caps_updated() under the middle scheduler's rq
   lock when ops.sub_ecaps_updated() picks up a pending PART_REDISTRIBUTE
   in execute_partition() -> apply_partition().

   ops.sub_caps_updated() has no scx_kf_allow_flags[] entry (only the
   universal any/idle/cid kfunc sets are allowed), yet kfuncs in
   scx_kfunc_ids_any consult scx_locked_rq() and the child has no safe way
   to probe which state it is in: scx_bpf_locked_rq() aborts the child
   when unlocked, SCX_DSQ_LOCAL in scx_bpf_dsq_reenq() and
   scx_bpf_dsq_nr_queued() resolves to scx_locked_rq() ?: this_rq(), and
   scx_bpf_cidperf_set() (for example on cids where SCX_CAP_PERF is
   already effective) works across all cids when unlocked but aborts the
   child on any cid outside the parent's rq:

     CPU 2, parent P, child sub-scheduler C
       enqueue_task_scx(rq2, p)                    // rq2 locked
         SCX_CALL_OP_TASK(P, enqueue, rq2, p)      // scx_locked_rq() = rq2
           P.enqueue(p)
             scx_bpf_sub_grant(C, SCX_CAP_PERF, cids)
               caps_updated_deliver()
                 SCX_CALL_OP(C, sub_caps_updated, NULL)   // still rq2
                   C.sub_caps_updated()
                     scx_bpf_cidperf_set(cid of CPU 1, ...)
                       scx_cpuperf_set()
                         scx_error("Invalid target CPU 1")  // C aborted

     sched_ext: BPF sub-scheduler "sub_caps_ctx" disabled (runtime error)
     sched_ext: sub_caps_ctx: Invalid target CPU 1
        bpf__sched_ext_ops_cid_sub_caps_updated+0x57/0xb7
        bpf__sched_ext_ops_cid_enqueue+0x50/0xb0

2) Parent lock hold time across subtree cascades. ops.sub_caps_updated()
   is meant to track held shard caps and propagate grant/revoke to
   children. A single grant or revoke can synchronously fan out across a
   subtree of descendant callbacks (up to SCX_SUB_MAX_DEPTH levels, with
   multiple children per level) while the parent's rq lock, p->pi_lock
   (from ops.select_cpu()) or scx_dump_lock (from ops.dump*()) remains
   held, delaying other CPUs contending on that lock.

3) Dump state leakage. When the call comes from ops.dump*(),
   scx_dump_data.cpu remains set, so a child calling scx_bpf_dump() passes
   the check in scx_bpf_dump_bstr() and writes into the parent's dump
   instead of failing.

Sending as an RFC because there is no obvious best way to resolve this:

- Restricting scx_bpf_sub_grant() and scx_bpf_sub_revoke() to unlocked ops
  would avoid deferred delivery, but would disallow repartitioning from
  rq-locked callbacks such as scx_qmap's ops.sub_ecaps_updated().

- Keeping synchronous delivery as-is, handling scx_dump_data separately
  (or rejecting grant/revoke during dump), and documenting that
  ops.sub_caps_updated() inherits the caller's locking context would avoid
  irq_work while keeping kfuncs deadlock-free, at the cost of an
  inconsistent locked/unlocked context and holding parent locks across
  subtree cascades.

- Deferring delivery to irq_work when called from an SCX op context (this
  series) keeps grant/revoke callable from any op while giving
  ops.sub_caps_updated() a consistent unlocked context and releasing
  parent locks and dump state before subtree callbacks run, at the cost of
  extra irq_work, hotplug, PM and teardown machinery.

Patch 1 adds caps_updated_deliver_or_defer() for grant/revoke. When the
caller has an rq locked, has kf_tasks[] set, is in ops.select_cpu() or is
dumping (the new scx_in_dump()), the to_deliver list is spliced onto a
global pending list and a per-cpu hard irq_work delivers it. Each cpu uses
its own hard irq_work so concurrent deliveries across cpus cannot clear
IRQ_WORK_BUSY early, and so a dying cpu on PREEMPT_RT doesn't strand work
after its irq_work thread is parked; if this cpu is already offline (such
as ops.update_idle() when hotplug parks the migration thread), an online
cpu's irq_work is kicked instead. While PM is in progress, kicking is
suppressed so irq_work_queue() never runs in the syscore phase, and
scx_sub_pm_end() kicks any queued deliveries once the PM cycle ends.
Otherwise delivery stays synchronous, as before. Queued scx_caps_updated
nodes stay in flight, so later changes keep coalescing into them.
scx_free_pshards() drops a dying scheduler's queued entries and flushes
every cpu's irq_work before freeing the pshards.

Patch 2 adds the sub_caps_ctx selftest. A cid-form root grants/revokes
SCX_CAP_PERF to a child from ops.enqueue() and SCX_CAP_PREEMPT from
ops.select_cid(). The child calls scx_bpf_cidperf_set() on every cid from
ops.sub_caps_updated(). Without patch 1, sub_caps_ctx fails with:

  Scheduler exited unexpectedly (kind=1024 code=0 msg='Invalid target CPU 0')

The patches were written with the help of an AI coding assistant.

Kuba Piecuch (2):
  sched_ext: sub: Defer ops.sub_caps_updated() out of SCX op contexts
  selftests/sched_ext: Test that ops.sub_caps_updated() runs unlocked

 kernel/sched/ext/ext.c                        |   6 +
 kernel/sched/ext/internal.h                   |   8 +
 kernel/sched/ext/sub.c                        | 129 +++++++++++++-
 tools/testing/selftests/sched_ext/Makefile    |   1 +
 .../selftests/sched_ext/sub_caps_ctx.bpf.c    | 136 +++++++++++++++
 .../selftests/sched_ext/sub_caps_ctx.c        | 165 ++++++++++++++++++
 6 files changed, 441 insertions(+), 4 deletions(-)
 create mode 100644 tools/testing/selftests/sched_ext/sub_caps_ctx.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/sub_caps_ctx.c


base-commit: 97e1c7d467d07734f8f5c8c64730e1f7c415bd46
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-25 15:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 15:34 [RFC PATCHSET sched_ext/for-7.4-fixes] sched_ext: sub: Don't run ops.sub_caps_updated() in an SCX op context Kuba Piecuch
2026-11-25 15:34 ` [RFC PATCH 1/2] sched_ext: sub: Defer ops.sub_caps_updated() out of SCX op contexts Kuba Piecuch
2026-11-25 15:34 ` [RFC PATCH 2/2] selftests/sched_ext: Test that ops.sub_caps_updated() runs unlocked Kuba Piecuch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®