mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kuba Piecuch <jpiecuch@google.com>
To: Tejun Heo <tj@kernel.org>, David Vernet <void@manifault.com>,
	Andrea Righi <arighi@nvidia.com>,
	 Changwoo Min <changwoo@igalia.com>
Cc: Kuba Piecuch <jpiecuch@google.com>,
	sched-ext@lists.linux.dev,  linux-kernel@vger.kernel.org
Subject: [RFC PATCH 1/2] sched_ext: sub: Defer ops.sub_caps_updated() out of SCX op contexts
Date: Wed, 25 Nov 2026 15:34:20 +0000	[thread overview]
Message-ID: <20261125153353.1364467-2-jpiecuch@google.com> (raw)
In-Reply-To: <20261125153353.1364467-1-jpiecuch@google.com>

scx_bpf_sub_grant() and scx_bpf_sub_revoke() are in scx_kfunc_ids_any and
deliver ops.sub_caps_updated() synchronously with a NULL locked_rq.
__SCX_CALL_OP() leaves scx_locked_rq_state untouched when locked_rq is
NULL, so ops.sub_caps_updated() inherits whatever context its parent
called grant or revoke from:

- From ops.sub_attach() or a BPF timer, scx_locked_rq() is NULL. From an
  rq-locked op (such as ops.enqueue(), or scx_qmap repartitioning a
  grandchild from ops.sub_ecaps_updated()), scx_locked_rq() points at the
  parent's rq. Unlike other callbacks, the child cannot safely tell which
  state it is in: scx_bpf_locked_rq() aborts the child when unlocked,
  scx_bpf_cidperf_set() aborts it with "Invalid target CPU" on any cid
  outside the parent's rq, and SCX_DSQ_LOCAL in scx_bpf_dsq_reenq() and
  scx_bpf_dsq_nr_queued() resolves to scx_locked_rq() ?: this_rq().

- Because ops.sub_caps_updated() may propagate grant/revoke to its own
  children, synchronous delivery can fan out across a subtree of
  descendant callbacks while holding the parent's rq lock, p->pi_lock
  (from ops.select_cpu()) or scx_dump_lock (from ops.dump*()), extending
  lock hold time.

- From ops.dump*(), scx_dump_data.cpu remains set, so a child calling
  scx_bpf_dump() writes into the parent's dump instead of failing.

Give ops.sub_caps_updated() a consistent unlocked context by deferring
delivery when grant or revoke runs in an SCX op context (an rq lock held,
kf_tasks[] set, ops.select_cpu() or ops.dump*()). Queue the deliveries on
a global list and kick a per-cpu hard irq_work, or an online cpu's when
this cpu is going down. Suppress the kick while PM is in progress, where
irq_work_queue() can malfunction in the syscore phase, and kick any queued
deliveries from scx_sub_pm_end(). Queued nodes stay in flight so later
updates keep coalescing into them. Before freeing a scheduler's pshards,
scx_free_pshards() drops its queued entries and flushes every cpu's
irq_work.

Fixes: 5f2a9a4c2e6c ("sched_ext: Add coalescing sub_caps_updated() notifier for sub-schedulers")
Cc: stable@vger.kernel.org # v7.3+
Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
 kernel/sched/ext/ext.c      |   6 ++
 kernel/sched/ext/internal.h |   8 +++
 kernel/sched/ext/sub.c      | 129 ++++++++++++++++++++++++++++++++++--
 3 files changed, 139 insertions(+), 4 deletions(-)

diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index efa1d01d03af..570d6c923904 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -7964,6 +7964,12 @@ static void ops_dump_exit(void)
 	scx_dump_data.cpu = -1;
 }
 
