All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Richard Weinberger (sigma star gmbh)" <rw@kernel.org>
To: xenomai@lists.linux.dev
Cc: upstream+xenomai@sigma-star.at, Richard Weinberger <richard@nod.at>
Subject: [PATCH 2/5] cobalt/sched: add RT throttling
Date: Tue,  1 Dec 2026 15:12:04 +0100	[thread overview]
Message-ID: <20261201141207.2876277-3-rw@kernel.org> (raw)
In-Reply-To: <20261201141207.2876277-1-rw@kernel.org>

From: Richard Weinberger <richard@nod.at>

Add an optional per-CPU limit on the CPU time Cobalt may use,
similar to sched_rt_runtime_us/sched_rt_period_us in Linux. Within
each window of xenomai.sched_rt_period_us, Cobalt threads may run for
at most xenomai.sched_rt_runtime_us. Once the budget is used up, Linux
gets the CPU until the window ends.

The use cases are CPU bandwidth throttling and a safety net against
buggy real-time applications, which would otherwise lock up a CPU and
starve Linux.

The limit applies to every Cobalt thread regardless of its scheduling
class and overrides the scheduler lock. The parameters can only be set
at boot time.

Scheduling classes keeping per-thread budgets can implement the new
sched_throttle hook to stop accounting when the throttle switches a
thread out.

Signed-off-by: Richard Weinberger <richard@nod.at>
---
 doc/asciidoc/README.INSTALL.adoc       |  12 ++
 include/cobalt/kernel/sched-throttle.h |  92 +++++++++
 include/cobalt/kernel/sched.h          |  25 ++-
 kernel/cobalt/Kconfig                  |  26 +++
 kernel/cobalt/Makefile                 |   1 +
 kernel/cobalt/sched-throttle.c         | 266 +++++++++++++++++++++++++
 kernel/cobalt/sched.c                  |  31 ++-
 7 files changed, 450 insertions(+), 3 deletions(-)
 create mode 100644 include/cobalt/kernel/sched-throttle.h
 create mode 100644 kernel/cobalt/sched-throttle.c

diff --git a/doc/asciidoc/README.INSTALL.adoc b/doc/asciidoc/README.INSTALL.adoc
index 5797b8fc0..8f8d7a0aa 100644
--- a/doc/asciidoc/README.INSTALL.adoc
+++ b/doc/asciidoc/README.INSTALL.adoc
@@ -150,6 +150,18 @@ This option activates a watchdog aimed at detecting runaway Cobalt threads.
 Timeout value can be set via CONFIG_XENO_OPT_WATCHDOG_TIMEOUT, with a range
 from 1 to 60. | 4
 
+|xenomai.sched_rt_period_us=<usecs> | Length of the real-time throttling
+window. Requires CONFIG_XENO_OPT_SCHED_THROTTLE to be enabled in kernel
+configuration.
+| 1000000
+
+|xenomai.sched_rt_runtime_us=<usecs> | CPU time Cobalt threads may
+consume on each CPU within every throttling window, regardless of their
+scheduling class. Once exhausted, Linux runs until the window ends, even
+if a Cobalt thread holds the scheduler lock. -1 disables throttling.
+Requires CONFIG_XENO_OPT_SCHED_THROTTLE to be enabled in kernel configuration.
+| 950000
+
 |xenomai.smi=<state> | *x86-specific*: Set the state of the SMI
 workaround. The possible values are _disabled_, _detect_ and
 _enabled_. See the discussion about link:dealing-with-x86-SMI[SMIs]
