From: Guan Jing guanjing6@huawei.com
hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/I9GZAQ CVE: NA
--------------------------------
Add bpf hook for 'can_migrate_task', which allow user to decide whether exit or not load_balance early.
Signed-off-by: Guan Jing guanjing6@huawei.com Signed-off-by: Hui Tang tanghui20@huawei.com --- include/linux/sched.h | 10 ++++++++++ include/linux/sched_hook_defs.h | 2 ++ kernel/sched/fair.c | 14 ++++++++++++++ scripts/bpf_helpers_doc.py | 2 ++ 4 files changed, 28 insertions(+)
diff --git a/include/linux/sched.h b/include/linux/sched.h index 0981c127f261..7ef4efd8cddb 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -2406,5 +2406,15 @@ struct sched_affine_ctx { KABI_RESERVE(3) KABI_RESERVE(4) }; + +struct sched_migrate_node { + int src_cpu; + int dst_cpu; + + KABI_RESERVE(1) + KABI_RESERVE(2) + KABI_RESERVE(3) + KABI_RESERVE(4) +}; #endif #endif diff --git a/include/linux/sched_hook_defs.h b/include/linux/sched_hook_defs.h index 818b1244a018..e2519a00aa6b 100644 --- a/include/linux/sched_hook_defs.h +++ b/include/linux/sched_hook_defs.h @@ -10,3 +10,5 @@ BPF_SCHED_HOOK(void, (void) 0, cfs_dequeue_task, struct rq *rq, struct task_stru BPF_SCHED_HOOK(int, -1, cfs_select_rq, struct sched_migrate_ctx *ctx) BPF_SCHED_HOOK(int, -1, cfs_wake_affine, struct sched_affine_ctx *ctx) BPF_SCHED_HOOK(int, -1, cfs_select_rq_exit, struct sched_migrate_ctx *ctx) +BPF_SCHED_HOOK(int, -1, cfs_can_migrate_task, struct task_struct *p, + struct sched_migrate_node *migrate_node) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index df3594b99c04..c6504f2c582c 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -9486,9 +9486,23 @@ static int can_migrate_task(struct task_struct *p, struct lb_env *env) { int tsk_cache_hot; +#ifdef CONFIG_BPF_SCHED + struct sched_migrate_node migrate_node; + int ret; +#endif
lockdep_assert_rq_held(env->src_rq);
+#ifdef CONFIG_BPF_SCHED + if (bpf_sched_enabled()) { + migrate_node.src_cpu = env->src_cpu; + migrate_node.dst_cpu = env->dst_cpu; + ret = bpf_sched_cfs_can_migrate_task(p, &migrate_node); + if (ret > 0) + return ret - 1; + } +#endif + /* * We do not migrate tasks that are: * 1) throttled_lb_pair, or diff --git a/scripts/bpf_helpers_doc.py b/scripts/bpf_helpers_doc.py index fc51d6f0d447..6001bcf66d5e 100755 --- a/scripts/bpf_helpers_doc.py +++ b/scripts/bpf_helpers_doc.py @@ -444,6 +444,7 @@ class PrinterHelpers(Printer): 'struct cpumask_op_args', 'struct sched_migrate_ctx', 'struct sched_affine_ctx', + 'struct sched_migrate_node', ] known_types = { '...', @@ -496,6 +497,7 @@ class PrinterHelpers(Printer): 'struct cpumask_op_args', 'struct sched_migrate_ctx', 'struct sched_affine_ctx', + 'struct sched_migrate_node', } mapped_types = { 'u8': '__u8',