Skip to content

Commit 0366017

Browse files
committed
sched_ext: Use task_can_run_on_remote_rq() test in dispatch_to_local_dsq()
When deciding whether a task can be migrated to a CPU, dispatch_to_local_dsq() was open-coding p->cpus_allowed and scx_rq_online() tests instead of using task_can_run_on_remote_rq(). This had two problems. - It was missing is_migration_disabled() check and thus could try to migrate a task which shouldn't leading to assertion and scheduling failures. - It was testing p->cpus_ptr directly instead of using task_allowed_on_cpu() and thus failed to consider ISA compatibility. Update dispatch_to_local_dsq() to use task_can_run_on_remote_rq(): - Move scx_ops_error() triggering into task_can_run_on_remote_rq(). - When migration isn't allowed, fall back to the global DSQ instead of the source DSQ by returning DTL_INVALID. This is both simpler and an overall better behavior. Signed-off-by: Tejun Heo <[email protected]> Cc: Peter Zijlstra <[email protected]> Acked-by: David Vernet <[email protected]>
1 parent bf934be commit 0366017

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

kernel/sched/ext.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,16 +2203,30 @@ static void consume_local_task(struct rq *rq, struct scx_dispatch_q *dsq,
22032203
* - The BPF scheduler is bypassed while the rq is offline and we can always say
22042204
* no to the BPF scheduler initiated migrations while offline.
22052205
*/
2206-
static bool task_can_run_on_remote_rq(struct task_struct *p, struct rq *rq)
2206+
static bool task_can_run_on_remote_rq(struct task_struct *p, struct rq *rq,
2207+
bool trigger_error)
22072208
{
22082209
int cpu = cpu_of(rq);
22092210

2210-
if (!task_allowed_on_cpu(p, cpu))
2211+
/*
2212+
* We don't require the BPF scheduler to avoid dispatching to offline
2213+
* CPUs mostly for convenience but also because CPUs can go offline
2214+
* between scx_bpf_dispatch() calls and here. Trigger error iff the
2215+
* picked CPU is outside the allowed mask.
2216+
*/
2217+
if (!task_allowed_on_cpu(p, cpu)) {
2218+
if (trigger_error)
2219+
scx_ops_error("SCX_DSQ_LOCAL[_ON] verdict target cpu %d not allowed for %s[%d]",
2220+
cpu_of(rq), p->comm, p->pid);
22112221
return false;
2222+
}
2223+
22122224
if (unlikely(is_migration_disabled(p)))
22132225
return false;
2226+
22142227
if (!scx_rq_online(rq))
22152228
return false;
2229+
22162230
return true;
22172231
}
22182232

@@ -2240,9 +2254,8 @@ static bool consume_remote_task(struct rq *rq, struct scx_dispatch_q *dsq,
22402254
return move_task_to_local_dsq(p, 0, task_rq, rq);
22412255
}
22422256
#else /* CONFIG_SMP */
2243-
static bool task_can_run_on_remote_rq(struct task_struct *p, struct rq *rq) { return false; }
2244-
static bool consume_remote_task(struct rq *rq, struct scx_dispatch_q *dsq,
2245-
struct task_struct *p, struct rq *task_rq) { return false; }
2257+
static inline bool task_can_run_on_remote_rq(struct task_struct *p, struct rq *rq, bool trigger_error) { return false; }
2258+
static inline bool consume_remote_task(struct rq *rq, struct scx_dispatch_q *dsq, struct task_struct *p, struct rq *task_rq) { return false; }
22462259
#endif /* CONFIG_SMP */
22472260

22482261
static bool consume_dispatch_q(struct rq *rq, struct scx_dispatch_q *dsq)
@@ -2267,7 +2280,7 @@ static bool consume_dispatch_q(struct rq *rq, struct scx_dispatch_q *dsq)
22672280
return true;
22682281
}
22692282

2270-
if (task_can_run_on_remote_rq(p, rq)) {
2283+
if (task_can_run_on_remote_rq(p, rq, false)) {
22712284
if (likely(consume_remote_task(rq, dsq, p, task_rq)))
22722285
return true;
22732286
goto retry;
@@ -2330,7 +2343,7 @@ dispatch_to_local_dsq(struct rq *rq, u64 dsq_id, struct task_struct *p,
23302343
}
23312344

23322345
#ifdef CONFIG_SMP
2333-
if (cpumask_test_cpu(cpu_of(dst_rq), p->cpus_ptr)) {
2346+
if (likely(task_can_run_on_remote_rq(p, dst_rq, true))) {
23342347
bool dsp;
23352348

23362349
/*
@@ -2355,17 +2368,6 @@ dispatch_to_local_dsq(struct rq *rq, u64 dsq_id, struct task_struct *p,
23552368
raw_spin_rq_lock(src_rq);
23562369
}
23572370

2358-
/*
2359-
* We don't require the BPF scheduler to avoid dispatching to
2360-
* offline CPUs mostly for convenience but also because CPUs can
2361-
* go offline between scx_bpf_dispatch() calls and here. If @p
2362-
* is destined to an offline CPU, queue it on its current CPU
2363-
* instead, which should always be safe. As this is an allowed
2364-
* behavior, don't trigger an ops error.
2365-
*/
2366-
if (!scx_rq_online(dst_rq))
2367-
dst_rq = src_rq;
2368-
23692371
if (src_rq == dst_rq) {
23702372
/*
23712373
* As @p is staying on the same rq, there's no need to
@@ -2399,8 +2401,6 @@ dispatch_to_local_dsq(struct rq *rq, u64 dsq_id, struct task_struct *p,
23992401
}
24002402
#endif /* CONFIG_SMP */
24012403

2402-
scx_ops_error("SCX_DSQ_LOCAL[_ON] verdict target cpu %d not allowed for %s[%d]",
2403-
cpu_of(dst_rq), p->comm, p->pid);
24042404
return DTL_INVALID;
24052405
}
24062406

0 commit comments

Comments
 (0)