diff --git a/include/cobalt/kernel/sched-throttle.h b/include/cobalt/kernel/sched-throttle.h
new file mode 100644
index 000000000..fe2022fc1
--- /dev/null
+++ b/include/cobalt/kernel/sched-throttle.h
@@ -0,0 +1,92 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2026 Richard Weinberger
+ */
+#ifndef _COBALT_KERNEL_SCHED_THROTTLE_H
+#define _COBALT_KERNEL_SCHED_THROTTLE_H
+
+#ifndef _COBALT_KERNEL_SCHED_H
+#error "please don't include cobalt/kernel/sched-throttle.h directly"
+#endif
+
+/**
+ * @addtogroup cobalt_core_sched
+ * @{
+ */
+
+struct xnsched;
+struct xnvfile_directory;
+
+#ifdef CONFIG_XENO_OPT_SCHED_THROTTLE
+
+#include <linux/jump_label.h>
+
+/*
+ * Per-CPU RT throttling state. All dates and durations are in ns,
+ * read from nkclock.
+ */
+struct xnsched_throttle {
+	/* Fires when the budget is used up or the window ends. */
+	struct xntimer budget_timer;
+	/* Fires at the end of the window the CPU is throttled in. */
+	struct xntimer window_timer;
+	/* Start of the current window, a multiple of the period. */
+	xnticks_t window_start;
+	/* Cobalt time charged to the current window. */
+	xnticks_t used;
+	/* Cobalt time since this date is not charged yet. */
+	xnticks_t charge_from;
+	/* Number of times the budget was used up. */
+	unsigned long count;
+};
+
+DECLARE_STATIC_KEY_FALSE(xnsched_throttle_enabled);
+
+void xnsched_throttle_setup(void);
+
+void xnsched_throttle_init(struct xnsched *sched);
+
+void xnsched_throttle_destroy(struct xnsched *sched);
+
+void __xnsched_throttle_leave_root(struct xnsched *sched);
+
+void __xnsched_throttle_enter_root(struct xnsched *sched);
+
+/* Linux hands the CPU over to Cobalt: start charging. */
+static inline void xnsched_throttle_leave_root(struct xnsched *sched)
+{
+	if (static_branch_unlikely(&xnsched_throttle_enabled))
+		__xnsched_throttle_leave_root(sched);
+}
+
+/* Cobalt hands the CPU back to Linux: stop charging. */
+static inline void xnsched_throttle_enter_root(struct xnsched *sched)
+{
+	if (static_branch_unlikely(&xnsched_throttle_enabled))
+		__xnsched_throttle_enter_root(sched);
+}
+
+int xnsched_throttle_init_proc(struct xnvfile_directory *vfroot);
+
+void xnsched_throttle_cleanup_proc(void);
+
+#else /* !CONFIG_XENO_OPT_SCHED_THROTTLE */
+
+static inline void xnsched_throttle_setup(void) { }
+static inline void xnsched_throttle_init(struct xnsched *sched) { }
+static inline void xnsched_throttle_destroy(struct xnsched *sched) { }
+static inline void xnsched_throttle_leave_root(struct xnsched *sched) { }
+static inline void xnsched_throttle_enter_root(struct xnsched *sched) { }
+
+static inline int xnsched_throttle_init_proc(struct xnvfile_directory *vfroot)
+{
+	return 0;
+}
+
+static inline void xnsched_throttle_cleanup_proc(void) { }
+
+#endif /* !CONFIG_XENO_OPT_SCHED_THROTTLE */
+
+/** @} */
+
+#endif /* !_COBALT_KERNEL_SCHED_THROTTLE_H */
diff --git a/include/cobalt/kernel/sched.h b/include/cobalt/kernel/sched.h
index e0b4249a4..539d4148e 100644
--- a/include/cobalt/kernel/sched.h
+++ b/include/cobalt/kernel/sched.h
@@ -28,6 +28,7 @@
 #include <cobalt/kernel/sched-sporadic.h>
 #include <cobalt/kernel/sched-quota.h>
 #include <cobalt/kernel/vfile.h>
+#include <cobalt/kernel/sched-throttle.h>
 #include <cobalt/kernel/assert.h>
 #include <asm/xenomai/machine.h>
 #include <pipeline/sched.h>
@@ -47,6 +48,7 @@
 #define XNHTICK		0x00008000	/* Host tick pending  */
 #define XNINIRQ		0x00004000	/* In IRQ handling context */
 #define XNHDEFER	0x00002000	/* Host tick deferred */
+#define XNTHROTTLED	0x00001000	/* RT budget exhausted */
 
 /*
  * Hardware timer is stopped.
@@ -108,6 +110,10 @@ struct xnsched {
 	/*!< Watchdog timer object. */
 	struct xntimer wdtimer;
 #endif
+#ifdef CONFIG_XENO_OPT_SCHED_THROTTLE
+	/*!< RT throttling state. */
+	struct xnsched_throttle throttle;
+#endif
 #ifdef CONFIG_XENO_OPT_STATS
 	/*!< Last account switch date (ticks). */
 	xnticks_t last_account_switch;
