
hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/ICA1GK -------------------------------- Implement the bpf prog for the 'cpu_online' interface. Signed-off-by: GONG Ruiqi <gongruiqi1@huawei.com> --- kernel/bpf-rvi/generic_single_iter.c | 1 + samples/bpf/Makefile | 1 + samples/bpf/bpf_rvi_cpu_online.bpf.c | 55 ++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 samples/bpf/bpf_rvi_cpu_online.bpf.c diff --git a/kernel/bpf-rvi/generic_single_iter.c b/kernel/bpf-rvi/generic_single_iter.c index 3c85304cf4e6..c8b462427366 100644 --- a/kernel/bpf-rvi/generic_single_iter.c +++ b/kernel/bpf-rvi/generic_single_iter.c @@ -49,6 +49,7 @@ static const struct seq_operations generic_single_seq_ops = { /* * Users of "generic_single" iter type: + * - cpu_online */ DEFINE_BPF_ITER_FUNC(generic_single, struct bpf_iter_meta *meta) diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index 5470187ce0b9..7627f996b5e5 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile @@ -152,6 +152,7 @@ always-y += hbm_edt_kern.o ifeq ($(ARCH), x86) always-$(CONFIG_BPF_RVI) += bpf_rvi_cpuinfo_x86.bpf.o endif +always-$(CONFIG_BPF_RVI) += bpf_rvi_cpu_online.bpf.o ifeq ($(ARCH), arm) # Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux diff --git a/samples/bpf/bpf_rvi_cpu_online.bpf.c b/samples/bpf/bpf_rvi_cpu_online.bpf.c new file mode 100644 index 000000000000..da031f7e5a7b --- /dev/null +++ b/samples/bpf/bpf_rvi_cpu_online.bpf.c @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Huawei Technologies Co., Ltd */ +#include <vmlinux.h> +#include <bpf/bpf_core_read.h> +#include <bpf/bpf_helpers.h> + +void bpf_task_release(struct task_struct *p) __ksym; +struct task_struct *bpf_current_level1_reaper(void) __ksym; +struct cpuset *bpf_cpuset_from_task(struct task_struct *p) __ksym; +unsigned int bpf_cpumask_weight(struct cpumask *pmask) __ksym; + +char _license[] SEC("license") = "GPL"; + + +#define RET_OK 0 +#define RET_FAIL 1 +#define RET_SKIP -1 + +static int task_effective_cpus_num(struct task_struct *reaper) +{ + struct cpuset *cpuset; + + cpuset = bpf_cpuset_from_task(reaper); + if (!cpuset) + return -1; + + return bpf_cpumask_weight(cpuset->effective_cpus); +} + +SEC("iter/generic_single") +s64 dump_cpu_online(struct bpf_iter__generic_single *ctx) +{ + struct seq_file *m = ctx->meta->seq; + struct task_struct *reaper; + int ncpus; + int ret = RET_OK; + + reaper = bpf_current_level1_reaper(); + if (!reaper) + return RET_FAIL; + ncpus = task_effective_cpus_num(reaper); + if (ncpus == -1) { + ret = RET_FAIL; + goto err; + } + + if (ncpus > 1) + BPF_SEQ_PRINTF(m, "0-%u\n", ncpus - 1); + else + BPF_SEQ_PRINTF(m, "0\n"); + +err: + bpf_task_release(reaper); + return ret; +} -- 2.25.1