
hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/ICA1GK -------------------------------- This kfunc is to acquire the memory & swap-related parts of sysinfo. Signed-off-by: GONG Ruiqi <gongruiqi1@huawei.com> --- kernel/bpf-rvi/common_kfuncs.c | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/kernel/bpf-rvi/common_kfuncs.c b/kernel/bpf-rvi/common_kfuncs.c index 22ac2c69de1a..28182d1ccce1 100644 --- a/kernel/bpf-rvi/common_kfuncs.c +++ b/kernel/bpf-rvi/common_kfuncs.c @@ -5,6 +5,9 @@ #include <linux/idr.h> #include <linux/pid_namespace.h> #include <linux/sched.h> +#include <linux/mm.h> +#include <linux/swap.h> +#include <linux/types.h> #include <linux/btf_ids.h> #include <linux/bpf.h> @@ -45,11 +48,46 @@ __bpf_kfunc u32 bpf_pidns_last_pid(struct pid_namespace *ns) return idr_get_cursor(&ns->idr) - 1; } +/* + * Swaps related kfuncs + */ + +/* + * no padding member "_f" compared to struct sysinfo, because sizeof(_f) + * maybe zero and not supported by bpf_verifier. + */ +struct bpf_sysinfo { + __kernel_long_t uptime; /* Seconds since boot */ + __kernel_ulong_t loads[3]; /* 1, 5, and 15 minute load averages */ + __kernel_ulong_t totalram; /* Total usable main memory size */ + __kernel_ulong_t freeram; /* Available memory size */ + __kernel_ulong_t sharedram; /* Amount of shared memory */ + __kernel_ulong_t bufferram; /* Memory used by buffers */ + __kernel_ulong_t totalswap; /* Total swap space size */ + __kernel_ulong_t freeswap; /* swap space still available */ + __u16 procs; /* Number of current processes */ + __u16 pad; /* Explicit padding for m68k */ + __kernel_ulong_t totalhigh; /* Total high memory size */ + __kernel_ulong_t freehigh; /* Available high memory size */ + __u32 mem_unit; /* Memory unit size in bytes */ +}; + +__bpf_kfunc void bpf_si_memswinfo(struct bpf_sysinfo *bsi) +{ + struct sysinfo *si = (struct sysinfo *)bsi; + + if (si) { + si_meminfo(si); + si_swapinfo(si); + } +} + BTF_SET8_START(bpf_common_kfuncs_ids) BTF_ID_FLAGS(func, bpf_mem_cgroup_from_task, KF_RET_NULL | KF_RCU) BTF_ID_FLAGS(func, bpf_task_active_pid_ns, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_pidns_nr_tasks) BTF_ID_FLAGS(func, bpf_pidns_last_pid) +BTF_ID_FLAGS(func, bpf_si_memswinfo) BTF_SET8_END(bpf_common_kfuncs_ids) static const struct btf_kfunc_id_set bpf_common_kfuncs_set = { -- 2.25.1