@@ -179,6 +185,14 @@ struct xnsched_class {
 			     const union xnsched_policy_param *p);
 	void (*sched_forget)(struct xnthread *thread);
 	void (*sched_kick)(struct xnthread *thread);
+	/**
+	 * Called on the base class of @a curr when RT throttling
+	 * switches it out in favour of the root thread, bypassing
+	 * sched_pick(). Classes that keep per-thread budgets should
+	 * stop accounting @a curr here, as they would when picking a
+	 * successor.
+	 */
+	void (*sched_throttle)(struct xnthread *curr);
 #ifdef CONFIG_XENO_OPT_VFILE
 	int (*sched_init_vfile)(struct xnsched_class *schedclass,
 				struct xnvfile_directory *vfroot);
@@ -307,6 +321,12 @@ static inline int __xnsched_run(struct xnsched *sched)
 	return pipeline_schedule(sched);
 }
 
+/* RT budget used up, Linux owns the CPU until the window ends. */
+static inline bool xnsched_throttled(struct xnsched *sched)
+{
+	return sched->lflags & XNTHROTTLED;
+}
+
 static inline int xnsched_run(void)
 {
 	struct xnsched *sched = xnsched_current();
@@ -323,7 +343,10 @@ static inline int xnsched_run(void)
 	 * If running over the root thread, hard irqs must be off
 	 * (asserted out of line in ___xnsched_run()).
 	 */
-	return curr->lock_count > 0 ? 0 : __xnsched_run(sched);
+	if (curr->lock_count > 0 && !xnsched_throttled(sched))
+		return 0;
+
+	return __xnsched_run(sched);
 }
 
 void xnsched_lock(void);
diff --git a/kernel/cobalt/Kconfig b/kernel/cobalt/Kconfig
index 4b61b2f59..266643a0d 100644
--- a/kernel/cobalt/Kconfig
+++ b/kernel/cobalt/Kconfig
@@ -126,6 +126,32 @@ config XENO_OPT_SCHED_QUOTA_NR_GROUPS
 	  The overall number of thread groups which may be defined
 	  across all CPUs.
 
+config XENO_OPT_SCHED_THROTTLE
+	bool "Real-time throttling"
+	default n
+	help
+	  Limit the CPU time Cobalt threads may consume on each CPU,
+	  similar to the RT throttling of the Linux scheduler. Within
+	  every period of xenomai.sched_rt_period_us microseconds,
+	  Cobalt threads may run for at most
+	  xenomai.sched_rt_runtime_us microseconds on a given CPU.
+	  Once this budget is exhausted, Linux gets the CPU until the
+	  period ends. Both parameters are boot-time only. Setting
+	  xenomai.sched_rt_runtime_us to -1 disables throttling.
+
+	  Windows are aligned to multiples of the period. Every Cobalt
+	  thread counts against the budget, whatever its scheduling
+	  class (including SCHED_WEAK threads running in primary
+	  mode), and throttling overrides the scheduler lock. Threads
+	  which wake up, switch to primary mode or need to relax while
+	  the CPU is throttled wait until the window ends.
+
+	  Throttling preempts real-time threads regardless of their
+	  priority, so enabling it voids real-time guarantees whenever
+	  the budget runs out. Use with care.
+
+	  If in doubt, say N.
+
 config XENO_OPT_STATS
 	bool "Runtime statistics"
 	depends on XENO_OPT_VFILE
diff --git a/kernel/cobalt/Makefile b/kernel/cobalt/Makefile
index 6cd2d5994..4bba9a742 100644
--- a/kernel/cobalt/Makefile
+++ b/kernel/cobalt/Makefile
@@ -21,6 +21,7 @@ xenomai-$(CONFIG_XENO_OPT_SCHED_QUOTA) += sched-quota.o
 xenomai-$(CONFIG_XENO_OPT_SCHED_WEAK) += sched-weak.o
 xenomai-$(CONFIG_XENO_OPT_SCHED_SPORADIC) += sched-sporadic.o
 xenomai-$(CONFIG_XENO_OPT_SCHED_TP) += sched-tp.o
