[PATCH 1/2] sched: Provide sched_set_batch()

From: Florian Schmaus
Date: Thu Nov 14 2024 - 16:15:15 EST


This function allows kernel threads created by modules to run under
SCHED_BATCH. Its usage may be a good option if the kernel thread is
not sensitive to scheduling latency; for example bcachefs' rebalancing
thread.

Signed-off-by: Florian Schmaus <flo@xxxxxxxxxxxx>
---
include/linux/sched.h | 1 +
kernel/sched/syscalls.c | 10 ++++++++++
2 files changed, 11 insertions(+)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index bb343136ddd0..100e6923fe39 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1862,6 +1862,7 @@ extern int sched_setscheduler_nocheck(struct task_struct *, int, const struct sc
extern void sched_set_fifo(struct task_struct *p);
extern void sched_set_fifo_low(struct task_struct *p);
extern void sched_set_normal(struct task_struct *p, int nice);
+extern void sched_set_batch(struct task_struct *p, int nice);
extern int sched_setattr(struct task_struct *, const struct sched_attr *);
extern int sched_setattr_nocheck(struct task_struct *, const struct sched_attr *);
extern struct task_struct *idle_task(int cpu);
diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c
index 24f9f90b6574..d272c0e0c508 100644
--- a/kernel/sched/syscalls.c
+++ b/kernel/sched/syscalls.c
@@ -880,6 +880,16 @@ void sched_set_normal(struct task_struct *p, int nice)
}
EXPORT_SYMBOL_GPL(sched_set_normal);

+void sched_set_batch(struct task_struct *p, int nice)
+{
+ struct sched_attr attr = {
+ .sched_policy = SCHED_BATCH,
+ .sched_nice = nice,
+ };
+ WARN_ON_ONCE(sched_setattr_nocheck(p, &attr) != 0);
+}
+EXPORT_SYMBOL_GPL(sched_set_batch);
+
static int
do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
{
--
2.45.2