implementation of eBPF maps
Subsystems (5)
cover implementation of eBPF maps
Hi All,
this set of patches adds implementation of HASH and ARRAY types of eBPF maps which were described in manpage in commit b4fc1a460f30("Merge branch 'bpf-next'")
The difference vs previous version of these patches from August:
- added 'flags' attribute to BPF_MAP_UPDATE_ELEM
- in HASH type implementation removed per-map kmem_cache.
I was doing kmem_cache_create() for every map to enable selective slub debugging to check for overflows and leaks. Now it's not needed, so just use normal kmalloc() for map elements.
- added ARRAY type which was mentioned in manpage, but wasn't public yet
- added map testsuite and removed temporary bits from test_stubs
Note, eBPF programs cannot be attached to events yet. It will come in the next set.
Alexei Starovoitov (7):
bpf: add 'flags' attribute to BPF_MAP_UPDATE_ELEM command
bpf: add hashtable type of eBPF maps
bpf: add array type of eBPF maps
bpf: fix BPF_MAP_LOOKUP_ELEM command return code
bpf: add a testsuite for eBPF maps
bpf: allow eBPF programs to use maps
bpf: remove test map scaffolding and user proper types
include/linux/bpf.h | 7 +- include/uapi/linux/bpf.h | 15 +- kernel/bpf/Makefile | 2 +- kernel/bpf/arraymap.c | 150 ++++++++++++++++++ kernel/bpf/hashtab.c | 362 +++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/helpers.c | 88 +++++++++++ kernel/bpf/syscall.c | 6 +- kernel/bpf/test_stub.c | 56 ++----- samples/bpf/Makefile | 3 +- samples/bpf/libbpf.c | 3 +- samples/bpf/libbpf.h | 2 +- samples/bpf/test_maps.c | 287 ++++++++++++++++++++++++++++++++++ samples/bpf/test_verifier.c | 14 +- 13 files changed, 932 insertions(+), 63 deletions(-) create mode 100644 kernel/bpf/arraymap.c create mode 100644 kernel/bpf/hashtab.c create mode 100644 kernel/bpf/helpers.c create mode 100644 samples/bpf/test_maps.c
Signature · 1 line
1.7.9.5
PATCH 1/7 bpf: add 'flags' attribute to BPF_MAP_UPDATE_ELEM command
the current meaning of BPF_MAP_UPDATE_ELEM syscall command is:
either update existing map element or create a new one. Initially the plan was to add a new command to handle the case of 'create new element if it didn't exist', but 'flags' style looks cleaner and overall diff is much smaller (more code reused), so add 'flags' attribute to BPF_MAP_UPDATE_ELEM command with the following meaning:
enum {
BPF_MAP_UPDATE_OR_CREATE = 0, /* add new element or update existing */
BPF_MAP_CREATE_ONLY, /* add new element if it didn't exist */
BPF_MAP_UPDATE_ONLY /* update existing element */
};
BPF_MAP_CREATE_ONLY can fail with EEXIST if element already exists. BPF_MAP_UPDATE_ONLY can fail with ENOENT if element doesn't exist.
Userspace will call it as:
int bpf_update_elem(int fd, void *key, void *value, __u64 flags)
{
union bpf_attr attr = {
.map_fd = fd,
.key = ptr_to_u64(key),
.value = ptr_to_u64(value),
.flags = flags;
};
return bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
}--- include/linux/bpf.h | 2 +- include/uapi/linux/bpf.h | 10 +++++++++- kernel/bpf/syscall.c | 4 ++-- 3 files changed, 12 insertions(+), 4 deletions(-)
include/linux/bpf.h +1 -1
@@ -22,7 +22,7 @@ struct bpf_map_ops {/* funcs callable from userspace and from eBPF programs */ void *(*map_lookup_elem)(struct bpf_map *map,void *key); -int (*map_update_elem)(struct bpf_map *map,void *key,void *value); +int (*map_update_elem)(struct bpf_map *map,void *key,void *value,u64 flags);int (*map_delete_elem)(struct bpf_map *map,void *key); };
include/uapi/linux/bpf.h +9 -1
@@ -82,7 +82,7 @@ enum bpf_cmd {/* create or update key/value pair in a given map * err = bpf(BPF_MAP_UPDATE_ELEM, union bpf_attr *attr, u32 size) -* Using attr->map_fd, attr->key, attr->value +* Using attr->map_fd, attr->key, attr->value, attr->flags * returns zero or negative error */ BPF_MAP_UPDATE_ELEM,
@@ -117,6 +117,13 @@ enum bpf_prog_type { BPF_PROG_TYPE_UNSPEC, }; +/* flags for BPF_MAP_UPDATE_ELEM command */ +enum bpf_map_update_flags { +BPF_MAP_UPDATE_OR_CREATE =0 ,/* add new element or update existing */ +BPF_MAP_CREATE_ONLY ,/* add new element if it didn't exist */ +BPF_MAP_UPDATE_ONLY /* update existing element */ +}; +union bpf_attr {struct {/* anonymous struct used by BPF_MAP_CREATE command */ __u32 map_type;/* one of enum bpf_map_type */
@@ -132,6 +139,7 @@ union bpf_attr {__aligned_u64 value;__aligned_u64 next_key; }; +__u64 flags; };struct {/* anonymous struct used by BPF_PROG_LOAD command */
kernel/bpf/syscall.c +2 -2
@@ -190,7 +190,7 @@ err_put:return err; } -#define BPF_MAP_UPDATE_ELEM_LAST_FIELD value +#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flagsstatic int map_update_elem (union bpf_attr *attr) {
@@ -231,7 +231,7 @@ static int map_update_elem(union bpf_attr *attr)* therefore all map accessors rely on this fact, so do the same here */ rcu_read_lock (); - err = map->ops->map_update_elem (map, key, value); + err = map->ops->map_update_elem (map, key, value, attr->flags);rcu_read_unlock (); free_value:
Signature · 1 line
1.7.9.5
PATCH 2/7 bpf: add hashtable type of eBPF maps
In reply to Alexei Starovoitovadd new map type BPF_MAP_TYPE_HASH and its implementation
- maps are created/destroyed by userspace. Both userspace and eBPF programs can lookup/update/delete elements from the map
- eBPF programs can be called in_irq(), so use spin_lock_irqsave() mechanism for concurrent updates
- key/value are opaque range of bytes (aligned to 8 bytes)
- user space provides 3 configuration attributes via BPF syscall:
key_size, value_size, max_entries
- map takes care of allocating/freeing key/value pairs
- map_update_elem() must fail to insert new element when max_entries limit is reached to make sure that eBPF programs cannot exhaust memory
- map_update_elem() replaces elements in an atomic way
- optimized for speed of lookup() which can be called multiple times from eBPF program which itself is triggered by high volume of events . in the future JIT compiler may recognize lookup() call and optimize it further, since key_size is constant for life of eBPF program
--- include/uapi/linux/bpf.h | 1 + kernel/bpf/Makefile | 2 +- kernel/bpf/hashtab.c | 362 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 364 insertions(+), 1 deletion(-) create mode 100644 kernel/bpf/hashtab.c
include/uapi/linux/bpf.h +1 -0
@@ -111,6 +111,7 @@ enum bpf_cmd {enum bpf_map_type {BPF_MAP_TYPE_UNSPEC , +BPF_MAP_TYPE_HASH , };enum bpf_prog_type {
kernel/bpf/Makefile +1 -1
@@ -1,5 +1,5 @@ obj-y := core.o -obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o +obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o hashtab.o ifdef CONFIG_TEST_BPF obj-$(CONFIG_BPF_SYSCALL) += test_stub.o endif
kernel/bpf/hashtab.c new file +362 -0
362 lines not on this page · raw message
Signature · 1 line
1.7.9.5
PATCH 3/7 bpf: add array type of eBPF maps
In reply to Alexei Starovoitovadd new map type BPF_MAP_TYPE_ARRAY and its implementation
- optimized for fastest possible lookup()
. in the future verifier/JIT may recognize lookup() with constant key
and optimize it into constant pointer. Can optimize non-constant key into direct pointer arithmetic as well, since pointers and value_size are constant for the life of the eBPF program. In other words array_map_lookup_elem() may be 'inlined' by verifier/JIT while preserving concurrent access to this map from user space
- two main use cases for array type:
. 'global' eBPF variables: array of 1 element with key=0 and value is a
collection of 'global' variables which programs can use to keep the state between events
. aggregation of tracing events into fixed set of buckets
- all array elements pre-allocated and zero initialized at init time
- key as an index in array and can only be 4 byte
- map_delete_elem() returns EINVAL, since elements cannot be deleted
- map_update_elem() replaces elements in an non-atomic way (for atomic updates hashtable type should be used instead)
---
Note, from eBPF program and from user space, all map types are accessed through the same API.
Example of using array type for 'global' variables from eBPF program:
struct globals {
u64 lat_ave;
u64 lat_sum;
u64 missed;
u64 max_lat;
int num_samples;
};struct bpf_map_def SEC("maps") global_map = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(struct globals),
.max_entries = 1,
};
int bpf_prog(struct bpf_context *ctx)
{
...
int ind = 0;
struct globals *g = bpf_map_lookup_elem(&global_map, &ind);
if (!g)
return 0;
if (g->lat_ave == 0) {
g->num_samples++;
g->lat_sum += delta;
if (g->num_samples >= 100) {
g->lat_ave = g->lat_sum / g->num_samples;
...The future verifier/JIT optimization will replace bpf_map_lookup_elem() call inside eBPF program with const pointer to element value of key=0, so that eBPF program will have no penalty whatsoever to access such 'global' variables.
At the same time user space can access this 'globals' via common map API.
Full example of both kernel and user side follows in later patches.
include/uapi/linux/bpf.h | 1 + kernel/bpf/Makefile | 2 +- kernel/bpf/arraymap.c | 150 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 kernel/bpf/arraymap.c
include/uapi/linux/bpf.h +1 -0
@@ -112,6 +112,7 @@ enum bpf_cmd {enum bpf_map_type {BPF_MAP_TYPE_UNSPEC ,BPF_MAP_TYPE_HASH , +BPF_MAP_TYPE_ARRAY , };enum bpf_prog_type {
kernel/bpf/Makefile +1 -1
@@ -1,5 +1,5 @@ obj-y := core.o -obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o hashtab.o +obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o hashtab.o arraymap.o ifdef CONFIG_TEST_BPF obj-$(CONFIG_BPF_SYSCALL) += test_stub.o endif
kernel/bpf/arraymap.c new file +150 -0
150 lines not on this page · raw message
Signature · 1 line
1.7.9.5
PATCH 4/7 bpf: fix BPF_MAP_LOOKUP_ELEM command return code
In reply to Alexei Starovoitovfix errno of BPF_MAP_LOOKUP_ELEM command as bpf manpage described it in commit b4fc1a460f30("Merge branch 'bpf-next'"):
-----BPF_MAP_LOOKUP_ELEM
int bpf_lookup_elem(int fd, void *key, void *value)
{
union bpf_attr attr = {
.map_fd = fd,
.key = ptr_to_u64(key),
.value = ptr_to_u64(value),
};
return bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
}
bpf() syscall looks up an element with given key in a map fd.
If element is found it returns zero and stores element's value
into value. If element is not found it returns -1 and sets
errno to ENOENT.and further down in manpage:
ENOENT For BPF_MAP_LOOKUP_ELEM or BPF_MAP_DELETE_ELEM, indicates that
element with given key was not found.
-----
In general all BPF commands return ENOENT when map element is not found (including BPF_MAP_GET_NEXT_KEY and BPF_MAP_UPDATE_ELEM with
flags == BPF_MAP_UPDATE_ONLY)
Subsequent patch adds a testsuite to check return values for all of these combinations.
---
I don't think this patch is needed for 'net', since 'net' has syscall shell only. Actual map types and their implementations are being introduced by this set of patches.
kernel/bpf/syscall.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/syscall.c +1 -1
@@ -169,7 +169,7 @@ static int map_lookup_elem(union bpf_attr *attr)if (copy_from_user (key, ukey, map->key_size) !=0 ) goto free_key; - err = -ESRCH ; + err = -ENOENT ;rcu_read_lock (); value = map->ops->map_lookup_elem (map, key);if (!value)
Signature · 1 line
1.7.9.5
PATCH 5/7 bpf: add a testsuite for eBPF maps
In reply to Alexei Starovoitov. check error conditions and sanity of hash and array map APIs
. check large maps (that kernel gracefully switches to vmalloc from kmalloc) . check multi-process parallel access and stress test
---
Eventually it can be moved tools/testing/selftests/bpf/, but for now keep it in samples/bpf/, since that's where all subsequent samples are coming to.
samples/bpf/Makefile | 3 +- samples/bpf/libbpf.c | 3 +- samples/bpf/libbpf.h | 2 +- samples/bpf/test_maps.c | 287 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 292 insertions(+), 3 deletions(-) create mode 100644 samples/bpf/test_maps.c
samples/bpf/Makefile +2 -1
@@ -2,9 +2,10 @@ obj- := dummy.o # List of programs to build -hostprogs-y := test_verifier +hostprogs-y := test_verifier test_maps test_verifier-objs := test_verifier.o libbpf.o +test_maps-objs := test_maps.o libbpf.o # Tell kbuild to always build the programs always := $(hostprogs-y)
samples/bpf/libbpf.c +2 -1
@@ -27,12 +27,13 @@ int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,return syscall (__NR_bpf,BPF_MAP_CREATE , &attr,sizeof (attr)); } -int bpf_update_elem (int fd,void *key,void *value) +int bpf_update_elem (int fd,void *key,void *value,unsigned long long flags) {union bpf_attr attr = { .map_fd = fd, .key =ptr_to_u64 (key), .value =ptr_to_u64 (value), + .flags = flags, };return syscall (__NR_bpf,BPF_MAP_UPDATE_ELEM , &attr,sizeof (attr));
samples/bpf/libbpf.h +1 -1
@@ -6,7 +6,7 @@ struct bpf_insn;int bpf_create_map (enum bpf_map_type map_type,int key_size,int value_size,int max_entries); -int bpf_update_elem (int fd,void *key,void *value); +int bpf_update_elem (int fd,void *key,void *value,unsigned long long flags);int bpf_lookup_elem (int fd,void *key,void *value);int bpf_delete_elem (int fd,void *key);int bpf_get_next_key (int fd,void *key,void *next_key);
samples/bpf/test_maps.c new file +287 -0
287 lines not on this page · raw message
Signature · 1 line
1.7.9.5
PATCH 6/7 bpf: allow eBPF programs to use maps
In reply to Alexei Starovoitovexpose bpf_map_lookup_elem(), bpf_map_update_elem(), bpf_map_delete_elem() map accessors to eBPF programs
---
Note, these helpers are exposed as '.gpl_only = false', so non-GPL eBPF programs can use them. That was requested by AndyL and DavidL before.
include/linux/bpf.h | 5 +++ include/uapi/linux/bpf.h | 3 ++ kernel/bpf/Makefile | 2 +- kernel/bpf/helpers.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 kernel/bpf/helpers.c
include/linux/bpf.h +5 -0
@@ -133,4 +133,9 @@ struct bpf_prog *bpf_prog_get(u32 ufd);/* verify correctness of eBPF program */ int bpf_check (struct bpf_prog *fp,union bpf_attr *attr); +/* verifier prototypes for helper functions called from eBPF programs */ +extern struct bpf_func_proto bpf_map_lookup_elem_proto; +extern struct bpf_func_proto bpf_map_update_elem_proto; +extern struct bpf_func_proto bpf_map_delete_elem_proto; +#endif /* _LINUX_BPF_H */
include/uapi/linux/bpf.h +3 -0
@@ -160,6 +160,9 @@ union bpf_attr {*/ enum bpf_func_id { BPF_FUNC_unspec, + BPF_FUNC_map_lookup_elem,/* void *map_lookup_elem(&map, &key) */ + BPF_FUNC_map_update_elem,/* int map_update_elem(&map, &key, &value, flags) */ + BPF_FUNC_map_delete_elem,/* int map_delete_elem(&map, &key) */ __BPF_FUNC_MAX_ID, };
kernel/bpf/Makefile +1 -1
@@ -1,5 +1,5 @@ obj-y := core.o -obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o hashtab.o arraymap.o +obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o hashtab.o arraymap.o helpers.o ifdef CONFIG_TEST_BPF obj-$(CONFIG_BPF_SYSCALL) += test_stub.o endif
kernel/bpf/helpers.c new file +88 -0
88 lines not on this page · raw message
Signature · 1 line
1.7.9.5
PATCH 7/7 bpf: remove test map scaffolding and use proper types
In reply to Alexei Starovoitovproper types and function helpers are ready. Use them in verifier testsuite. Remove temporary stubs
--- kernel/bpf/test_stub.c | 56 +++++++------------------------------------ samples/bpf/test_verifier.c | 14 +++++------ 2 files changed, 16 insertions(+), 54 deletions(-)
kernel/bpf/test_stub.c +9 -47
@@ -18,26 +18,18 @@ struct bpf_context {u64 arg2; }; -static u64 test_func (u64 r1,u64 r2,u64 r3,u64 r4,u64 r5) -{ -return 0 ; -} - -static struct bpf_func_proto test_funcs[] = { - [BPF_FUNC_unspec] = { - .func = test_func, - .gpl_only = true, - .ret_type =RET_PTR_TO_MAP_VALUE_OR_NULL , - .arg1_type =ARG_CONST_MAP_PTR , - .arg2_type =ARG_PTR_TO_MAP_KEY , - }, -}; -static const struct bpf_func_proto *test_func_proto (enum bpf_func_id func_id) { -if (func_id <0 || func_id >=ARRAY_SIZE (test_funcs)) +switch (func_id) { +case BPF_FUNC_map_lookup_elem: +return &bpf_map_lookup_elem_proto; +case BPF_FUNC_map_update_elem: +return &bpf_map_update_elem_proto; +case BPF_FUNC_map_delete_elem: +return &bpf_map_delete_elem_proto; +default :return NULL ; -return &test_funcs[func_id]; + } } static const struct bpf_context_access {
@@ -78,38 +70,8 @@ static struct bpf_prog_type_list tl_prog = { .type = BPF_PROG_TYPE_UNSPEC, }; -static struct bpf_map *test_map_alloc (union bpf_attr *attr) -{ -struct bpf_map *map; - - map =kzalloc (sizeof (*map),GFP_USER ); -if (!map) -return ERR_PTR (-ENOMEM ); - - map->key_size = attr->key_size; - map->value_size = attr->value_size; - map->max_entries = attr->max_entries; -return map; -} - -static void test_map_free (struct bpf_map *map) -{ -kfree (map); -} - -static struct bpf_map_ops test_map_ops = { - .map_alloc = test_map_alloc, - .map_free = test_map_free, -}; - -static struct bpf_map_type_list tl_map = { - .ops = &test_map_ops, - .type =BPF_MAP_TYPE_UNSPEC , -}; -static int __init register_test_ops (void ) { -bpf_register_map_type (&tl_map);bpf_register_prog_type (&tl_prog);return 0 ; }
samples/bpf/test_verifier.c +7 -7
@@ -261,7 +261,7 @@ static struct bpf_test tests[] = {BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ),BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ),BPF_LD_MAP_FD (BPF_REG_1 ,0 ), -BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_unspec), +BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_map_lookup_elem), BPF_EXIT_INSN(), }, .fixup = {2},
@@ -417,7 +417,7 @@ static struct bpf_test tests[] = {BPF_ALU64_REG (BPF_MOV ,BPF_REG_2 ,BPF_REG_10 ),BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ),BPF_LD_MAP_FD (BPF_REG_1 ,0 ), -BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_unspec), +BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_map_delete_elem), BPF_EXIT_INSN(), }, .errstr = "fd 0 is not pointing to valid bpf_map",
@@ -430,7 +430,7 @@ static struct bpf_test tests[] = {BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ),BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ),BPF_LD_MAP_FD (BPF_REG_1 ,0 ), -BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_unspec), +BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_map_lookup_elem),BPF_ST_MEM (BPF_DW ,BPF_REG_0 ,0 ,0 ), BPF_EXIT_INSN(), },
@@ -445,7 +445,7 @@ static struct bpf_test tests[] = {BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ),BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ),BPF_LD_MAP_FD (BPF_REG_1 ,0 ), -BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_unspec), +BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_map_lookup_elem),BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,1 ),BPF_ST_MEM (BPF_DW ,BPF_REG_0 ,4 ,0 ), BPF_EXIT_INSN(),
@@ -461,7 +461,7 @@ static struct bpf_test tests[] = {BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ),BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ),BPF_LD_MAP_FD (BPF_REG_1 ,0 ), -BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_unspec), +BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_map_lookup_elem),BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,2 ),BPF_ST_MEM (BPF_DW ,BPF_REG_0 ,0 ,0 ), BPF_EXIT_INSN(),
@@ -548,7 +548,7 @@ static struct bpf_test tests[] = {BPF_ST_MEM (BPF_DW ,BPF_REG_2 ,-56 ,0 ),BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-56 ),BPF_LD_MAP_FD (BPF_REG_1 ,0 ), -BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_unspec), +BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_map_delete_elem), BPF_EXIT_INSN(), }, .fixup = {24},
@@ -659,7 +659,7 @@ static int create_map(void)long long key, value =0 ;int map_fd; - map_fd =bpf_create_map (BPF_MAP_TYPE_UNSPEC ,sizeof (key),sizeof (value),1024 ); + map_fd =bpf_create_map (BPF_MAP_TYPE_HASH ,sizeof (key),sizeof (value),1024 );if (map_fd <0 ) {printf ("failed to create map '%s'\n" ,strerror (errno)); }
Signature · 1 line
1.7.9.5
Re: [PATCH net-next 1/7] bpf: add 'flags' attribute to BPF_MAP_UPDATE_ELEM command
In reply to Alexei StarovoitovQuoted 11 lines · Alexei Starovoitov
the current meaning of
BPF_MAP_UPDATE_ELEMsyscall command is:
either update existing map element or create a new one. Initially the plan was to add a new command to handle the case of 'create new element if it didn't exist', but 'flags' style looks cleaner and overall diff is much smaller (more code reused), so add 'flags' attribute toBPF_MAP_UPDATE_ELEMcommand with the following meaning:
enum {
BPF_MAP_UPDATE_OR_CREATE= 0, /* add new element or update existing */
BPF_MAP_CREATE_ONLY, /* add new element if it didn't exist */
BPF_MAP_UPDATE_ONLY/* update existing element */
};
From you commit message/code I currently don't see an explanation why it cannot be done in typical ``flags style'' as various syscalls do, i.e. BPF_MAP_UPDATE_OR_CREATE rather represented as ...
BPF_MAP_CREATE | BPF_MAP_UPDATE
Do you expect more than 64 different flags to be passed from user space for BPF_MAP?
Quoted 17 lines
BPF_MAP_CREATE_ONLYcan fail with EEXIST if element already exists.BPF_MAP_UPDATE_ONLYcan fail with ENOENT if element doesn't exist.Userspace will call it as:
intbpf_update_elem(int fd, void *key, void *value, __u64 flags){ union bpf_attr attr = { .map_fd = fd, .key = ptr_to_u64(key), .value = ptr_to_u64(value), .flags = flags; }; return bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)); }Signed-off-by Alexei Starovoitov <[email protected]>
Re: [PATCH net-next 6/7] bpf: allow eBPF programs to use maps
In reply to Alexei StarovoitovQuoted 4 lines · Alexei Starovoitov
expose
bpf_map_lookup_elem(),bpf_map_update_elem(),bpf_map_delete_elem()map accessors to eBPF programs
...
Quoted 59 lines
kernel/bpf/helpers.c new file +59
-0+#include <linux/bpf.h> +#include <linux/rcupdate.h> + +/* called from eBPF program under rcu lock +* +* if kernel subsystem is allowing eBPF programs to call this function, +* inside its own verifier_ops->get_func_proto() callback it should return +* bpf_map_lookup_elem_proto, so that verifier can properly checks the arguments +*/ +static u64 bpf_map_lookup_elem (u64 r1,u64 r2,u64 r3,u64 r4,u64 r5) +{ +/* verifier checked that R1 contains a valid pointer to bpf_map +* and R2 points to a program stack and map->key_size bytes were +* initialized +*/ +struct bpf_map *map = (struct bpf_map *) (unsigned long ) r1; +void *key = (void *) (unsigned long ) r2; +void *value; + +WARN_ON_ONCE (!rcu_read_lock_held ()); + + value = map->ops->map_lookup_elem (map, key); + +/* lookup() returns either pointer to element value or NULL +* which is the meaning of PTR_TO_MAP_VALUE_OR_NULL type +*/ +return (unsigned long ) value; +} + +struct bpf_func_proto bpf_map_lookup_elem_proto = { + .func = bpf_map_lookup_elem, + .gpl_only = false, + .ret_type =RET_PTR_TO_MAP_VALUE_OR_NULL , + .arg1_type =ARG_CONST_MAP_PTR , + .arg2_type =ARG_PTR_TO_MAP_KEY , +}; + +/* called from eBPF program under rcu lock */ +static u64 bpf_map_update_elem (u64 r1,u64 r2,u64 r3,u64 r4,u64 r5) +{ +struct bpf_map *map = (struct bpf_map *) (unsigned long ) r1; +void *key = (void *) (unsigned long ) r2; +void *value = (void *) (unsigned long ) r3; + +WARN_ON_ONCE (!rcu_read_lock_held ()); + +return map->ops->map_update_elem (map, key, value, r4); +} + +struct bpf_func_proto bpf_map_update_elem_proto = { + .func = bpf_map_update_elem, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_CONST_MAP_PTR , + .arg2_type =ARG_PTR_TO_MAP_KEY , + .arg3_type =ARG_PTR_TO_MAP_VALUE , + .arg4_type =ARG_ANYTHING , +}; +
kernel/bpf/helpers.c line 80
Quoted 10 lines
kernel/bpf/helpers.c new file +10
-0+/* called from eBPF program under rcu lock */ +static u64 bpf_map_delete_elem (u64 r1,u64 r2,u64 r3,u64 r4,u64 r5) +{ +struct bpf_map *map = (struct bpf_map *) (unsigned long ) r1; +void *key = (void *) (unsigned long ) r2; + +WARN_ON_ONCE (!rcu_read_lock_held ()); + +return map->ops->map_delete_elem (map, key); +}
These WARN_ON_ONCE(!rcu_read_lock_held()) seem odd. While I see the point that you're holding RCU read lock on the lookup, can you elaborate on your RCU usage here and why it's necessary for delete/update?
I suspect due to the synchronize_rcu() you're using and not using any RCU accessors but plain memcpy() e.g. in case of the array ...?
Quoted 8 lines
kernel/bpf/helpers.c new file +7
-0+struct bpf_func_proto bpf_map_delete_elem_proto = { + .func = bpf_map_delete_elem, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_CONST_MAP_PTR , + .arg2_type =ARG_PTR_TO_MAP_KEY , +};
Re: [PATCH net-next 3/7] bpf: add array type of eBPF maps
In reply to Alexei StarovoitovQuoted 26 lines · Alexei Starovoitov
add new map type
BPF_MAP_TYPE_ARRAYand its implementation- optimized for fastest possible
lookup()
. in the future verifier/JIT may recognizelookup()with constant key
and optimize it into constant pointer. Can optimize non-constant key into direct pointer arithmetic as well, since pointers andvalue_sizeare constant for the life of the eBPF program. In other wordsarray_map_lookup_elem()may be 'inlined' by verifier/JIT while preserving concurrent access to this map from user space- two main use cases for array type:
. 'global' eBPF variables: array of 1 element with key=0 and value is a
collection of 'global' variables which programs can use to keep the state between events
. aggregation of tracing events into fixed set of buckets- all array elements pre-allocated and zero initialized at init time
- key as an index in array and can only be 4 byte
-
map_delete_elem()returns EINVAL, since elements cannot be deleted-
map_update_elem()replaces elements in an non-atomic way (for atomic updates hashtable type should be used instead)Signed-off-by Alexei Starovoitov <[email protected]>
...
Quoted 9 lines
kernel/bpf/arraymap.c new file +9
-0+/* Called from syscall or from eBPF program */ +static int array_map_update_elem (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; + +if (map_flags >BPF_MAP_UPDATE_ONLY ) +/* unknown flags */
kernel/bpf/arraymap.c line 106
Quoted 10 lines
kernel/bpf/arraymap.c new file +10
-0+return -EINVAL ; + +if (map_flags ==BPF_MAP_CREATE_ONLY ) +return -EINVAL ; + +if (index >= array->map.max_entries) +/* all elements were pre-allocated, cannot insert a new one */ +return -E2BIG ; + +memcpy (array->value + array->elem_size * index, value, array->elem_size);
What would protect this from concurrent updates?
Quoted 2 lines
(quoted diff) +2
-0+ return 0; +}
Re: [PATCH net-next 1/7] bpf: add 'flags' attribute to BPF_MAP_UPDATE_ELEM command
In reply to Daniel BorkmannQuoted 16 lines · Daniel Borkmann
Quoted 12 lines · Alexei Starovoitov
the current meaning of
BPF_MAP_UPDATE_ELEMsyscall command is:
either update existing map element or create a new one. Initially the plan was to add a new command to handle the case of 'create new element if it didn't exist', but 'flags' style looks cleaner and overall diff is much smaller (more code reused), so add 'flags' attribute toBPF_MAP_UPDATE_ELEMcommand with the following meaning:
enum {
BPF_MAP_UPDATE_OR_CREATE= 0, /* add new element or update existing */
BPF_MAP_CREATE_ONLY, /* add new element if it didn't exist */
BPF_MAP_UPDATE_ONLY/* update existing element */
};
Quoted 8 lines
From you commit message/code I currently don't see an explanation why it cannot be done in typical ``flags style'' as various syscalls do, i.e.
BPF_MAP_UPDATE_OR_CREATErather represented as ...
BPF_MAP_CREATE|BPF_MAP_UPDATEDo you expect more than 64 different flags to be passed from user space for
BPF_MAP?
several reasons:
- preserve flags==0 as default behavior
- avoid holes and extra checks for invalid combinations, so if (flags > BPF_MAP_UPDATE_ONLY) goto err, is enough.
- it looks much neater when user space uses BPF_MAP_UPDATE_OR_CREATE instead of ORing bits.
Note this choice doesn't prevent adding bit-like flags in the future. Today I cannot think of any new flags for the update() command, but if somebody comes up with a new selector that can apply to all three combinations, we can add it as 3rd bit that can be ORed. Default will stay zero and 'if >' check in older kernels will seamlessly work with new userspace. I don't like holes in flags and combinatorial explosion of bits and checks for them unless absolutely necessary.
Re: [PATCH net-next 6/7] bpf: allow eBPF programs to use maps
In reply to Daniel BorkmannQuoted 8 lines · Daniel Borkmann
These WARN_ON_ONCE(!rcu_read_lock_held()) seem odd. While I see the point that
you're holding RCU read lock on the lookup, can you elaborate on your RCU usage
here and why it's necessary for delete/update?I suspect due to the
synchronize_rcu()you're using and not using any RCU accessors but plainmemcpy()e.g. in case of the array ...?
Correct in case of array.
Also hash delete/update() call into lookup() internally that is using _rcu() helpers...
Future map types might have much more
complex implementations (like LPM), so it helps to state the rules early.
Another reason is more complex to explain:
A program that intends to access maps has to be one rcu critical section. So all lookup/update/delete calls are under rcu_lock_held.
Since programs by themselves cannot have WARN_ON inside them, I've added WARN_ON in these three functions that will be called from the programs to make sure that kernel subsystems don't do (*prog->bpf_func)(...) without taking rcu_lock if they intend to let programs access maps.
Having said that in the future we might have a case for programs that don't call into these functions at all and execute instructions only. Those won't need rcu_lock() wrap. I experimented with that for the patch where I replaced pred-tree walker with eBPF program. There is no rcu there. And no calls
to map accessors.
Has to be noted, that socket filters use rcu to protect sk_filter pointer and program itself. So for that use case we'll keep using rcu for foreseeable future. For tracing filters I had to add rcu_lock() around BPF_PROG_RUN() invocation and these WARN_ON checks saved me a lot of headache, so I prefer to keep them since they cost nothing when lockdep is off.
Re: [PATCH net-next 3/7] bpf: add array type of eBPF maps
In reply to Daniel BorkmannQuoted 5 lines · Daniel Borkmann
Quoted 3 lines
(quoted diff) +2
-0+ + memcpy(array->value + array->elem_size * index, value,
array->elem_size);What would protect this from concurrent updates?
nothing.
that's what I meant in commit log:
- map_update_elem() replaces elements in an non-atomic way (for atomic updates hashtable type should be used instead)
The array map is like C array of structures. Nothing protects concurrent access.
It's used in the cases where accuracy is not needed or when there is no concurrent access.
To compute a histogram of events in tracing the array of integers is used. Every integer is a counter. Program increments it (may be without using xadd) and user space periodically reads it back. map_update_elem() is called by userspace once
to initialize it if zero-init is not enough. Programs do lookup() and modify the values.
For array type update() method is used rarely, delete() is never used and get_next() is needed for completeness to browse maps through
common map API.
I'm guessing you're asking, because it may feel that adding a lock() will help to make it more useful? ;) It's not, since programs cannot take a lock().
Re: [PATCH net-next 1/7] bpf: add 'flags' attribute to BPF_MAP_UPDATE_ELEM command
In reply to Alexei StarovoitovQuoted 31 lines · Alexei Starovoitov
Quoted 14 lines · Daniel Borkmann
Quoted 12 lines · Alexei Starovoitov
the current meaning of
BPF_MAP_UPDATE_ELEMsyscall command is:
either update existing map element or create a new one. Initially the plan was to add a new command to handle the case of 'create new element if it didn't exist', but 'flags' style looks cleaner and overall diff is much smaller (more code reused), so add 'flags' attribute toBPF_MAP_UPDATE_ELEMcommand with the following meaning:
enum {
BPF_MAP_UPDATE_OR_CREATE= 0, /* add new element or update existing */
BPF_MAP_CREATE_ONLY, /* add new element if it didn't exist */
BPF_MAP_UPDATE_ONLY/* update existing element */
};Quoted 8 lines
From you commit message/code I currently don't see an explanation why
it cannot be done in typical ``flags style'' as various syscalls do, i.e.BPF_MAP_UPDATE_OR_CREATErather represented as ...
BPF_MAP_CREATE|BPF_MAP_UPDATEDo you expect more than 64 different flags to be passed from user space for
BPF_MAP?several reasons:
- preserve flags==0 as default behavior
- avoid holes and extra checks for invalid combinations, so if (flags >BPF_MAP_UPDATE_ONLY) goto err, is enough.
- it looks much neater when user space usesBPF_MAP_UPDATE_OR_CREATEinstead of ORing bits.
Quoted 5 lines
Note this choice doesn't prevent adding bit-like flags in the future. Today I cannot think of any new flags for the
update()command, but if somebody comes up with a new selector that can apply to all three combinations, we can add it as 3rd bit that can be ORed.
Hm, mixing enums together with bitfield-like flags seems kind of hacky ... :/ Or, do you mean to say you're adding a 2nd flag field, i.e. splitting the 64bits into a 32bit ``cmd enum'' and 32bit ``flag section''?
I see the point with flags == 0 as default behavior though, but at this point in time we won't get burnt by it since the API is not yet in a usable state and defaults to be compiled-out.
Quoted 5 lines
Default will stay zero and 'if >' check in older kernels will seamlessly work with new userspace. I don't like holes in flags and combinatorial explosion of bits and checks for them unless absolutely necessary.
Hm, my concern is that we start to add many *_OR_* enum elements once we find that a flag might be a useful in combination with many other flags ... even though if we only can think of 3 flags /right now/.
Re: [PATCH net-next 1/7] bpf: add 'flags' attribute to BPF_MAP_UPDATE_ELEM command
Quoted 48 lines · Daniel Borkmann
Quoted 40 lines · Alexei Starovoitov
Quoted 20 lines · Daniel Borkmann
Quoted 16 lines · Alexei Starovoitov
the current meaning of
BPF_MAP_UPDATE_ELEMsyscall command is:
either update existing map element or create a new one. Initially the plan was to add a new command to handle the case of 'create new element if it didn't exist', but 'flags' style looks cleaner and overall diff is much smaller (more code reused), so add 'flags'
attribute toBPF_MAP_UPDATE_ELEMcommand with the following meaning:
enum {
BPF_MAP_UPDATE_OR_CREATE= 0, /* add new element or update existing
*/
BPF_MAP_CREATE_ONLY, /* add new element if it didn't exist
*/
BPF_MAP_UPDATE_ONLY/* update existing element */
};Quoted 8 lines
From you commit message/code I currently don't see an explanation why
it cannot be done in typical ``flags style'' as various syscalls do, i.e.BPF_MAP_UPDATE_OR_CREATErather represented as ...
BPF_MAP_CREATE|BPF_MAP_UPDATEDo you expect more than 64 different flags to be passed from user space for
BPF_MAP?several reasons:
- preserve flags==0 as default behavior
- avoid holes and extra checks for invalid combinations, so if (flags >BPF_MAP_UPDATE_ONLY) goto err, is enough.
- it looks much neater when user space usesBPF_MAP_UPDATE_OR_CREATEinstead of ORing bits.Quoted 5 lines
Note this choice doesn't prevent adding bit-like flags in the future. Today I cannot think of any new flags for the
update()command, but if somebody comes up with a new selector that can apply to all three combinations, we can add it as 3rd bit that can be ORed.
Quoted 4 lines
Hm, mixing enums together with bitfield-like flags seems kind of hacky ... :/ Or, do you mean to say you're adding a 2nd flag field, i.e. splitting the 64bits into a 32bit ``cmd enum'' and 32bit ``flag section''?
something like this.
or splitting 64-bit into 2 and 62. We'll see. First two encode this 'type' of update and the rest - whatever else.
Quoted 4 lines
Hm, my concern is that we start to add many *_OR_* enum elements once we find that a flag might be a useful in combination with many other flags ... even though if we only can think of 3 flags /right now/.
Agree. Adding many *_OR_* would look bad, that's why I said that future additions can be bits. Like in paragraph above.
Also, we don't have 3 flags now. In this patch I'm showing 3 types and you're suggesting to treat them as 2 flags. To me that's incorrect, since 'no flags' becomes invalid combination, which logically incorrect. Therefore I cannot see them as 'flags'. This is a 'type' or 'style' of update() command.
I think it actually matches how open() defines things in similar situation:
#define O_RDONLY 00000000 #define O_WRONLY 00000001 #define O_RDWR 00000002
We used to think of them as flags, but they're not bit flags, though the rest of open() flags are bit-like. If we apply your argument to open() then open() should have defined O_RD as 1 and OR_WR as 2 and force everyone to mix and match them, but then zero would be invalid. So I still think that what I have is a cleaner API :)