+xenomai-$(CONFIG_XENO_OPT_SCHED_THROTTLE) += sched-throttle.o
 xenomai-$(CONFIG_XENO_OPT_DEBUG) += debug.o
 xenomai-$(CONFIG_XENO_OPT_PIPE) += pipe.o
 xenomai-$(CONFIG_XENO_OPT_MAP) += map.o
diff --git a/kernel/cobalt/sched-throttle.c b/kernel/cobalt/sched-throttle.c
new file mode 100644
index 000000000..a116caf95
--- /dev/null
+++ b/kernel/cobalt/sched-throttle.c
@@ -0,0 +1,266 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2026 Richard Weinberger
+ *
+ * RT throttling: limit the CPU time Cobalt may use on each CPU, like
+ * sched_rt_runtime_us/sched_rt_period_us do for Linux.
+ *
+ * Time is cut into windows of throttle_period ns, aligned to
+ * multiples of the period. Whenever a Cobalt thread runs instead of
+ * Linux (i.e. between leave_root() and enter_root()), that time is
+ * charged to the current window, whatever the scheduling class of
+ * the thread. Once throttle_runtime ns have been charged, the CPU is
+ * throttled: xnsched_pick_next() hands it to Linux until the window
+ * ends, overriding the scheduler lock.
+ *
+ * Two timers per CPU drive this, since Cobalt has no periodic tick:
+ * the budget timer runs while Cobalt has the CPU and fires when the
+ * budget is used up or the window ends; the window timer runs while
+ * the CPU is throttled and lifts the throttle at the end of the
+ * window.
+ */
+#include <linux/module.h>
+#include <linux/jump_label.h>
+#include <linux/math64.h>
+#include <cobalt/kernel/sched.h>
+#include <cobalt/kernel/timer.h>
+#include <cobalt/kernel/clock.h>
+#include <trace/events/cobalt-core.h>
+
+/* Boot-time settings, see xnsched_throttle_setup(). */
+static unsigned long period_us = 1000000;
+module_param_named(sched_rt_period_us, period_us, ulong, 0444);
+
+static long runtime_us = 950000;
+module_param_named(sched_rt_runtime_us, runtime_us, long, 0444);
+
+DEFINE_STATIC_KEY_FALSE(xnsched_throttle_enabled);
+
+static xnticks_t throttle_period __read_mostly;	/* window length, ns */
+static xnticks_t throttle_runtime __read_mostly; /* budget per window, ns */
+
+void xnsched_throttle_setup(void)
+{
+	if (runtime_us < 0 || (unsigned long)runtime_us >= period_us) {
+		printk(XENO_INFO "RT throttling disabled\n");
+		return;
+	}
+
+	throttle_period = period_us * 1000ULL;
+	throttle_runtime = runtime_us * 1000ULL;
+	static_branch_enable(&xnsched_throttle_enabled);
+	printk(XENO_INFO "RT throttling: %ld us every %lu us\n",
+	       runtime_us, period_us);
+}
+
+/* Move to the window containing @date, which must not precede it. */
+static void throttle_window_advance(struct xnsched_throttle *t,
+				    xnticks_t date)
+{
+	t->window_start += div64_u64(date - t->window_start,
+				     throttle_period) * throttle_period;
+	t->used = 0;
+}
+
+static bool throttle_window_over(struct xnsched_throttle *t, xnticks_t now)
+{
+	return (xnsticks_t)(now - t->window_start) >=
+		(xnsticks_t)throttle_period;
+}
+
+/* Charge the Cobalt time elapsed since t->charge_from. */
+static void throttle_charge(struct xnsched_throttle *t, xnticks_t now)
+{
+	if (throttle_window_over(t, now)) {
+		throttle_window_advance(t, now);
+		/* Only the time spent in the new window counts. */
+		if ((xnsticks_t)(t->charge_from - t->window_start) < 0)
+			t->charge_from = t->window_start;
+	}
+
+	t->used += now - t->charge_from;
+	t->charge_from = now;
+}
+
+/* Arm the budget timer for the budget left or the window end. */
+static void throttle_arm_budget_timer(struct xnsched_throttle *t,
+				      xnticks_t now)
+{
+	xnticks_t left = 0, window_left;
+
+	if (t->used < throttle_runtime)
+		left = throttle_runtime - t->used;
+
+	window_left = t->window_start + throttle_period - now;
+	if ((xnsticks_t)window_left < 0)
+		window_left = 0;
+
+	xntimer_start(&t->budget_timer, min(left, window_left),
+		      XN_INFINITE, XN_RELATIVE);
+}
+
+static void throttle_start(struct xnsched *sched, xnticks_t now)
+{
+	struct xnsched_throttle *t = &sched->throttle;
+	xnticks_t window_left;
+
+	sched->lflags |= XNTHROTTLED;
+	t->count++;
+
+	window_left = t->window_start + throttle_period - now;
+	if ((xnsticks_t)window_left < 0)
+		window_left = 0;
+
+	xntimer_start(&t->window_timer, window_left, XN_INFINITE, XN_RELATIVE);
+	xnsched_set_self_resched(sched);
+	trace_cobalt_sched_throttle(xnsched_cpu(sched), true);
+	printk_once(XENO_WARNING "RT throttling activated\n");
+}
+
+/* Budget timer handler: budget used up or window over. */
+static void throttle_budget_timeout(struct xntimer *timer)
+{
+	struct xnsched *sched = container_of(timer, struct xnsched,
+					     throttle.budget_timer);
+	struct xnsched_throttle *t = &sched->throttle;
+	xnticks_t now = xnclock_read_monotonic(&nkclock);
+
+	/*
+	 * enter_root() stops this timer under nklock, so curr should
+	 * never be root here. If it ever were, charging would bill
+	 * Linux time to Cobalt and throttle it for nothing.
+	 */
+	if (xnthread_test_state(sched->curr, XNROOT)) {
+		XENO_WARN_ON_ONCE(COBALT, 1);
+		return;
+	}
+
+	throttle_charge(t, now);
+
+	if (t->used >= throttle_runtime)
+		throttle_start(sched, now);
+	else	/* Window over with budget left, charge the next one. */
+		throttle_arm_budget_timer(t, now);
+}
+
+/* Window timer handler: lift the throttle. */
+static void throttle_window_timeout(struct xntimer *timer)
+{
+	struct xnsched *sched = container_of(timer, struct xnsched,
+					     throttle.window_timer);
+	struct xnsched_throttle *t = &sched->throttle;
+
+	sched->lflags &= ~XNTHROTTLED;
+	trace_cobalt_sched_throttle(xnsched_cpu(sched), false);
+
+	/* Not from the clock: the timer may fire early (gravity). */
+	throttle_window_advance(t, t->window_start + throttle_period);
+
+	/*
+	 * If the window ended before the throttled thread could be
+	 * switched out, it is still running and leave_root() won't
+	 * be called for it: charge it to the new window from here.
+	 */
+	if (!xnthread_test_state(sched->curr, XNROOT))
+		throttle_arm_budget_timer(t, xnclock_read_monotonic(&nkclock));
+
+	xnsched_set_self_resched(sched);
+}
+
+void xnsched_throttle_init(struct xnsched *sched)
+{
+	struct xnsched_throttle *t = &sched->throttle;
+
+	/* The first leave_root() advances to the current window. */
+	t->window_start = 0;
+	t->used = 0;
+	t->count = 0;
+
+	xntimer_init(&t->budget_timer, &nkclock, throttle_budget_timeout,
+		     sched, XNTIMER_IGRAVITY);
+	xntimer_set_name(&t->budget_timer, "[throttle-budget]");
+	xntimer_set_priority(&t->budget_timer, XNTIMER_LOPRIO);
+	xntimer_init(&t->window_timer, &nkclock, throttle_window_timeout,
+		     sched, XNTIMER_IGRAVITY);
+	xntimer_set_name(&t->window_timer, "[throttle-window]");
+	xntimer_set_priority(&t->window_timer, XNTIMER_LOPRIO);
+}
+
+void xnsched_throttle_destroy(struct xnsched *sched)
+{
+	xntimer_destroy(&sched->throttle.budget_timer);
+	xntimer_destroy(&sched->throttle.window_timer);
+}
+
+/* nklock held, irqs off. */
+void __xnsched_throttle_leave_root(struct xnsched *sched)
+{
+	struct xnsched_throttle *t = &sched->throttle;
+	xnticks_t now = xnclock_read_monotonic(&nkclock);
+
+	if (throttle_window_over(t, now))
+		throttle_window_advance(t, now);
+
+	t->charge_from = now;
+	throttle_arm_budget_timer(t, now);
+}
+
+/* nklock held, irqs off. */
+void __xnsched_throttle_enter_root(struct xnsched *sched)
+{
+	struct xnsched_throttle *t = &sched->throttle;
+
+	xntimer_stop(&t->budget_timer);
+	throttle_charge(t, xnclock_read_monotonic(&nkclock));
+}
+
+#ifdef CONFIG_XENO_OPT_VFILE
+
+static int throttle_vfile_show(struct xnvfile_regular_iterator *it,
+			       void *data)
+{
+	struct xnsched *sched;
+	unsigned long count;
+	bool throttled;
+	int cpu;
+	spl_t s;
+
+	xnvfile_printf(it, "%-3s  %-9s  %-10s  %-10s  %s\n",
+		       "CPU", "THROTTLED", "PERIOD(us)", "BUDGET(us)", "COUNT");
+
+	for_each_realtime_cpu(cpu) {
+		sched = xnsched_struct(cpu);
+		xnlock_get_irqsave(&nklock, s);
+		throttled = sched->lflags & XNTHROTTLED;
+		count = sched->throttle.count;
+		xnlock_put_irqrestore(&nklock, s);
+
+		/* Both are zero if throttling is disabled. */
+		xnvfile_printf(it, "%3d  %-9s  %-10llu  %-10llu  %lu\n",
+			       cpu, throttled ? "yes" : "no",
+			       div_u64(throttle_period, 1000),
+			       div_u64(throttle_runtime, 1000), count);
+	}
+
+	return 0;
+}
+
+static struct xnvfile_regular_ops throttle_vfile_ops = {
+	.show = throttle_vfile_show,
+};
+
+static struct xnvfile_regular throttle_vfile = {
+	.ops = &throttle_vfile_ops,
+};
+
+int xnsched_throttle_init_proc(struct xnvfile_directory *vfroot)
+{
+	return xnvfile_init_regular("throttle", &throttle_vfile, vfroot);
+}
+
+void xnsched_throttle_cleanup_proc(void)
+{
+	xnvfile_destroy_regular(&throttle_vfile);
+}
+
+#endif /* CONFIG_XENO_OPT_VFILE */
diff --git a/kernel/cobalt/sched.c b/kernel/cobalt/sched.c
index ed99ddd0a..e0e01a222 100644
--- a/kernel/cobalt/sched.c
+++ b/kernel/cobalt/sched.c
@@ -149,6 +149,7 @@ static void watchdog_handler(struct xntimer *timer)
 
 #endif /* CONFIG_XENO_OPT_WATCHDOG */
 
