This patch introduces a minimal struct_ops for BPF-based THP adjustment. Currently, only a single BPF program can be attached. Support for multiple attachments will be added in the future. Signed-off-by: Yafang Shao <laoar.shao@xxxxxxxxx> --- include/linux/huge_mm.h | 13 +---- mm/Makefile | 3 + mm/bpf_thp.c | 120 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 12 deletions(-) create mode 100644 mm/bpf_thp.c diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index fedb5b014d9a..a75f5f902af0 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -193,18 +193,7 @@ static inline bool hugepage_global_always(void) (1<<TRANSPARENT_HUGEPAGE_FLAG); } -static inline bool hugepage_bpf_allowable(void) -{ - /* Works only for BPF mode */ - if (!(transparent_hugepage_flags & (1<<TRANSPARENT_HUGEPAGE_REQ_BPF_FLAG))) - return 0; - - /* No BPF program is attached */ - if (!(transparent_hugepage_flags & (1<<TRANSPARENT_HUGEPAGE_BPF_ATTACHED))) - return 0; - /* We will add struct ops in the future */ - return 1; -} +bool hugepage_bpf_allowable(void); static inline bool hugepaged_bpf_allowable(void) { diff --git a/mm/Makefile b/mm/Makefile index e7f6bbf8ae5f..c355f9426c93 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -99,6 +99,9 @@ obj-$(CONFIG_MIGRATION) += migrate.o obj-$(CONFIG_NUMA) += memory-tiers.o obj-$(CONFIG_DEVICE_MIGRATION) += migrate_device.o obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.o khugepaged.o +ifdef CONFIG_BPF_SYSCALL +obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += bpf_thp.o +endif obj-$(CONFIG_PAGE_COUNTER) += page_counter.o obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o diff --git a/mm/bpf_thp.c b/mm/bpf_thp.c new file mode 100644 index 000000000000..690980cdb4da --- /dev/null +++ b/mm/bpf_thp.c @@ -0,0 +1,120 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <linux/btf.h> +#include <linux/huge_mm.h> + +struct bpf_thp_ops { + /** + * @thp_bpf_allowable: Determines whether a task is permitted to + * allocate a THP when it is allocating anon memory. + * + * Return: %true if THP allocation is allowed, %false otherwise. + */ + bool (*thp_bpf_allowable)(void); +}; + +static struct bpf_thp_ops bpf_thp; + +bool hugepage_bpf_allowable(void) +{ + /* Works only for "bpf" mode */ + if (!(transparent_hugepage_flags & (1<<TRANSPARENT_HUGEPAGE_REQ_BPF_FLAG))) + return 0; + + /* No BPF program is attached */ + if (!(transparent_hugepage_flags & (1<<TRANSPARENT_HUGEPAGE_BPF_ATTACHED))) + return 0; + + /* BPF adjustment hook */ + if (bpf_thp.thp_bpf_allowable) + return bpf_thp.thp_bpf_allowable(); + return 0; +} + +static bool bpf_thp_ops_is_valid_access(int off, int size, + enum bpf_access_type type, + const struct bpf_prog *prog, + struct bpf_insn_access_aux *info) +{ + return bpf_tracing_btf_ctx_access(off, size, type, prog, info); +} + +static const struct bpf_func_proto * +bpf_thp_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) +{ + return bpf_base_func_proto(func_id, prog); +} + +static const struct bpf_verifier_ops thp_bpf_verifier_ops = { + .get_func_proto = bpf_thp_get_func_proto, + .is_valid_access = bpf_thp_ops_is_valid_access, +}; + +static int bpf_thp_reg(void *kdata, struct bpf_link *link) +{ + struct bpf_thp_ops *ops = kdata; + + /* TODO: add support for multiple attaches */ + if (test_and_set_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED, + &transparent_hugepage_flags)) + return -EOPNOTSUPP; + bpf_thp.thp_bpf_allowable = ops->thp_bpf_allowable; + return 0; +} + +static void bpf_thp_unreg(void *kdata, struct bpf_link *link) +{ + clear_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED, &transparent_hugepage_flags); + bpf_thp.thp_bpf_allowable = NULL; +} + +static int bpf_thp_check_member(const struct btf_type *t, + const struct btf_member *member, + const struct bpf_prog *prog) +{ + return 0; +} + +static int bpf_thp_init_member(const struct btf_type *t, + const struct btf_member *member, + void *kdata, const void *udata) +{ + return 0; +} + +static int bpf_thp_init(struct btf *btf) +{ + return 0; +} + +static bool thp_bpf_allowable(void) +{ + return 0; +} + +static struct bpf_thp_ops __bpf_thp_ops = { + .thp_bpf_allowable = thp_bpf_allowable, +}; + +static struct bpf_struct_ops bpf_bpf_thp_ops = { + .verifier_ops = &thp_bpf_verifier_ops, + .init = bpf_thp_init, + .check_member = bpf_thp_check_member, + .init_member = bpf_thp_init_member, + .reg = bpf_thp_reg, + .unreg = bpf_thp_unreg, + .name = "bpf_thp_ops", + .cfi_stubs = &__bpf_thp_ops, + .owner = THIS_MODULE, +}; + +static int __init bpf_thp_ops_init(void) +{ + int err = register_bpf_struct_ops(&bpf_bpf_thp_ops, bpf_thp_ops); + + if (err) + pr_err("bpf_thp: Failed to register struct_ops (%d)\n", err); + return err; +} +late_initcall(bpf_thp_ops_init); -- 2.43.5