[PATCH OLK-5.10 V1 0/2] sched-ebpf-add-exec

sched-ebpf-add-exec Cheng Yu (2): sched/ebpf: Add bpf_sched_cfs_exec_set_cpumask hook sched/ebpf: Add helper to set prefer cpumask for the task fs/exec.c | 7 +++++++ include/linux/sched_hook_defs.h | 1 + include/uapi/linux/bpf.h | 7 +++++++ kernel/bpf/helpers.c | 3 +++ kernel/sched/bpf_sched.c | 37 +++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 7 +++++++ tools/lib/bpf/libbpf_sched.h | 6 ++++++ 7 files changed, 68 insertions(+) -- 2.25.1

hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/ICFKYV -------------------------------- Add bpf_sched_cfs_exec_set_cpumask hook in exec_binprm, which allows setting cpumask for the process. Signed-off-by: Cheng Yu <serein.chengyu@huawei.com> --- fs/exec.c | 7 +++++++ include/linux/sched_hook_defs.h | 1 + 2 files changed, 8 insertions(+) diff --git a/fs/exec.c b/fs/exec.c index e328d435be64..4d01aebf5500 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -77,6 +77,7 @@ #include "internal.h" #include <trace/events/sched.h> +#include <linux/bpf_sched.h> static int bprm_creds_from_file(struct linux_binprm *bprm); @@ -1823,6 +1824,12 @@ static int exec_binprm(struct linux_binprm *bprm) trace_sched_process_exec(current, old_pid, bprm); ptrace_event(PTRACE_EVENT_EXEC, old_vpid); proc_exec_connector(current); + +#ifdef CONFIG_BPF_SCHED + if (bpf_sched_enabled()) + bpf_sched_cfs_exec_set_cpumask(current); +#endif + return 0; } diff --git a/include/linux/sched_hook_defs.h b/include/linux/sched_hook_defs.h index de6451930d8a..34eac1ffca2d 100644 --- a/include/linux/sched_hook_defs.h +++ b/include/linux/sched_hook_defs.h @@ -15,3 +15,4 @@ BPF_SCHED_HOOK(int, -1, cfs_can_migrate_task, struct task_struct *p, BPF_SCHED_HOOK(void, (void) 0, cfs_change_preferred_node, struct sched_preferred_node_ctx *ctx) BPF_SCHED_HOOK(int, -1, cfs_preferred_nid_init, struct task_struct *tsk) +BPF_SCHED_HOOK(int, -1, cfs_exec_set_cpumask, struct task_struct *tsk) -- 2.25.1

hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/ICFKYV -------------------------------- Add libbpf_sched_set_task_prefer_cpumask helper, which allows setting preferred cpumask for the task. Signed-off-by: Cheng Yu <serein.chengyu@huawei.com> --- include/uapi/linux/bpf.h | 7 +++++++ kernel/bpf/helpers.c | 3 +++ kernel/sched/bpf_sched.c | 37 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 7 +++++++ tools/lib/bpf/libbpf_sched.h | 6 ++++++ 5 files changed, 60 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index f4ebb476cf03..ab746f19f624 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -3934,6 +3934,12 @@ union bpf_attr { * update network's relationship to sched subsystem. * Return * 0 on success, or a negative error in case of failure. + * + * int bpf_sched_set_task_prefer_cpumask(struct task_struct *tsk, struct cpumask *mask, int len) + * Description + * set prefer cpumask for the task. + * Return + * 0 on success, or a negative error in case of failure. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -4111,6 +4117,7 @@ union bpf_attr { FN(sched_set_curr_preferred_node),\ FN(get_node_stats), \ FN(sched_net_rship_submit), \ + FN(sched_set_task_prefer_cpumask), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 1fbda5c87a29..72be3af042cb 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -664,6 +664,7 @@ const struct bpf_func_proto bpf_sched_tg_tag_of_proto __weak; const struct bpf_func_proto bpf_sched_task_tag_of_proto __weak; const struct bpf_func_proto bpf_sched_set_tg_tag_proto __weak; const struct bpf_func_proto bpf_sched_set_task_tag_proto __weak; +const struct bpf_func_proto bpf_sched_set_task_prefer_cpumask_proto __weak; const struct bpf_func_proto * bpf_base_func_proto(enum bpf_func_id func_id) @@ -711,6 +712,8 @@ bpf_base_func_proto(enum bpf_func_id func_id) return &bpf_sched_tg_tag_of_proto; case BPF_FUNC_sched_task_tag_of: return &bpf_sched_task_tag_of_proto; + case BPF_FUNC_sched_set_task_prefer_cpumask: + return &bpf_sched_set_task_prefer_cpumask_proto; default: break; } diff --git a/kernel/sched/bpf_sched.c b/kernel/sched/bpf_sched.c index 3e14d1fa911e..cdb83e261eb9 100644 --- a/kernel/sched/bpf_sched.c +++ b/kernel/sched/bpf_sched.c @@ -414,6 +414,43 @@ const struct bpf_func_proto bpf_sched_set_curr_preferred_node_proto = { }; #endif +#ifdef CONFIG_QOS_SCHED_DYNAMIC_AFFINITY +static inline int +set_task_prefer_cpumask(struct task_struct *tsk, struct cpumask *mask, int len) +{ + if (!tsk || !mask || sizeof(*mask) != len) + return -EINVAL; + + if (set_prefer_cpus_ptr(tsk, mask)) + return -EINVAL; + + return 0; +} +#else +static inline int +set_task_prefer_cpumask(struct task_struct *tsk, struct cpumask *mask, int len) +{ + return -EINVAL; +} +#endif + +BPF_CALL_3(bpf_sched_set_task_prefer_cpumask, + struct task_struct *, tsk, struct cpumask *, mask, int, len) +{ + return set_task_prefer_cpumask(tsk, mask, len); +} + +const struct bpf_func_proto bpf_sched_set_task_prefer_cpumask_proto = { + .func = bpf_sched_set_task_prefer_cpumask, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = PTR_MAYBE_NULL | ARG_PTR_TO_BTF_ID, + .arg1_btf_id = &btf_sched_task_ids[0], + .arg2_type = PTR_MAYBE_NULL | ARG_PTR_TO_MEM | MEM_RDONLY, + .arg3_type = ARG_CONST_SIZE, +}; + + static const struct bpf_func_proto * bpf_sched_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) { diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 120d4b1a3323..77317ffb64e2 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -3934,6 +3934,12 @@ union bpf_attr { * update network's relationship to sched subsystem. * Return * 0 on success, or a negative error in case of failure. + * + * int bpf_sched_set_task_prefer_cpumask(struct task_struct *tsk, struct cpumask *mask, int len) + * Description + * set prefer cpumask for the task. + * Return + * 0 on success, or a negative error in case of failure. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -4111,6 +4117,7 @@ union bpf_attr { FN(sched_set_curr_preferred_node),\ FN(get_node_stats), \ FN(sched_net_rship_submit), \ + FN(sched_set_task_prefer_cpumask), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper diff --git a/tools/lib/bpf/libbpf_sched.h b/tools/lib/bpf/libbpf_sched.h index 3e9b41788637..e84268399d10 100644 --- a/tools/lib/bpf/libbpf_sched.h +++ b/tools/lib/bpf/libbpf_sched.h @@ -665,4 +665,10 @@ libbpf_mem_preferred_nid(struct task_struct *tsk, nodemask_t *preferred_node) *preferred_node = getVal(stats->mm.comm.preferred_node); return 0; } + +static __always_inline int +libbpf_sched_set_task_prefer_cpumask(struct task_struct *tsk, struct cpumask *mask, int len) +{ + return bpf_sched_set_task_prefer_cpumask(tsk, mask, len); +} #endif -- 2.25.1

反馈: 您发送到kernel@openeuler.org的补丁/补丁集,已成功转换为PR! PR链接地址: https://gitee.com/openeuler/kernel/pulls/16736 邮件列表地址:https://mailweb.openeuler.org/archives/list/kernel@openeuler.org/message/QXW... FeedBack: The patch(es) which you have sent to kernel@openeuler.org mailing list has been converted to a pull request successfully! Pull request link: https://gitee.com/openeuler/kernel/pulls/16736 Mailing list address: https://mailweb.openeuler.org/archives/list/kernel@openeuler.org/message/QXW...
participants (2)
-
Cheng Yu
-
patchwork bot