+
 static void roundrobin_handler(struct xntimer *timer)
 {
 	struct xnsched *sched = container_of(timer, struct xnsched, rrbtimer);
@@ -223,6 +224,7 @@ static void xnsched_init(struct xnsched *sched, int cpu)
 	xntimer_set_name(&sched->wdtimer, "[watchdog]");
 	xntimer_set_priority(&sched->wdtimer, XNTIMER_LOPRIO);
 #endif /* CONFIG_XENO_OPT_WATCHDOG */
+	xnsched_throttle_init(sched);
 }
 
 void xnsched_init_all(void)
@@ -230,6 +232,8 @@ void xnsched_init_all(void)
 	struct xnsched *sched;
 	int cpu;
 
+	xnsched_throttle_setup();
+
 	for_each_online_cpu(cpu) {
 		sched = &per_cpu(nksched, cpu);
 		xnsched_init(sched, cpu);
@@ -247,6 +251,7 @@ static void xnsched_destroy(struct xnsched *sched)
 #ifdef CONFIG_XENO_OPT_WATCHDOG
 	xntimer_destroy(&sched->wdtimer);
 #endif /* CONFIG_XENO_OPT_WATCHDOG */
+	xnsched_throttle_destroy(sched);
 }
 
 void xnsched_destroy_all(void)
