Skip to contentSkip to conversation

sched_ext: Add scx_bpf_task_add_dsq_vtime()

PATCH In review Cheng-Yang Chou 2 participants 3 messages Mar 11

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched_ext: Add scx_bpf_task_add_dsq_vtime()

Writing directly to p->scx.slice and p->scx.dsq_vtime from a BPF scheduler is deprecated. Currently, if schedulers want to add a virtual time delta, they are forced to do it directly via p->scx.dsq_vtime += vtime_delta.

Unlike the existing scx_bpf_task_set_* helpers, this direct modification bypasses the scx_task_on_sched() authority check, potentially modifying tasks the calling scheduler does not control.

Introduce scx_bpf_task_add_dsq_vtime() to provide a safe and approved API for schedulers to easily apply a delta to p->scx.dsq_vtime while ensuring task authority is properly verified.

Additionally, update the deprecation warning in bpf_scx_btf_struct_access() to suggest using the new helper.

---
 kernel/sched/ext.c                       | 27 +++++++++++++++++++++++-
 tools/sched_ext/include/scx/compat.bpf.h |  9 ++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)
kernel/sched/ext.c +26 -1
index e7ab3647e35f..26a85ec58d31 100644
@@ -6963,7 +6963,7 @@ static int bpf_scx_btf_struct_access(struct bpf_verifier_log *log,
 		     off + size <= offsetofend(struct task_struct, scx.slice)) ||
 		    (off >= offsetof(struct task_struct, scx.dsq_vtime) &&
 		     off + size <= offsetofend(struct task_struct, scx.dsq_vtime))) {
-			pr_warn("sched_ext: Writing directly to p->scx.slice/dsq_vtime is deprecated, use scx_bpf_task_set_slice/dsq_vtime()");
+			pr_warn("sched_ext: Writing directly to p->scx.slice/dsq_vtime is deprecated, use scx_bpf_task_set_slice/dsq_vtime() or scx_bpf_task_add_dsq_vtime()");
 			return SCALAR_VALUE;
 		}
 
@@ -8281,6 +8281,30 @@ __bpf_kfunc bool scx_bpf_task_set_dsq_vtime(struct task_struct *p, u64 vtime,
 	return true;
 }
 
