From: Alexei Starovoitov LLVM replaces a comparison or a copy of memory with a call to memcmp(), memcpy() or memmove(). BPF has none of these functions. Rust progs keep all their memory in arena. Add two kfuncs that take arena pointers: int bpf_arena_memcmp(const void *a__arena, const void *b__arena, u64 size); u64 bpf_arena_memcpy(void *dst__arena, const void *src__arena, u64 size); An arena pointer can point to a page that is not allocated. When the kernel accesses such a page, the fault handler maps a scratch page there, and the access continues. Thus a plain memcmp() or memmove() would compare or copy the scratch page, and the prog would not see the error. Walk the page tables before the access, and stop at the first page that is not allocated. Report this page to the BPF_STDERR stream of the prog, the same as an access of the prog to this page. Then the fault handler is necessary only for a page that is freed after the walk. The kfuncs can run in NMI. Thus one call processes 64 Kbyte at most, and the prog copies more in a loop. The verifier rejects a call if size is always larger. kfunc return value ------------------ --------------------------------------------------- bpf_arena_memcmp() 0 or the difference of two bytes, as memcmp() does. -E2BIG if it stopped early and found no difference. bpf_arena_memcpy() The number of bytes that it copied. The areas can overlap. The cost of a call on x86-64 (Zen 5) with hot caches: copy of 1 to 64 bytes in one page 12.0 nsec compare of 1 to 64 bytes in one page up to 12.3 nsec the two walks of the page tables, included 5.2 to 5.5 nsec the same copy with inline loads and stores less than 2 nsec 64 Kbyte 0.58 to 2.0 usec Inline code is faster up to 1024 bytes. Thus it is better to keep small copies inline. Signed-off-by: Alexei Starovoitov --- Documentation/bpf/kfuncs.rst | 10 ++++ include/linux/bpf.h | 2 + kernel/bpf/arena.c | 91 ++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 14 ++++++ 4 files changed, 117 insertions(+) diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst index 73578a4c3b1f..47d3bed73b83 100644 --- a/Documentation/bpf/kfuncs.rst +++ b/Documentation/bpf/kfuncs.rst @@ -965,3 +965,13 @@ the verifier. bpf_cgroup_ancestor() can be used as follows: BPF provides a set of kfuncs that can be used to query, allocate, mutate, and destroy struct cpumask * objects. Please refer to :ref:`cpumasks-header-label` for more details. + +4.4 Arena kfuncs +---------------- + +Compilers turn copies and comparisons of memory into calls to memcpy(), +memmove() and memcmp(). Programs that keep all of their memory in arena, +like the ones written in Rust, call the following kfuncs instead: + +.. kernel-doc:: kernel/bpf/arena.c + :identifiers: bpf_arena_memcmp bpf_arena_memcpy diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 4ca06a217055..d62dc3f7f148 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -698,6 +698,8 @@ int bpf_arena_stack_init(struct bpf_arena *arena); void __percpu *bpf_arena_stack_percpu(struct bpf_arena *arena); u64 bpf_arena_stack_get(struct bpf_prog_aux *aux, u64 busy); void bpf_arena_stack_put(struct bpf_prog_aux *aux, u32 base); +/* bpf_arena_memcmp() and bpf_arena_memcpy() work on that many bytes per call at most */ +#define BPF_ARENA_MEM_MAX SZ_64K u64 bpf_arena_map_kern_vm_start(struct bpf_map *map); struct bpf_map *bpf_prog_arena(struct bpf_prog *prog); int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size); diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c index 8b99b3b6fa02..c6c13dfa1102 100644 --- a/kernel/bpf/arena.c +++ b/kernel/bpf/arena.c @@ -47,6 +47,8 @@ #define KERN_VM_SZ (SZ_4G + GUARD_SZ) static void arena_free_pages(struct bpf_arena *arena, long uaddr, long page_cnt, bool sleepable); +static void __bpf_prog_report_arena_violation(struct bpf_prog *prog, bool write, + unsigned long addr, unsigned long fault_ip); struct bpf_arena { struct bpf_map map; @@ -1296,12 +1298,101 @@ __bpf_kfunc int bpf_arena_reserve_pages(void *p__map, void *ptr__ign, u32 page_c return arena_reserve_pages(arena, (long)ptr__ign, page_cnt); } + +/* + * The number of bytes at @ptr, @size at most, that are in pages of arena + * that are allocated. @ptr is what JIT made of __arena argument. + */ +static u64 arena_span(struct bpf_arena *arena, const void *ptr, u64 size) +{ + unsigned long start = (unsigned long)ptr, end; + struct page *page; + + size = min(size, SZ_4G - (start - bpf_arena_get_kern_vm_start(arena))); + for (end = start; end < start + size; end = (end | ~PAGE_MASK) + 1) { + page = vmalloc_to_page((void *)end); + if (!page || page == arena->scratch_page) + break; + } + return min(size, end - start); +} + +/* + * @size bytes at @a and at @b were asked for. Returns what there is, + * BPF_ARENA_MEM_MAX at most, and reports the page that is missing. + */ +static u64 arena_span2(struct bpf_prog_aux *aux, const void *a, const void *b, u64 size, + bool write) +{ + const void *bad; + u64 span; + + size = min_t(u64, size, BPF_ARENA_MEM_MAX); + span = min(arena_span(aux->arena, a, size), arena_span(aux->arena, b, size)); + if (span < size) { + bad = arena_span(aux->arena, a, size) == span ? a : b; + __bpf_prog_report_arena_violation(aux->prog, write && bad == a, + (unsigned long)bad + span - + bpf_arena_get_kern_vm_start(aux->arena), 0); + } + return span; +} + +/** + * bpf_arena_memcmp() - Compare two areas of arena + * @a__arena: One area + * @b__arena: Another area + * @size: The number of bytes to compare, BPF_ARENA_MEM_MAX at most + * @aux: Implicit argument, set by the verifier + * + * The comparison stops at the first page of either area that is not + * allocated. It's reported to the stream of the program as an access to + * that page. + * + * Return: 0 when the areas are equal. The difference between the first two + * bytes that differ, like memcmp() does, when they are not. -E2BIG when + * the areas are equal as far as they were compared and that is less than + * @size. + */ +__bpf_kfunc int bpf_arena_memcmp(const void *a__arena, const void *b__arena, u64 size, + struct bpf_prog_aux *aux) +{ + u64 span = arena_span2(aux, a__arena, b__arena, size, false); + int ret = memcmp(a__arena, b__arena, span); + + return ret || span == size ? ret : -E2BIG; +} + +/** + * bpf_arena_memcpy() - Copy one area of arena to another + * @dst__arena: Where to copy to + * @src__arena: Where to copy from + * @size: The number of bytes to copy, BPF_ARENA_MEM_MAX at most + * @aux: Implicit argument, set by the verifier + * + * The areas may overlap. The copy stops at the first page of either area + * that is not allocated. It's reported to the stream of the program as + * an access to that page. + * + * Return: The number of bytes copied. The program copies more than + * BPF_ARENA_MEM_MAX in a loop. + */ +__bpf_kfunc u64 bpf_arena_memcpy(void *dst__arena, const void *src__arena, u64 size, + struct bpf_prog_aux *aux) +{ + u64 span = arena_span2(aux, dst__arena, src__arena, size, true); + + memmove(dst__arena, src__arena, span); + return span; +} __bpf_kfunc_end_defs(); BTF_KFUNCS_START(arena_kfuncs) BTF_ID_FLAGS(func, bpf_arena_alloc_pages, KF_ARENA_RET | KF_ARENA_ARG2 | KF_SPINLOCK_SAFE) BTF_ID_FLAGS(func, bpf_arena_free_pages, KF_ARENA_ARG2 | KF_SPINLOCK_SAFE) BTF_ID_FLAGS(func, bpf_arena_reserve_pages, KF_ARENA_ARG2 | KF_SPINLOCK_SAFE) +BTF_ID_FLAGS(func, bpf_arena_memcmp, KF_IMPLICIT_ARGS) +BTF_ID_FLAGS(func, bpf_arena_memcpy, KF_IMPLICIT_ARGS) BTF_KFUNCS_END(arena_kfuncs) static const struct btf_kfunc_id_set common_kfunc_set = { diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 92b563a9b158..fb80bc4d2d01 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -13719,6 +13719,8 @@ enum special_kfunc_type { KF_bpf_call_rcu_tasks_trace, KF_bpf_arena_alloc_pages, KF_bpf_arena_free_pages, + KF_bpf_arena_memcmp, + KF_bpf_arena_memcpy, KF_bpf_session_is_return, }; @@ -13812,9 +13814,13 @@ BTF_ID(func, bpf_call_rcu_tasks_trace) #if defined(CONFIG_MMU) && defined(CONFIG_64BIT) BTF_ID(func, bpf_arena_alloc_pages) BTF_ID(func, bpf_arena_free_pages) +BTF_ID(func, bpf_arena_memcmp) +BTF_ID(func, bpf_arena_memcpy) #else BTF_ID_UNUSED BTF_ID_UNUSED +BTF_ID_UNUSED +BTF_ID_UNUSED #endif #ifdef CONFIG_BPF_EVENTS BTF_ID(func, bpf_session_is_return) @@ -15551,6 +15557,14 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, if (err < 0) return err; + if ((meta.func_id == special_kfunc_list[KF_bpf_arena_memcmp] || + meta.func_id == special_kfunc_list[KF_bpf_arena_memcpy]) && + reg_umin(®s[BPF_REG_3]) > BPF_ARENA_MEM_MAX) { + verbose(env, "R3 is more than %u bytes that %s() takes per call\n", + BPF_ARENA_MEM_MAX, func_name); + return -EINVAL; + } + err = mark_raw_stack(env, &meta, insn_idx); if (err) return err; -- 2.55.0