@@ -297,9 +302,9 @@ struct xnthread *xnsched_pick_next(struct xnsched *sched)
 	if (!xnthread_test_state(curr, XNTHREAD_BLOCK_BITS | XNZOMBIE)) {
 		/*
 		 * Do not preempt the current thread if it holds the
-		 * scheduler lock.
+		 * scheduler lock, unless the RT budget is exhausted.
 		 */
-		if (curr->lock_count > 0) {
+		if (curr->lock_count > 0 && !xnsched_throttled(sched)) {
 			xnsched_set_self_resched(sched);
 			return curr;
 		}
@@ -314,6 +319,21 @@ struct xnthread *xnsched_pick_next(struct xnsched *sched)
 		}
 	}
 
+	/*
+	 * Hand the CPU over to Linux until the RT window ends. This
+	 * applies to every scheduling class and overrides the
+	 * scheduler lock: the holder keeps its lock count and no
+	 * other Cobalt thread can run until the window ends.
+	 */
+	if (unlikely(xnsched_throttled(sched))) {
+		/* Per-thread budgets belong to the base class. */
+		if (curr->base_class->sched_throttle)
+			curr->base_class->sched_throttle(curr);
+		thread = &sched->rootcb;
+		set_thread_running(sched, thread);
+		return thread;
+	}
+
 	/*
 	 * Find the runnable thread having the highest priority among
 	 * all scheduling classes, scanned by decreasing priority.
@@ -876,6 +896,7 @@ static inline void enter_root(struct xnthread *root)
 #ifdef CONFIG_XENO_OPT_WATCHDOG
 	xntimer_stop(&root->sched->wdtimer);
 #endif
+	xnsched_throttle_enter_root(root->sched);
 }
 
 static inline void leave_root(struct xnthread *root)
@@ -886,6 +907,7 @@ static inline void leave_root(struct xnthread *root)
 	xntimer_start(&root->sched->wdtimer, get_watchdog_timeout(),
 		      XN_INFINITE, XN_RELATIVE);
 #endif
+	xnsched_throttle_leave_root(root->sched);
 }
 
 static inline void do_lazy_user_work(struct xnthread *curr)
@@ -1412,6 +1434,10 @@ int xnsched_init_proc(void)
 	xnvfile_init_regular("affinity", &affinity_vfile, &cobalt_vfroot);
 #endif /* CONFIG_SMP */
 