+/**
+ * scx_bpf_task_add_dsq_vtime - Add to task's virtual time for DSQ ordering
+ * @p: task of interest
+ * @vtime_delta: virtual time delta to add
+ * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
+ *
+ * Add @vtime_delta to @p's virtual time. Returns %true on success, %false if the
+ * calling scheduler doesn't have authority over @p.
+ */
+
+__bpf_kfunc bool scx_bpf_task_add_dsq_vtime(struct task_struct *p, s64 vtime_delta,
+					    const struct bpf_prog_aux *aux)
+{
+	struct scx_sched *sch;
+
+	guard(rcu)();
+	sch = scx_prog_sched(aux);
+	if (unlikely(!scx_task_on_sched(sch, p)))
+		return false;
+
+	p->scx.dsq_vtime += vtime_delta;
+	return true;
+}
+
 static void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags)
 {
 	struct rq *this_rq;
@@ -9167,6 +9191,7 @@ __bpf_kfunc_end_defs();
 BTF_KFUNCS_START(scx_kfunc_ids_any)
 BTF_ID_FLAGS(func, scx_bpf_task_set_slice, KF_IMPLICIT_ARGS | KF_RCU);
 BTF_ID_FLAGS(func, scx_bpf_task_set_dsq_vtime, KF_IMPLICIT_ARGS | KF_RCU);
+BTF_ID_FLAGS(func, scx_bpf_task_add_dsq_vtime, KF_IMPLICIT_ARGS | KF_RCU);
 BTF_ID_FLAGS(func, scx_bpf_kick_cpu, KF_IMPLICIT_ARGS)
 BTF_ID_FLAGS(func, scx_bpf_dsq_nr_queued)
 BTF_ID_FLAGS(func, scx_bpf_destroy_dsq)
tools/sched_ext/include/scx/compat.bpf.h +9 -0
index 704728864d83..06f298f6909f 100644
@@ -343,6 +343,7 @@ scx_bpf_dsq_insert(struct task_struct *p, u64 dsq_id, u64 slice, u64 enq_flags)
  */
 bool scx_bpf_task_set_slice___new(struct task_struct *p, u64 slice) __ksym __weak;
 bool scx_bpf_task_set_dsq_vtime___new(struct task_struct *p, u64 vtime) __ksym __weak;
+bool scx_bpf_task_add_dsq_vtime___new(struct task_struct *p, u64 vtime_delta) __ksym __weak;
 
 static inline void scx_bpf_task_set_slice(struct task_struct *p, u64 slice)
 {
@@ -360,6 +361,14 @@ static inline void scx_bpf_task_set_dsq_vtime(struct task_struct *p, u64 vtime)
 		p->scx.dsq_vtime = vtime;
 }
 
+static inline void scx_bpf_task_add_dsq_vtime(struct task_struct *p, u64 vtime_delta)
+{
+	if (bpf_ksym_exists(scx_bpf_task_add_dsq_vtime___new))
+		scx_bpf_task_add_dsq_vtime___new(p, vtime_delta);
+	else
+		p->scx.dsq_vtime += vtime_delta;
+}
+
 /*
  * v6.19: The new void variant can be called from anywhere while the older v1
  * variant can only be called from ops.cpu_release(). The double ___ prefixes on
Signature · 1 line
2.48.1
reviewer
#1 lore raw
Quoted 9 lines · Cheng-Yang Chou

Writing directly to p->scx.slice and p->scx.dsq_vtime from a BPF scheduler is deprecated. Currently, if schedulers want to add a virtual time delta, they are forced to do it directly via p->scx.dsq_vtime += vtime_delta.

Unlike the existing scx_bpf_task_set_* helpers, this direct modification bypasses the scx_task_on_sched() authority check, potentially modifying tasks the calling scheduler does not control.

Quoted 8 lines

Introduce scx_bpf_task_add_dsq_vtime() to provide a safe and approved API for schedulers to easily apply a delta to p->scx.dsq_vtime while ensuring task authority is properly verified.

Additionally, update the deprecation warning in bpf_scx_btf_struct_access() to suggest using the new helper.

Hm, I'm not sure we need a dedicated kfunc for this. Sub-schedulers can read p->scx.slice and p->scx.dsq_vtime from any task, they just can't modify those values for the tasks they don't own.

So they could read the current value, apply the delta, and then use scx_bpf_task_set_*() to update it.

Am I missing something here?

Thanks,
-Andrea

Quoted 87 lines
---
 kernel/sched/ext.c                       | 27 +++++++++++++++++++++++-
 tools/sched_ext/include/scx/compat.bpf.h |  9 ++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)
kernel/sched/ext.c +26 -1
index e7ab3647e35f..26a85ec58d31 100644
@@ -6963,7 +6963,7 @@ static int bpf_scx_btf_struct_access(struct bpf_verifier_log *log,
 		     off + size <= offsetofend(struct task_struct, scx.slice)) ||
 		    (off >= offsetof(struct task_struct, scx.dsq_vtime) &&
 		     off + size <= offsetofend(struct task_struct, scx.dsq_vtime))) {
-			pr_warn("sched_ext: Writing directly to p->scx.slice/dsq_vtime is deprecated, use scx_bpf_task_set_slice/dsq_vtime()");
+			pr_warn("sched_ext: Writing directly to p->scx.slice/dsq_vtime is deprecated, use scx_bpf_task_set_slice/dsq_vtime() or scx_bpf_task_add_dsq_vtime()");
 			return SCALAR_VALUE;
 		}
 
@@ -8281,6 +8281,30 @@ __bpf_kfunc bool scx_bpf_task_set_dsq_vtime(struct task_struct *p, u64 vtime,
 	return true;
 }
 
+/**
+ * scx_bpf_task_add_dsq_vtime - Add to task's virtual time for DSQ ordering
+ * @p: task of interest
+ * @vtime_delta: virtual time delta to add
+ * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
+ *
+ * Add @vtime_delta to @p's virtual time. Returns %true on success, %false if the
+ * calling scheduler doesn't have authority over @p.
+ */
+
+__bpf_kfunc bool scx_bpf_task_add_dsq_vtime(struct task_struct *p, s64 vtime_delta,
+					    const struct bpf_prog_aux *aux)
+{
+	struct scx_sched *sch;
+
+	guard(rcu)();
+	sch = scx_prog_sched(aux);
+	if (unlikely(!scx_task_on_sched(sch, p)))
+		return false;
+
+	p->scx.dsq_vtime += vtime_delta;
+	return true;
+}
+
 static void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags)
 {
 	struct rq *this_rq;
@@ -9167,6 +9191,7 @@ __bpf_kfunc_end_defs();
 BTF_KFUNCS_START(scx_kfunc_ids_any)
 BTF_ID_FLAGS(func, scx_bpf_task_set_slice, KF_IMPLICIT_ARGS | KF_RCU);
 BTF_ID_FLAGS(func, scx_bpf_task_set_dsq_vtime, KF_IMPLICIT_ARGS | KF_RCU);
+BTF_ID_FLAGS(func, scx_bpf_task_add_dsq_vtime, KF_IMPLICIT_ARGS | KF_RCU);
 BTF_ID_FLAGS(func, scx_bpf_kick_cpu, KF_IMPLICIT_ARGS)
 BTF_ID_FLAGS(func, scx_bpf_dsq_nr_queued)
 BTF_ID_FLAGS(func, scx_bpf_destroy_dsq)
tools/sched_ext/include/scx/compat.bpf.h +9 -0
index 704728864d83..06f298f6909f 100644
@@ -343,6 +343,7 @@ scx_bpf_dsq_insert(struct task_struct *p, u64 dsq_id, u64 slice, u64 enq_flags)
  */
 bool scx_bpf_task_set_slice___new(struct task_struct *p, u64 slice) __ksym __weak;
 bool scx_bpf_task_set_dsq_vtime___new(struct task_struct *p, u64 vtime) __ksym __weak;
+bool scx_bpf_task_add_dsq_vtime___new(struct task_struct *p, u64 vtime_delta) __ksym __weak;
 
 static inline void scx_bpf_task_set_slice(struct task_struct *p, u64 slice)
 {
@@ -360,6 +361,14 @@ static inline void scx_bpf_task_set_dsq_vtime(struct task_struct *p, u64 vtime)
 		p->scx.dsq_vtime = vtime;
 }
 
+static inline void scx_bpf_task_add_dsq_vtime(struct task_struct *p, u64 vtime_delta)
+{
+	if (bpf_ksym_exists(scx_bpf_task_add_dsq_vtime___new))
+		scx_bpf_task_add_dsq_vtime___new(p, vtime_delta);
+	else
+		p->scx.dsq_vtime += vtime_delta;
+}
+
 /*
  * v6.19: The new void variant can be called from anywhere while the older v1
  * variant can only be called from ops.cpu_release(). The double ___ prefixes on
Signature · 1 line
2.48.1
#2 lore raw

Hi Andrea,

Quoted 8 lines · Andrea Righi

Hm, I'm not sure we need a dedicated kfunc for this. Sub-schedulers can read p->scx.slice and p->scx.dsq_vtime from any task, they just can't modify those values for the tasks they don't own.

So they could read the current value, apply the delta, and then use scx_bpf_task_set_*() to update it.

Am I missing something here?

Oh I see.

My original thought was just to make the API a bit more convenient. Instead of writing:

    scx_bpf_task_set_dsq_vtime(p, p->scx.dsq_vtime + delta);

Developers could simply write:

    scx_bpf_task_add_dsq_vtime(p, delta);

But as you pointed out, the set() helper is already a straightforward one-liner and functionally identical. The authority check is also properly handled.

I am totally fine with dropping this patch.

Signature · 2 lines
Thanks,
Cheng-Yang