
hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/ICA1GK -------------------------------- Implement the bpf prog for the 'partitions' interface. Signed-off-by: GONG Ruiqi <gongruiqi1@huawei.com> --- samples/bpf/Makefile | 1 + samples/bpf/bpf_rvi_partitions.bpf.c | 42 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 samples/bpf/bpf_rvi_partitions.bpf.c diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index 91d73753c7fb..bc85adcb714f 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile @@ -157,6 +157,7 @@ always-$(CONFIG_BPF_RVI) += bpf_rvi_cpuinfo_arm64.bpf.o endif always-$(CONFIG_BPF_RVI) += bpf_rvi_cpu_online.bpf.o always-$(CONFIG_BPF_RVI) += bpf_rvi_diskstats.bpf.o +always-$(CONFIG_BPF_RVI) += bpf_rvi_partitions.bpf.o ifeq ($(ARCH), arm) # Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux diff --git a/samples/bpf/bpf_rvi_partitions.bpf.c b/samples/bpf/bpf_rvi_partitions.bpf.c new file mode 100644 index 000000000000..56afe576a789 --- /dev/null +++ b/samples/bpf/bpf_rvi_partitions.bpf.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Huawei Technologies Co., Ltd */ +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> + +char _license[] SEC("license") = "GPL"; + +/* Copied from kdev_t.h */ +#define MINORBITS 20 +#define MINORMASK ((1U << MINORBITS) - 1) +#define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS)) +#define MINOR(dev) ((unsigned int) ((dev) & MINORMASK)) + +static inline sector_t bdev_nr_sectors(struct block_device *bdev) +{ + return bdev->bd_nr_sectors; +} + +/* Reference: show_partition() in block/genh.c */ +SEC("iter/partitions") +s64 dump_partitions(struct bpf_iter__partitions *ctx) +{ + struct seq_file *m = ctx->meta->seq; + struct block_device *part = ctx->part; + + if (!bdev_nr_sectors(part)) + return 0; + BPF_SEQ_PRINTF(m, "%4d %7d %10llu ", + MAJOR(part->bd_dev), MINOR(part->bd_dev), + bdev_nr_sectors(part) >> 1); + + /* + * Mimic %pg format of printk. + * Reference: bdev_name() in lib/vsprintf.c + */ + if (part->bd_partno) + BPF_SEQ_PRINTF(m, "%sp%d\n", part->bd_disk->disk_name, part->bd_partno); + else + BPF_SEQ_PRINTF(m, "%s\n", part->bd_disk->disk_name); + + return 0; +} -- 2.25.1