+	ret = xnsched_throttle_init_proc(&sched_vfroot);
+	if (ret)
+		return ret;
+
 	return 0;
 }
 
@@ -1424,6 +1450,7 @@ void xnsched_cleanup_proc(void)
 			p->sched_cleanup_vfile(p);
 	}
 
+	xnsched_throttle_cleanup_proc();
 #ifdef CONFIG_SMP
 	xnvfile_destroy_regular(&affinity_vfile);
 #endif /* CONFIG_SMP */
-- 
2.56.1


  parent reply	other threads:[~2026-12-01 14:12 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 14:12 [RFC PATCH 0/5] RT throttling Richard Weinberger (sigma star gmbh)
2026-12-01 14:12 ` [PATCH 1/5] cobalt/trace: add cobalt_sched_throttle tracepoint Richard Weinberger (sigma star gmbh)
2026-12-01 14:12 ` Richard Weinberger (sigma star gmbh) [this message]
2026-12-01 14:12 ` [PATCH 3/5] cobalt/sched-sporadic: stop budget accounting on RT throttling Richard Weinberger (sigma star gmbh)
2026-12-01 14:12 ` [PATCH 4/5] cobalt/sched-quota: charge the running group " Richard Weinberger (sigma star gmbh)
2026-12-01 14:12 ` [PATCH 5/5] testsuite/smokey: add sched-throttle test Richard Weinberger (sigma star gmbh)
2026-12-01 15:29 ` [RFC PATCH 0/5] RT throttling Jan Kiszka
2026-12-01 15:32   ` Richard Weinberger
2026-12-01 15:36     ` Jan Kiszka
2026-12-01 15:38       ` Richard Weinberger
2026-12-01 15:40         ` Jan Kiszka

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=20261201141207.2876277-3-rw@kernel.org \
    --to=rw@kernel.org \
    --cc=richard@nod.at \
    --cc=upstream+xenomai@sigma-star.at \
    --cc=xenomai@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.