+/* whether this cpu is in scx_dump_state(), where scx_bpf_dump() is allowed */
+bool scx_in_dump(void)
+{
+	return READ_ONCE(scx_dump_data.cpu) == raw_smp_processor_id();
+}
+
 static void scx_dump_task(struct scx_sched *sch, struct seq_buf *s, struct scx_dump_ctx *dctx,
 			  struct rq *rq, struct task_struct *p, char marker)
 {
diff --git a/kernel/sched/ext/internal.h b/kernel/sched/ext/internal.h
index 58652370da5a..69846ac85a44 100644
--- a/kernel/sched/ext/internal.h
+++ b/kernel/sched/ext/internal.h
@@ -909,6 +909,8 @@ struct sched_ext_ops {
 	 * cpu, once it is in effect.
 	 *
 	 * May call scx_bpf_sub_grant() / scx_bpf_sub_revoke() on children.
+	 * Even if the parent called grant / revoke under an rq lock or from
+	 * ops.select_cpu() or ops.dump*(), this callback runs unlocked.
 	 */
 	void (*sub_caps_updated)(const struct scx_cmask *cmask, u64 caps);
 
@@ -1666,6 +1668,11 @@ enum scx_cap_flags {
  *
  * (3) leads to deferred delivery. Events accumulate during grant/revoke and are
  * delivered after the shard lock is released.
+ *
+ * When grant or revoke runs in an SCX op context (an rq lock held,
+ * ops.select_cpu() or ops.dump*()), caps_updated_deliver_or_defer() defers the
+ * delivery to an irq_work so that ops.sub_caps_updated() always runs unlocked
+ * instead of inheriting the caller's context.
  */
 struct scx_caps_updated {
 	raw_spinlock_t		lock;
@@ -2314,6 +2321,7 @@ bool scx_consume_dispatch_q(struct scx_sched *sch, struct rq *rq,
 			    struct scx_dispatch_q *dsq, u64 enq_flags);
 bool scx_consume_global_dsq(struct scx_sched *sch, struct rq *rq);
 bool scx_rq_online(struct rq *rq);
+bool scx_in_dump(void);
 void scx_flush_dispatch_buf(struct scx_sched *sch, struct rq *rq);
 s32 scx_init_dsq(struct scx_dispatch_q *dsq, u64 dsq_id, struct scx_sched *sch);
 __printf(2, 3) void scx_dump_line(struct seq_buf *s, const char *fmt, ...);
diff --git a/kernel/sched/ext/sub.c b/kernel/sched/ext/sub.c
index 7c069a289766..d535b9258b10 100644
--- a/kernel/sched/ext/sub.c
+++ b/kernel/sched/ext/sub.c
@@ -66,7 +66,10 @@ static DECLARE_WAIT_QUEUE_HEAD(ecaps_offline_waitq);
 static struct cpumask scx_frozen_cpus;
 static DECLARE_WAIT_QUEUE_HEAD(scx_frozen_cpus_waitq);
 
-/* between the PREPARE and POST notifications of a PM cycle */
+/*
+ * True between the PREPARE and POST notifications of a PM cycle. Updated under
+ * caps_updated_deferred_lock.
+ */
 static bool scx_pm_in_progress;
 
 /**
@@ -307,6 +310,47 @@ void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch)
 		rcu_assign_pointer(pos->scx_sched, sch);
 }
 
+/*
+ * Queue of ops.sub_caps_updated() deliveries deferred out of SCX op contexts.
+ * Queued entries stay linked on scx_caps_updated->node_in_flight so that
+ * caps_updated_record() keeps coalescing into them.
+ *
+ * Each cpu has its own hard irq_work:
+ * - A single irq_work queued on multiple cpus can run irq_work_single()
+ *   concurrently and clear IRQ_WORK_BUSY early, breaking irq_work_sync().
+ * - A dying cpu still runs SCX ops after hotplug parks its irq_work thread,
+ *   which would strand a regular irq_work on PREEMPT_RT.
+ */
+static void caps_updated_deferred_workfn(struct irq_work *irq_work);
+static LIST_HEAD(caps_updated_deferred);
+static DEFINE_RAW_SPINLOCK(caps_updated_deferred_lock);
+static DEFINE_PER_CPU(struct irq_work, caps_updated_deferred_irq_work) =
+	IRQ_WORK_INIT_HARD(caps_updated_deferred_workfn);
+
+/*
+ * Drop @sch's deferred caps_updated deliveries before freeing its pshards.
+ * scx_sched_free_rcu_work() calls this after two RCU grace periods post
+ * unlink, so scx_bpf_sub_grant/revoke() cannot queue new entries for @sch.
+ */
+static void caps_updated_cancel_deferred(struct scx_sched *sch)
+{
+	struct scx_caps_updated *cu, *tmp;
+	s32 cpu;
+
+	scoped_guard (raw_spinlock_irqsave, &caps_updated_deferred_lock) {
+		list_for_each_entry_safe(cu, tmp, &caps_updated_deferred, node_in_flight) {
+			if (container_of(cu, struct scx_pshard, caps_updated)->sch != sch)
+				continue;
+			guard(raw_spinlock)(&cu->lock);
+			list_del_init(&cu->node_in_flight);
+		}
+	}
+
+	/* wait for any workfn that already spliced @sch's entries */
+	for_each_possible_cpu(cpu)
+		irq_work_sync(per_cpu_ptr(&caps_updated_deferred_irq_work, cpu));
+}
+
 static void free_pshard(struct scx_pshard *pshard)
 {
 	struct scx_caps_updated *cu;
@@ -327,6 +371,7 @@ void scx_free_pshards(struct scx_sched *sch)
 
 	if (!sch->pshard)
 		return;
+	caps_updated_cancel_deferred(sch);
 	for (si = 0; si < sch->nr_pshards; si++)
 		free_pshard(sch->pshard[si]);
 	kfree(sch->pshard);
@@ -1034,6 +1079,77 @@ static void caps_updated_deliver(struct list_head *to_deliver)
 	}
 }
 
+static void caps_updated_deferred_workfn(struct irq_work *irq_work)
+{
+	LIST_HEAD(to_deliver);
+
+	/* hard irq_work, same irq-disabled context as a synchronous delivery */
+	lockdep_assert_irqs_disabled();
+
+	scoped_guard (raw_spinlock, &caps_updated_deferred_lock)
+		list_splice_init(&caps_updated_deferred, &to_deliver);
+
+	caps_updated_deliver(&to_deliver);
+}
+
+static void caps_updated_kick_deferred(void)
+{
+	s32 cpu = smp_processor_id();
+
+	lockdep_assert_held(&caps_updated_deferred_lock);
+	lockdep_assert_irqs_disabled();
+
+	/*
+	 * During the PM syscore phase, irq_work_queue() can malfunction.
+	 * scx_sub_pm_end() kicks the queue when the PM cycle finishes.
+	 */
+	if (unlikely(scx_pm_in_progress))
+		return;
+
+	if (likely(cpu_online(cpu))) {
+		irq_work_queue(this_cpu_ptr(&caps_updated_deferred_irq_work));
+		return;
+	}
+
+	/*
+	 * When hotplug parks a dying cpu's migration thread, ops.update_idle()
+	 * runs after smpcfd_dying_cpu() already drained local irq_work. Route
+	 * to an online cpu; cpus_write_lock() keeps cpu_online_mask stable.
+	 */
+	cpu = cpumask_first(cpu_online_mask);
+	if (likely(cpu < nr_cpu_ids))
+		irq_work_queue_on(per_cpu_ptr(&caps_updated_deferred_irq_work, cpu),
+				  cpu);
+}
+
+/*
+ * If the caller runs in an SCX op context (an rq lock held, ops.select_cpu()
+ * or ops.dump*()), defer @to_deliver to an irq_work. Otherwise deliver it now.
+ *
+ * ops.sub_caps_updated() is called with a NULL locked_rq, which leaves
+ * scx_locked_rq() untouched. Delivering synchronously here would inherit the
+ * parent's scx_locked_rq() or dump state and hold the parent's rq lock, pi_lock
+ * or scx_dump_lock across any cascading grant/revoke down the subtree.
+ */
+static void caps_updated_deliver_or_defer(struct list_head *to_deliver)
+{
+	lockdep_assert_irqs_disabled();
+
+	if (likely(!scx_locked_rq() && !current->scx.kf_tasks[0] &&
+		   !this_rq()->scx.in_select_cpu && !scx_in_dump())) {
+		caps_updated_deliver(to_deliver);
+		return;
+	}
+
+	if (list_empty(to_deliver))
+		return;
+
+	scoped_guard (raw_spinlock, &caps_updated_deferred_lock) {
+		list_splice_tail_init(to_deliver, &caps_updated_deferred);
+		caps_updated_kick_deferred();
+	}
+}
+
 /*
  * Deliver caps owed to @sch that couldn't be delivered earlier (e.g. a grant
  * taken during its sub_attach(), before has_op was set). Called once has_op is
@@ -1325,6 +1441,7 @@ void __scx_process_sync_ecaps(struct rq *rq, struct task_struct *prev)
 
 void scx_sub_pm_begin(void)
 {
+	guard(raw_spinlock_irqsave)(&caps_updated_deferred_lock);
 	WRITE_ONCE(scx_pm_in_progress, true);
 }
 
@@ -1376,7 +1493,11 @@ void scx_sub_pm_end(void)
 				   cpumask_weight(&scx_frozen_cpus));
 	}
 
-	WRITE_ONCE(scx_pm_in_progress, false);
+	scoped_guard (raw_spinlock_irqsave, &caps_updated_deferred_lock) {
+		WRITE_ONCE(scx_pm_in_progress, false);
+		if (!list_empty(&caps_updated_deferred))
+			caps_updated_kick_deferred();
+	}
 	cpumask_clear(&scx_frozen_cpus);
 	wake_up_all(&scx_frozen_cpus_waitq);
 }
@@ -2981,7 +3102,7 @@ __bpf_kfunc s32 scx_bpf_sub_grant(u64 cgroup_id, u64 caps,
 
 	ret = any_denied ? -EPERM : 0;
 out:
-	caps_updated_deliver(&to_deliver);
+	caps_updated_deliver_or_defer(&to_deliver);
 	return ret;
 }
 
@@ -3073,7 +3194,7 @@ __bpf_kfunc void scx_bpf_sub_revoke(u64 cgroup_id, u64 caps,
 		}
 	}
 
-	caps_updated_deliver(&to_deliver);
+	caps_updated_deliver_or_defer(&to_deliver);
 }
 
 /**
-- 
2.56.1.839.g22a8290db9-goog


  reply	other threads:[~2026-11-25 15:34 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Kuba Piecuch [this message]
2026-11-25 15:34 ` [RFC PATCH 2/2] selftests/sched_ext: Test that ops.sub_caps_updated() runs unlocked Kuba Piecuch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125153353.1364467-2-jpiecuch@google.com \
    --to=jpiecuch@google.com \
    --cc=arighi@nvidia.com \
    --cc=changwoo@igalia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=tj@kernel.org \
    --cc=void@manifault.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®