staticvoid bpf_array_free_percpu(struct bpf_array *array)
{ int i;
for (i = 0; i < array->map.max_entries; i++) {
free_percpu(array->pptrs[i]);
cond_resched();
}
}
staticint bpf_array_alloc_percpu(struct bpf_array *array)
{ void __percpu *ptr; int i;
for (i = 0; i < array->map.max_entries; i++) {
ptr = bpf_map_alloc_percpu(&array->map, array->elem_size, 8,
GFP_USER | __GFP_NOWARN); if (!ptr) {
bpf_array_free_percpu(array); return -ENOMEM;
}
array->pptrs[i] = ptr;
cond_resched();
}
return 0;
}
/* Called from syscall */ int array_map_alloc_check(union bpf_attr *attr)
{ bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY; int numa_node = bpf_map_attr_numa_node(attr);
/* On 32 bit archs roundup_pow_of_two() with max_entries that has * upper most bit set in u32 space is undefined behavior due to * resulting 1U << 32, so do it manually here in u64 space.
*/
mask64 = fls_long(max_entries - 1);
mask64 = 1ULL << mask64;
mask64 -= 1;
index_mask = mask64; if (!bypass_spec_v1) { /* round up array size to nearest power of 2, * since cpu will speculate within index_mask limits
*/
max_entries = index_mask + 1; /* Check for overflows. */ if (max_entries < attr->max_entries) return ERR_PTR(-E2BIG);
}
array_size = sizeof(*array); if (percpu) {
array_size += (u64) max_entries * sizeof(void *);
} else { /* rely on vmalloc() to return page-aligned memory and * ensure array->value is exactly page-aligned
*/ if (attr->map_flags & BPF_F_MMAPABLE) {
array_size = PAGE_ALIGN(array_size);
array_size += PAGE_ALIGN((u64) max_entries * elem_size);
} else {
array_size += (u64) max_entries * elem_size;
}
}
/* allocate all map elements and zero-initialize them */ if (attr->map_flags & BPF_F_MMAPABLE) { void *data;
/* kmalloc'ed memory can't be mmap'ed, use explicit vmalloc */
data = bpf_map_area_mmapable_alloc(array_size, numa_node); if (!data) return ERR_PTR(-ENOMEM);
array = data + PAGE_ALIGN(sizeof(struct bpf_array))
- offsetof(struct bpf_array, value);
} else {
array = bpf_map_area_alloc(array_size, numa_node);
} if (!array) return ERR_PTR(-ENOMEM);
array->index_mask = index_mask;
array->map.bypass_spec_v1 = bypass_spec_v1;
/* Called from syscall or from eBPF program */ staticvoid *array_map_lookup_elem(struct bpf_map *map, void *key)
{ struct bpf_array *array = container_of(map, struct bpf_array, map);
u32 index = *(u32 *)key;
if (unlikely(index >= array->map.max_entries)) return NULL;
int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
{ struct bpf_array *array = container_of(map, struct bpf_array, map);
u32 index = *(u32 *)key; void __percpu *pptr; int cpu, off = 0;
u32 size;
if (unlikely(index >= array->map.max_entries)) return -ENOENT;
/* per_cpu areas are zero-filled and bpf programs can only * access 'value_size' of them, so copying rounded areas * will not leak any kernel data
*/
size = array->elem_size;
rcu_read_lock();
pptr = array->pptrs[index & array->index_mask];
for_each_possible_cpu(cpu) {
copy_map_value_long(map, value + off, per_cpu_ptr(pptr, cpu));
check_and_init_map_value(map, value + off);
off += size;
}
rcu_read_unlock(); return 0;
}
int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
u64 map_flags)
{ struct bpf_array *array = container_of(map, struct bpf_array, map);
u32 index = *(u32 *)key; void __percpu *pptr; int cpu, off = 0;
u32 size;
if (unlikely(map_flags > BPF_EXIST)) /* unknown flags */ return -EINVAL;
if (unlikely(index >= array->map.max_entries)) /* all elements were pre-allocated, cannot insert a new one */ return -E2BIG;
if (unlikely(map_flags == BPF_NOEXIST)) /* all elements already exist */ return -EEXIST;
/* the user space will provide round_up(value_size, 8) bytes that * will be copied into per-cpu area. bpf programs can only access * value_size of it. During lookup the same extra bytes will be * returned or zeros which were zero-filled by percpu_alloc, * so no kernel data leaks possible
*/
size = array->elem_size;
rcu_read_lock();
pptr = array->pptrs[index & array->index_mask];
for_each_possible_cpu(cpu) {
copy_map_value_long(map, per_cpu_ptr(pptr, cpu), value + off);
bpf_obj_free_fields(array->map.record, per_cpu_ptr(pptr, cpu));
off += size;
}
rcu_read_unlock(); return 0;
}
/* Called from syscall or from eBPF program */ staticlong array_map_delete_elem(struct bpf_map *map, void *key)
{ return -EINVAL;
}
/* We don't reset or free fields other than timer and workqueue * on uref dropping to zero.
*/ if (btf_record_has_field(map->record, BPF_TIMER | BPF_WORKQUEUE)) { for (i = 0; i < array->map.max_entries; i++) { if (btf_record_has_field(map->record, BPF_TIMER))
bpf_obj_free_timer(map->record, array_map_elem_ptr(array, i)); if (btf_record_has_field(map->record, BPF_WORKQUEUE))
bpf_obj_free_workqueue(map->record, array_map_elem_ptr(array, i));
}
}
}
/* Called when map->refcnt goes to zero, either from workqueue or from syscall */ staticvoid array_map_free(struct bpf_map *map)
{ struct bpf_array *array = container_of(map, struct bpf_array, map); int i;
if (!IS_ERR_OR_NULL(map->record)) { if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { for (i = 0; i < array->map.max_entries; i++) { void __percpu *pptr = array->pptrs[i & array->index_mask]; int cpu;
for_each_possible_cpu(cpu) {
bpf_obj_free_fields(map->record, per_cpu_ptr(pptr, cpu));
cond_resched();
}
}
} else { for (i = 0; i < array->map.max_entries; i++)
bpf_obj_free_fields(map->record, array_map_elem_ptr(array, i));
}
}
if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
bpf_array_free_percpu(array);
if (array->map.map_flags & BPF_F_MMAPABLE)
bpf_map_area_free(array_map_vmalloc_addr(array)); else
bpf_map_area_free(array);
}
staticint array_map_check_btf(conststruct bpf_map *map, conststruct btf *btf, conststruct btf_type *key_type, conststruct btf_type *value_type)
{ /* One exception for keyless BTF: .bss/.data/.rodata map */ if (btf_type_is_void(key_type)) { if (map->map_type != BPF_MAP_TYPE_ARRAY ||
map->max_entries != 1) return -EINVAL;
if (BTF_INFO_KIND(value_type->info) != BTF_KIND_DATASEC) return -EINVAL;
return 0;
}
/* * Bpf array can only take a u32 key. This check makes sure * that the btf matches the attr used during map_create.
*/ if (!btf_type_is_i32(key_type)) return -EINVAL;
if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
buf_size = array->elem_size * num_possible_cpus();
value_buf = kmalloc(buf_size, GFP_USER | __GFP_NOWARN); if (!value_buf) return -ENOMEM;
seq_info->percpu_value_buf = value_buf;
}
/* bpf_iter_attach_map() acquires a map uref, and the uref may be * released before or in the middle of iterating map elements, so * acquire an extra map uref for iterator.
*/
bpf_map_inc_with_uref(map);
seq_info->map = map; return 0;
}
staticint fd_array_map_alloc_check(union bpf_attr *attr)
{ /* only file descriptors can be stored in this type of map */ if (attr->value_size != sizeof(u32)) return -EINVAL; /* Program read-only/write-only not supported for special maps yet. */ if (attr->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) return -EINVAL; return array_map_alloc_check(attr);
}
mutex_lock(&prog->aux->ext_mutex);
prog->aux->prog_array_member_cnt--;
mutex_unlock(&prog->aux->ext_mutex); /* bpf_prog is freed after one RCU or tasks trace grace period */
bpf_prog_put(prog);
}
/* decrement refcnt of all bpf_progs that are stored in this map */ staticvoid bpf_fd_array_map_clear(struct bpf_map *map, bool need_defer)
{ struct bpf_array *array = container_of(map, struct bpf_array, map); int i;
for (i = 0; i < array->map.max_entries; i++)
__fd_array_map_delete_elem(map, &i, need_defer);
}
staticint prog_array_map_poke_track(struct bpf_map *map, struct bpf_prog_aux *prog_aux)
{ struct prog_poke_elem *elem; struct bpf_array_aux *aux; int ret = 0;
aux = container_of(map, struct bpf_array, map)->aux;
mutex_lock(&aux->poke_mutex);
list_for_each_entry(elem, &aux->poke_progs, list) { if (elem->aux == prog_aux) goto out;
}
elem = kmalloc(sizeof(*elem), GFP_KERNEL); if (!elem) {
ret = -ENOMEM; goto out;
}
INIT_LIST_HEAD(&elem->list); /* We must track the program's aux info at this point in time * since the program pointer itself may not be stable yet, see * also comment in prog_array_map_poke_run().
*/
elem->aux = prog_aux;
aux = container_of(map, struct bpf_array, map)->aux;
WARN_ON_ONCE(!mutex_is_locked(&aux->poke_mutex));
list_for_each_entry(elem, &aux->poke_progs, list) { struct bpf_jit_poke_descriptor *poke; int i;
for (i = 0; i < elem->aux->size_poke_tab; i++) {
poke = &elem->aux->poke_tab[i];
/* Few things to be aware of: * * 1) We can only ever access aux in this context, but * not aux->prog since it might not be stable yet and * there could be danger of use after free otherwise. * 2) Initially when we start tracking aux, the program * is not JITed yet and also does not have a kallsyms * entry. We skip these as poke->tailcall_target_stable * is not active yet. The JIT will do the final fixup * before setting it stable. The various * poke->tailcall_target_stable are successively * activated, so tail call updates can arrive from here * while JIT is still finishing its final fixup for * non-activated poke entries. * 3) Also programs reaching refcount of zero while patching * is in progress is okay since we're protected under * poke_mutex and untrack the programs before the JIT * buffer is freed.
*/ if (!READ_ONCE(poke->tailcall_target_stable)) continue; if (poke->reason != BPF_POKE_REASON_TAIL_CALL) continue; if (poke->tail_call.map != map ||
poke->tail_call.key != key) continue;
if (map->map_flags & BPF_F_PRESERVE_ELEMS) return;
rcu_read_lock(); for (i = 0; i < array->map.max_entries; i++) {
ee = READ_ONCE(array->ptrs[i]); if (ee && ee->map_file == map_file)
__fd_array_map_delete_elem(map, &i, true);
}
rcu_read_unlock();
}
inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd); if (IS_ERR(inner_map_meta)) return inner_map_meta;
map = array_map_alloc(attr); if (IS_ERR(map)) {
bpf_map_meta_free(inner_map_meta); return map;
}
map->inner_map_meta = inner_map_meta;
return map;
}
staticvoid array_of_map_free(struct bpf_map *map)
{ /* map->inner_map_meta is only accessed by syscall which * is protected by fdget/fdput.
*/
bpf_map_meta_free(map->inner_map_meta);
bpf_fd_array_map_clear(map, false);
fd_array_map_free(map);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.