Skip to content

Commit 90d643d

Browse files
Alexei StarovoitovKernel Patches Daemon
authored andcommitted
bpf: Introduce bpf_arena.
Introduce bpf_arena, which is a sparse shared memory region between the bpf program and user space. Use cases: 1. User space mmap-s bpf_arena and uses it as a traditional mmap-ed anonymous region, like memcached or any key/value storage. The bpf program implements an in-kernel accelerator. XDP prog can search for a key in bpf_arena and return a value without going to user space. 2. The bpf program builds arbitrary data structures in bpf_arena (hash tables, rb-trees, sparse arrays), while user space occasionally consumes it. 3. bpf_arena is a "heap" of memory from the bpf program's point of view. It is not shared with user space. Initially, the kernel vm_area and user vma are not populated. User space can fault in pages within the range. While servicing a page fault, bpf_arena logic will insert a new page into the kernel and user vmas. The bpf program can allocate pages from that region via bpf_arena_alloc_pages(). This kernel function will insert pages into the kernel vm_area. The subsequent fault-in from user space will populate that page into the user vma. The BPF_F_SEGV_ON_FAULT flag at arena creation time can be used to prevent fault-in from user space. In such a case, if a page is not allocated by the bpf program and not present in the kernel vm_area, the user process will segfault. This is useful for use cases 2 and 3 above. bpf_arena_alloc_pages() is similar to user space mmap(). It allocates pages either at a specific address within the arena or allocates a range with the maple tree. bpf_arena_free_pages() is analogous to munmap(), which frees pages and removes the range from the kernel vm_area and from user process vmas. bpf_arena can be used as a bpf program "heap" of up to 4GB. The memory is not shared with user space. This is use case 3. In such a case, the BPF_F_NO_USER_CONV flag is recommended. It will tell the verifier to treat the rX = bpf_arena_cast_user(rY) instruction as a 32-bit move wX = wY, which will improve bpf prog performance. Otherwise, bpf_arena_cast_user is translated by JIT to conditionally add the upper 32 bits of user vm_start (if the pointer is not NULL) to arena pointers before they are stored into memory. This way, user space sees them as valid 64-bit pointers. Diff llvm/llvm-project#79902 taught LLVM BPF backend to generate the bpf_cast_kern() instruction before dereference of the arena pointer and the bpf_cast_user() instruction when the arena pointer is formed. In a typical bpf program there will be very few bpf_cast_user(). From LLVM's point of view, arena pointers are tagged as __attribute__((address_space(1))). Hence, clang provides helpful diagnostics when pointers cross address space. Libbpf and the kernel support only address_space == 1. All other address space identifiers are reserved. rX = bpf_cast_kern(rY, addr_space) tells the verifier that rX->type = PTR_TO_ARENA. Any further operations on PTR_TO_ARENA register have to be in the 32-bit domain. The verifier will mark load/store through PTR_TO_ARENA with PROBE_MEM32. JIT will generate them as kern_vm_start + 32bit_addr memory accesses. The behavior is similar to copy_from_kernel_nofault() except that no address checks are necessary. The address is guaranteed to be in the 4GB range. If the page is not present, the destination register is zeroed on read, and the operation is ignored on write. rX = bpf_cast_user(rY, addr_space) tells the verifier that rX->type = unknown scalar. If arena->map_flags has BPF_F_NO_USER_CONV set, then the verifier converts cast_user to mov32. Otherwise, JIT will emit native code equivalent to: rX = (u32)rY; if (rX) rX |= arena->user_vm_start & ~(u64)~0U; After such conversion, the pointer becomes a valid user pointer within bpf_arena range. The user process can access data structures created in bpf_arena without any additional computations. For example, a linked list built by a bpf program can be walked natively by user space. Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent f64ae22 commit 90d643d

File tree

9 files changed

+554
-2
lines changed

9 files changed

+554
-2
lines changed

include/linux/bpf.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct perf_event;
3737
struct bpf_prog;
3838
struct bpf_prog_aux;
3939
struct bpf_map;
40+
struct bpf_arena;
4041
struct sock;
4142
struct seq_file;
4243
struct btf;
@@ -531,8 +532,8 @@ void bpf_list_head_free(const struct btf_field *field, void *list_head,
531532
struct bpf_spin_lock *spin_lock);
532533
void bpf_rb_root_free(const struct btf_field *field, void *rb_root,
533534
struct bpf_spin_lock *spin_lock);
534-
535-
535+
u64 bpf_arena_get_kern_vm_start(struct bpf_arena *arena);
536+
u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena);
536537
int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size);
537538

538539
struct bpf_offload_dev;

include/linux/bpf_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_STRUCT_OPS, bpf_struct_ops_map_ops)
132132
BPF_MAP_TYPE(BPF_MAP_TYPE_RINGBUF, ringbuf_map_ops)
133133
BPF_MAP_TYPE(BPF_MAP_TYPE_BLOOM_FILTER, bloom_filter_map_ops)
134134
BPF_MAP_TYPE(BPF_MAP_TYPE_USER_RINGBUF, user_ringbuf_map_ops)
135+
BPF_MAP_TYPE(BPF_MAP_TYPE_ARENA, arena_map_ops)
135136

136137
BPF_LINK_TYPE(BPF_LINK_TYPE_RAW_TRACEPOINT, raw_tracepoint)
137138
BPF_LINK_TYPE(BPF_LINK_TYPE_TRACING, tracing)

include/uapi/linux/bpf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,7 @@ enum bpf_map_type {
983983
BPF_MAP_TYPE_BLOOM_FILTER,
984984
BPF_MAP_TYPE_USER_RINGBUF,
985985
BPF_MAP_TYPE_CGRP_STORAGE,
986+
BPF_MAP_TYPE_ARENA,
986987
__MAX_BPF_MAP_TYPE
987988
};
988989

@@ -1370,6 +1371,12 @@ enum {
13701371

13711372
/* BPF token FD is passed in a corresponding command's token_fd field */
13721373
BPF_F_TOKEN_FD = (1U << 16),
1374+
1375+
/* When user space page faults in bpf_arena send SIGSEGV instead of inserting new page */
1376+
BPF_F_SEGV_ON_FAULT = (1U << 17),
1377+
1378+
/* Do not translate kernel bpf_arena pointers to user pointers */
1379+
BPF_F_NO_USER_CONV = (1U << 18),
13731380
};
13741381

13751382
/* Flags for BPF_PROG_QUERY. */

kernel/bpf/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ obj-${CONFIG_BPF_LSM} += bpf_inode_storage.o
1515
obj-$(CONFIG_BPF_SYSCALL) += disasm.o mprog.o
1616
obj-$(CONFIG_BPF_JIT) += trampoline.o
1717
obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o
18+
ifeq ($(CONFIG_MMU)$(CONFIG_64BIT),yy)
19+
obj-$(CONFIG_BPF_SYSCALL) += arena.o
20+
endif
1821
obj-$(CONFIG_BPF_JIT) += dispatcher.o
1922
ifeq ($(CONFIG_NET),y)
2023
obj-$(CONFIG_BPF_SYSCALL) += devmap.o

0 commit comments

Comments
 (0)