Introduce BPF map tracing capability
Subsystems (7)
v2 cover Introduce BPF map tracing capability
From: Joe Burton <[email protected]>
This patch introduces 'map tracing': the capability to execute a tracing program after updating a map.
Map tracing enables upgrades of stateful programs with fewer race conditions than otherwise possible. We use a tracing program to imbue a map with copy-on-write semantics, then use an iterator to perform a bulk copy of data in the map. After bulk copying concludes, updates to that map automatically propagate via the tracing program, avoiding a class of race conditions. This use case is demonstrated in the new 'real_world_example' selftest.
Extend BPF_PROG_TYPE_TRACING with a new attach type, BPF_TRACE_MAP, and allow linking these programs to arbitrary maps.
Extend the verifier to invoke helper calls directly after bpf_map_update_elem() and bpf_map_delete_elem(). The helpers have the exact same signature as the functions they trace, and simply pass those arguments to the list of tracing programs attached to the map.
struct bpf_map gets a single additional pointer, which points to a structure containing lists of tracing programs. This pointer is populated when the first tracing program is attached to a map, to minimize memory overhead for the majority of maps which are not traced. I use an atomic cmpxchg() to avoid the need for a mutex when accessing the pointer. Once populated, the pointer is never freed until the map itself is freed.
One open question is how to handle pointer-based map updates. For example:
int *x = bpf_map_lookup_elem(...);
if (...) *x++;
if (...) *x--;
We can't just call a helper function right after the bpf_map_lookup_elem(), since the updates occur later on. We also can't determine where the last modification to the pointer occurs, due to branch instructions. I would therefore consider a pattern where we 'flush' pointers at the end of a BPF program:
int *x = bpf_map_lookup_elem(...);
...
/* Execute tracing programs for this cell in this map. */ bpf_map_trace_pointer_update(x);
return 0;
We can't necessarily do this in the verifier, since 'x' may no longer be in a register or on the stack. Thus we might introduce a helper to save pointers that should be flushed, then flush all registered pointers at every exit point:
int *x = bpf_map_lookup_elem(...);
/*
* Saves 'x' somewhere in kernel memory. Does nothing if no
* corresponding tracing progs are attached to the map. */
bpf_map_trace_register_pointer(x);
...
/* flush all registered pointers */ bpf_map_trace_pointer_update();
return 0;
This should be easy to implement in the verifier.
In addition, we use the verifier to instrument certain map update calls. This requires saving arguments onto the stack, which means that a program using MAX_BPF_STACK bytes of stack could exceed the limit. I don't know whether this actually causes any problems.
Linux Plumbers Conference slides on this topic:
https://linuxplumbersconf.org/event/11/contributions/942/attachments/
814/1533/joe_burton_lpc_2021_slides.pdf
Joe Burton (13):
bpf: Add machinery to register map tracing hooks
bpf: Allow loading BPF_TRACE_MAP programs
bpf: Add list of tracing programs to struct bpf_map
bpf: Define a few bpf_link_ops for BPF_TRACE_MAP
bpf: Enable creation of BPF_LINK_TYPE_MAP_TRACE
bpf: Add APIs to invoke tracing programs
bpf: Register BPF_MAP_TRACE_{UPDATE,DELETE}_ELEM hooks
libbpf: Support BPF_TRACE_MAP
bpf: Add infinite loop check on map tracers
Add bpf_map_trace_{update,delete}_elem() helper functions
bpf: verifier inserts map tracing helper call
bpf: Add selftests for map tracing
bpf: Add real world example for map tracing
include/linux/bpf.h | 48 ++- include/linux/bpf_types.h | 1 + include/uapi/linux/bpf.h | 22 + kernel/bpf/Makefile | 1 + kernel/bpf/helpers.c | 29 ++ kernel/bpf/map_trace.c | 406 ++++++++++++++++++ kernel/bpf/syscall.c | 7 + kernel/bpf/verifier.c | 81 +++- tools/include/uapi/linux/bpf.h | 22 + tools/lib/bpf/bpf.c | 13 +- tools/lib/bpf/bpf.h | 4 +- tools/lib/bpf/libbpf.c | 118 +++++ tools/lib/bpf/libbpf.h | 11 + tools/lib/bpf/libbpf.map | 1 + .../selftests/bpf/prog_tests/bpf_map_trace.c | 401 +++++++++++++++++ .../bpf/progs/bpf_map_trace_delete_elem.c | 49 +++ .../selftests/bpf/progs/bpf_map_trace_loop0.c | 26 ++ .../selftests/bpf/progs/bpf_map_trace_loop1.c | 43 ++ .../progs/bpf_map_trace_real_world_common.h | 125 ++++++ .../bpf_map_trace_real_world_migration.c | 96 +++++ .../bpf/progs/bpf_map_trace_real_world_new.c | 4 + .../bpf/progs/bpf_map_trace_real_world_old.c | 5 + .../bpf/progs/bpf_map_trace_update_elem.c | 51 +++ .../selftests/bpf/verifier/map_trace.c | 40 ++ 24 files changed, 1592 insertions(+), 12 deletions(-) create mode 100644 kernel/bpf/map_trace.c create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_map_trace.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_map_trace_delete_elem.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_map_trace_loop0.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_map_trace_loop1.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_map_trace_real_world_common.h create mode 100644 tools/testing/selftests/bpf/progs/bpf_map_trace_real_world_migration.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_map_trace_real_world_new.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_map_trace_real_world_old.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_map_trace_update_elem.c create mode 100644 tools/testing/selftests/bpf/verifier/map_trace.c
Signature · 1 line
2.33.0.685.g46640cef36-goog
RFC PATCH v2 1/13 bpf: Add machinery to register map tracing hooks
From: Joe Burton <[email protected]>
Maps may be traced in two ways (so far): after a key-only operation s.a. a deletion, and after a key-value operation s.a. an update. Each type is identified by a tracing type.
In order to reject invalid map tracing programs at load time, we export a function for each tracing type. Programs include this function's name or BTF ID when loading. The verifier will check that the traced function is registered for map tracing and that the program has the right arguments.
--- include/linux/bpf.h | 12 ++++++++++++ include/uapi/linux/bpf.h | 9 +++++++++ kernel/bpf/Makefile | 1 + kernel/bpf/map_trace.c | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 kernel/bpf/map_trace.c
include/linux/bpf.h +12 -0
@@ -1510,6 +1510,17 @@ struct bpf_iter_reg {const struct bpf_iter_seq_info *seq_info; }; +#define BPF_MAP_TRACE_FUNC_SYM (trace_type) bpf_map_trace__ ## trace_type +#define DEFINE_BPF_MAP_TRACE_FUNC(trace_type, args...) \ + extern int BPF_MAP_TRACE_FUNC_SYM(trace_type)(args); \ + int __init BPF_MAP_TRACE_FUNC_SYM(trace_type)(args) \ + { return 0; } + +struct bpf_map_trace_reg { +const char *target; +enum bpf_map_trace_type trace_type; +}; +struct bpf_iter_meta { __bpf_md_ptr(struct seq_file *, seq);u64 session_id;
@@ -1528,6 +1539,7 @@ void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);bool bpf_iter_prog_supported (struct bpf_prog *prog);const struct bpf_func_proto *bpf_iter_get_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog); +int bpf_map_trace_reg_target (const struct bpf_map_trace_reg *reg_info);int bpf_iter_link_attach (const union bpf_attr *attr,bpfptr_t uattr,struct bpf_prog *prog);int bpf_iter_new_fd (struct bpf_link *link);bool bpf_link_is_iter (struct bpf_link *link);
include/uapi/linux/bpf.h +9 -0
@@ -93,6 +93,15 @@ union bpf_iter_link_info { } map; }; +enum bpf_map_trace_type { +BPF_MAP_TRACE_UPDATE_ELEM =0 , +BPF_MAP_TRACE_DELETE_ELEM =1 , + +MAX_BPF_MAP_TRACE_TYPE , +}; + +#define BPF_MAP_TRACE_FUNC (trace_type) "bpf_map_trace__" #trace_type +/* BPF syscall commands, see bpf(2) man-page for more details. */ /** * DOC: eBPF Syscall Preamble
kernel/bpf/Makefile +1 -0
@@ -36,3 +36,4 @@ obj-$(CONFIG_BPF_SYSCALL) += bpf_struct_ops.o obj-${CONFIG_BPF_LSM} += bpf_lsm.o endif obj-$(CONFIG_BPF_PRELOAD) += preload/ +obj-$(CONFIG_BPF_SYSCALL) += map_trace.o
kernel/bpf/map_trace.c new file +33 -0
@@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2021 Google */ + +#include <linux/filter.h> +#include <linux/bpf.h> + +struct bpf_map_trace_target_info { +struct list_head list; +const struct bpf_map_trace_reg *reg_info; +u32 btf_id; +}; + +static struct list_head targets =LIST_HEAD_INIT (targets); +static DEFINE_MUTEX (targets_mutex); + +int bpf_map_trace_reg_target (const struct bpf_map_trace_reg *reg_info) +{ +struct bpf_map_trace_target_info *tinfo; + + tinfo =kmalloc (sizeof (*tinfo),GFP_KERNEL ); +if (!tinfo) +return -ENOMEM ; + +INIT_LIST_HEAD (&tinfo->list); + tinfo->reg_info = reg_info; + tinfo->btf_id =0 ; + +mutex_lock (&targets_mutex); +list_add (&tinfo->list, &targets); +mutex_unlock (&targets_mutex); + +return 0 ; +}
Signature · 1 line
2.33.0.685.g46640cef36-goog
RFC PATCH v2 2/13 bpf: Allow loading BPF_TRACE_MAP programs
In reply to Joe BurtonFrom: Joe Burton <[email protected]>
Add new attach type, BPF_TRACE_MAP. Tracing programs may specify this type when loading. The verifier then checks that the traced function has been registered for map tracing.
--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 1 + kernel/bpf/map_trace.c | 25 +++++++++++++++++++++++++ kernel/bpf/verifier.c | 6 ++++++ 4 files changed, 33 insertions(+)
include/linux/bpf.h +1 -0
@@ -1539,6 +1539,7 @@ void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);bool bpf_iter_prog_supported (struct bpf_prog *prog);const struct bpf_func_proto *bpf_iter_get_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog); +bool bpf_map_trace_prog_supported (struct bpf_prog *prog);int bpf_map_trace_reg_target (const struct bpf_map_trace_reg *reg_info);int bpf_iter_link_attach (const union bpf_attr *attr,bpfptr_t uattr,struct bpf_prog *prog);int bpf_iter_new_fd (struct bpf_link *link);
include/uapi/linux/bpf.h +1 -0
@@ -1003,6 +1003,7 @@ enum bpf_attach_type {BPF_SK_REUSEPORT_SELECT ,BPF_SK_REUSEPORT_SELECT_OR_MIGRATE ,BPF_PERF_EVENT , +BPF_TRACE_MAP , __MAX_BPF_ATTACH_TYPE };
kernel/bpf/map_trace.c +25 -0
@@ -31,3 +31,28 @@ int bpf_map_trace_reg_target(const struct bpf_map_trace_reg *reg_info)return 0 ; } + +bool bpf_map_trace_prog_supported (struct bpf_prog *prog) +{ +const char *attach_fname = prog->aux->attach_func_name; +u32 prog_btf_id = prog->aux->attach_btf_id; +struct bpf_map_trace_target_info *tinfo; +bool supported = false; + +mutex_lock (&targets_mutex); +list_for_each_entry (tinfo, &targets, list) { +if (tinfo->btf_id && tinfo->btf_id == prog_btf_id) { + supported = true; +break ; + } +if (!strcmp (attach_fname, tinfo->reg_info->target)) { + tinfo->btf_id = prog->aux->attach_btf_id; + supported = true; +break ; + } + } +mutex_unlock (&targets_mutex); + +return supported; +} +
kernel/bpf/verifier.c +6 -0
@@ -9464,6 +9464,7 @@ static int check_return_code(struct bpf_verifier_env *env)break ;case BPF_TRACE_RAW_TP :case BPF_MODIFY_RETURN : +case BPF_TRACE_MAP :return 0 ;case BPF_TRACE_ITER :break ;
@@ -13496,6 +13497,7 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,break ;case BPF_TRACE_ITER : +case BPF_TRACE_MAP :if (!btf_type_is_func (t)) {bpf_log (log,"attach_btf_id %u is not a function\n" , btf_id);
@@ -13672,6 +13674,10 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)if (!bpf_iter_prog_supported (prog))return -EINVAL ;return 0 ; + }else if (prog->expected_attach_type ==BPF_TRACE_MAP ) { +if (!bpf_map_trace_prog_supported (prog)) +return -EINVAL ; +return 0 ; }if (prog->type ==BPF_PROG_TYPE_LSM ) {
Signature · 1 line
2.33.0.685.g46640cef36-goog
RFC PATCH v2 3/13 bpf: Add list of tracing programs to struct bpf_map
In reply to Joe BurtonFrom: Joe Burton <[email protected]>
Add array of programs to struct bpf_map. To minimize cost, this array is allocated at the time that the first program is attached to a map. It is installed using a cmpxchg() to avoid adding another mutex inside bpf_map.
--- include/linux/bpf.h | 18 +++++++++++++++++- kernel/bpf/map_trace.c | 21 +++++++++++++++++++++ kernel/bpf/syscall.c | 4 ++++ 3 files changed, 42 insertions(+), 1 deletion(-)
include/linux/bpf.h +17 -1
@@ -43,6 +43,7 @@ struct kobject;struct mem_cgroup ;struct module ;struct bpf_func_state ; +struct bpf_map_trace_progs ;extern struct idr btf_idr;extern spinlock_t btf_idr_lock;
@@ -180,10 +181,11 @@ struct bpf_map {struct mem_cgroup *memcg;#endif char name[BPF_OBJ_NAME_LEN ]; +struct bpf_map_trace_progs *trace_progs;u32 btf_vmlinux_value_type_id;bool bypass_spec_v1;bool frozen;/* write-once; write-protected by freeze_mutex */ -/* 22 bytes hole */ +/* 14 bytes hole */ /* The 3rd and 4th cacheline with misc members to avoid false sharing * particularly with refcounting.
@@ -1510,12 +1512,26 @@ struct bpf_iter_reg {const struct bpf_iter_seq_info *seq_info; }; +/* Maximum number of tracing programs that may be attached to one map */ +#define BPF_MAP_TRACE_MAX_PROGS 16#define BPF_MAP_TRACE_FUNC_SYM (trace_type) bpf_map_trace__ ## trace_type #define DEFINE_BPF_MAP_TRACE_FUNC(trace_type, args...) \ extern int BPF_MAP_TRACE_FUNC_SYM(trace_type)(args); \ int __init BPF_MAP_TRACE_FUNC_SYM(trace_type)(args) \ { return 0; } +struct bpf_map_trace_prog { +struct list_head list; +struct bpf_prog *prog; +struct rcu_head rcu; +}; + +struct bpf_map_trace_progs { +struct bpf_map_trace_prog __rcu progs[MAX_BPF_MAP_TRACE_TYPE ]; +u32 length[MAX_BPF_MAP_TRACE_TYPE ]; +struct mutex mutex;/* protects writes to progs, length */ +}; +struct bpf_map_trace_reg {const char *target;enum bpf_map_trace_type trace_type;
kernel/bpf/map_trace.c +21 -0
@@ -3,6 +3,7 @@#include <linux/filter.h> #include <linux/bpf.h> +#include <linux/rcupdate.h> struct bpf_map_trace_target_info {struct list_head list;
@@ -56,3 +57,23 @@ bool bpf_map_trace_prog_supported(struct bpf_prog *prog)return supported; } +int bpf_map_initialize_trace_progs (struct bpf_map *map) +{ +struct bpf_map_trace_progs *new_trace_progs; +int i; + +if (!READ_ONCE (map->trace_progs)) { + new_trace_progs =kzalloc (sizeof (struct bpf_map_trace_progs ), +GFP_KERNEL ); +if (!new_trace_progs) +return -ENOMEM ; +mutex_init (&new_trace_progs->mutex); +for (i =0 ; i <MAX_BPF_MAP_TRACE_TYPE ; i++) +INIT_LIST_HEAD (&new_trace_progs->progs[i].list); +if (cmpxchg (&map->trace_progs,NULL , new_trace_progs)) +kfree (new_trace_progs); + } + +return 0 ; +} +
kernel/bpf/syscall.c +4 -0
@@ -460,6 +460,7 @@ static void bpf_map_free_deferred(struct work_struct *work)security_bpf_map_free (map);bpf_map_release_memcg (map); +kfree (map->trace_progs);/* implementation dependent freeing */ map->ops->map_free (map); }
@@ -913,6 +914,9 @@ static int map_create(union bpf_attr *attr)return err; } +/* tracing programs lists are allocated when attached */ + map->trace_progs =NULL ; +return err; free_map_sec:
Signature · 1 line
2.33.0.685.g46640cef36-goog
RFC PATCH v2 4/13 bpf: Define a few bpf_link_ops for BPF_TRACE_MAP
In reply to Joe BurtonFrom: Joe Burton <[email protected]>
Define release, dealloc, and update_prog for the new tracing programs. Updates are protected by a single global mutex.
--- kernel/bpf/map_trace.c | 71 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+)
kernel/bpf/map_trace.c +71 -0
@@ -14,6 +14,14 @@ struct bpf_map_trace_target_info {static struct list_head targets =LIST_HEAD_INIT (targets);static DEFINE_MUTEX (targets_mutex); +struct bpf_map_trace_link { +struct bpf_link link; +struct bpf_map *map; +struct bpf_map_trace_target_info *tinfo; +}; + +static DEFINE_MUTEX (link_mutex); +int bpf_map_trace_reg_target (const struct bpf_map_trace_reg *reg_info) {struct bpf_map_trace_target_info *tinfo;
@@ -77,3 +85,66 @@ int bpf_map_initialize_trace_progs(struct bpf_map *map)return 0 ; } +static void bpf_map_trace_link_release (struct bpf_link *link) +{ +struct bpf_map_trace_link *map_trace_link = + container_of(link, struct bpf_map_trace_link, link); +enum bpf_map_trace_type trace_type = + map_trace_link->tinfo->reg_info->trace_type; +struct bpf_map_trace_prog *cur_prog; +struct bpf_map_trace_progs *progs; + + progs = map_trace_link->map->trace_progs; +mutex_lock (&progs->mutex); +list_for_each_entry (cur_prog, &progs->progs[trace_type].list, list) { +if (cur_prog->prog == link->prog) { + progs->length[trace_type] -=1 ; +list_del_rcu (&cur_prog->list); +kfree_rcu (cur_prog, rcu); + } + } +mutex_unlock (&progs->mutex); +bpf_map_put_with_uref (map_trace_link->map); +} + +static void bpf_map_trace_link_dealloc (struct bpf_link *link) +{ +struct bpf_map_trace_link *map_trace_link = + container_of(link, struct bpf_map_trace_link, link); + +kfree (map_trace_link); +} + +static int bpf_map_trace_link_replace (struct bpf_link *link, +struct bpf_prog *new_prog, +struct bpf_prog *old_prog) +{ +int ret =0 ; + +mutex_lock (&link_mutex); +if (old_prog && link->prog != old_prog) { + ret = -EPERM ; + goto out_unlock; + } + +if (link->prog->type != new_prog->type || + link->prog->expected_attach_type != new_prog->expected_attach_type || + link->prog->aux->attach_btf_id != new_prog->aux->attach_btf_id) { + ret = -EINVAL ; + goto out_unlock; + } + + old_prog =xchg (&link->prog, new_prog); +bpf_prog_put (old_prog); + +out_unlock: +mutex_unlock (&link_mutex); +return ret; +} + +static const struct bpf_link_ops bpf_map_trace_link_ops = { + .release = bpf_map_trace_link_release, + .dealloc = bpf_map_trace_link_dealloc, + .update_prog = bpf_map_trace_link_replace, +}; +
Signature · 1 line
2.33.0.685.g46640cef36-goog
Re: [RFC PATCH v2 04/13] bpf: Define a few bpf_link_ops for BPF_TRACE_MAP
Quoted 35 lines · Joe Burton
From: Joe Burton <[email protected]>
Define release, dealloc, and
update_progfor the new tracing programs. Updates are protected by a single global mutex.Signed-off-by Joe Burton <[email protected]>--- kernel/bpf/map_trace.c | 71 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+)kernel/bpf/map_trace.c +10
-0@@ -14,6 +14,14 @@ struct bpf_map_trace_target_info {static struct list_head targets =LIST_HEAD_INIT (targets);static DEFINE_MUTEX (targets_mutex); +struct bpf_map_trace_link { +struct bpf_link link; +struct bpf_map *map; +struct bpf_map_trace_target_info *tinfo; +}; + +static DEFINE_MUTEX (link_mutex); +int bpf_map_trace_reg_target (const struct bpf_map_trace_reg *reg_info) {struct bpf_map_trace_target_info *tinfo;@@ -77,3 +85,66 @@ int bpf_map_initialize_trace_progs(struct bpf_map *map)return 0 ; } +static void bpf_map_trace_link_release (struct bpf_link *link) +{
kernel/bpf/map_trace.c int bpf_map_initialize_trace_progs(struct bpf_map *map) · line 99
Quoted 10 lines
kernel/bpf/map_trace.c +10
-0+struct bpf_map_trace_link *map_trace_link = + container_of(link, struct bpf_map_trace_link, link); +enum bpf_map_trace_type trace_type = + map_trace_link->tinfo->reg_info->trace_type; +struct bpf_map_trace_prog *cur_prog; +struct bpf_map_trace_progs *progs; + + progs = map_trace_link->map->trace_progs; +mutex_lock (&progs->mutex); +list_for_each_entry (cur_prog, &progs->progs[trace_type].list, list) {
You might consider using list_for_each_entry_safe(), or ...
kernel/bpf/map_trace.c int bpf_map_initialize_trace_progs(struct bpf_map *map) · line 103
Quoted 4 lines
kernel/bpf/map_trace.c +4
-0+if (cur_prog->prog == link->prog) { + progs->length[trace_type] -=1 ; +list_del_rcu (&cur_prog->list); +kfree_rcu (cur_prog, rcu);
or add a break; if you do not expect to find multiple entries.
Quoted 48 lines
kernel/bpf/map_trace.c +47
-0+ } + } +mutex_unlock (&progs->mutex); +bpf_map_put_with_uref (map_trace_link->map); +} + +static void bpf_map_trace_link_dealloc (struct bpf_link *link) +{ +struct bpf_map_trace_link *map_trace_link = + container_of(link, struct bpf_map_trace_link, link); + +kfree (map_trace_link); +} + +static int bpf_map_trace_link_replace (struct bpf_link *link, +struct bpf_prog *new_prog, +struct bpf_prog *old_prog) +{ +int ret =0 ; + +mutex_lock (&link_mutex); +if (old_prog && link->prog != old_prog) { + ret = -EPERM ; + goto out_unlock; + } + +if (link->prog->type != new_prog->type || + link->prog->expected_attach_type != new_prog->expected_attach_type || + link->prog->aux->attach_btf_id != new_prog->aux->attach_btf_id) { + ret = -EINVAL ; + goto out_unlock; + } + + old_prog =xchg (&link->prog, new_prog); +bpf_prog_put (old_prog); + +out_unlock: +mutex_unlock (&link_mutex); +return ret; +} + +static const struct bpf_link_ops bpf_map_trace_link_ops = { + .release = bpf_map_trace_link_release, + .dealloc = bpf_map_trace_link_dealloc, + .update_prog = bpf_map_trace_link_replace, +}; +
Re: [RFC PATCH v2 04/13] bpf: Define a few bpf_link_ops for BPF_TRACE_MAP
Good catch, applied both changes. The expectation is to remove only one
program. Theoretically an app could link the same program to the same
map
twice, twice, in which case close()ing one link should not detach both programs.
I opted to also apply the _safe() suffix mostly as a matter of convention.
- struct bpf_map_trace_prog *cur_prog;
+ struct bpf_map_trace_prog *cur_prog, *tmp;
struct bpf_map_trace_progs *progs;
progs = map_trace_link->map->trace_progs;
mutex_lock(&progs->mutex);
- list_for_each_entry(cur_prog, &progs->progs[trace_type].list, list) {
+ list_for_each_entry_safe(cur_prog, tmp, &progs->progs[trace_type].list,
+ list) {
if (cur_prog->prog == link->prog) {
progs->length[trace_type] -= 1;
list_del_rcu(&cur_prog->list);
kfree_rcu(cur_prog, rcu);
+ break;
}
}Quoted 107 lines · Eric Dumazet
Quoted 35 lines · Joe Burton
From: Joe Burton <[email protected]>
Define release, dealloc, and
update_progfor the new tracing programs. Updates are protected by a single global mutex.Signed-off-by Joe Burton <[email protected]>--- kernel/bpf/map_trace.c | 71 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+)kernel/bpf/map_trace.c +10
-0@@ -14,6 +14,14 @@ struct bpf_map_trace_target_info {static struct list_head targets =LIST_HEAD_INIT (targets);static DEFINE_MUTEX (targets_mutex); +struct bpf_map_trace_link { +struct bpf_link link; +struct bpf_map *map; +struct bpf_map_trace_target_info *tinfo; +}; + +static DEFINE_MUTEX (link_mutex); +int bpf_map_trace_reg_target (const struct bpf_map_trace_reg *reg_info) {struct bpf_map_trace_target_info *tinfo;@@ -77,3 +85,66 @@ int bpf_map_initialize_trace_progs(struct bpf_map *map)return 0 ; } +static void bpf_map_trace_link_release (struct bpf_link *link) +{kernel/bpf/map_trace.c int bpf_map_initialize_trace_progs(struct bpf_map *map) · line 99
Quoted 10 lines
kernel/bpf/map_trace.c +10
-0+struct bpf_map_trace_link *map_trace_link = + container_of(link, struct bpf_map_trace_link, link); +enum bpf_map_trace_type trace_type = + map_trace_link->tinfo->reg_info->trace_type; +struct bpf_map_trace_prog *cur_prog; +struct bpf_map_trace_progs *progs; + + progs = map_trace_link->map->trace_progs; +mutex_lock (&progs->mutex); +list_for_each_entry (cur_prog, &progs->progs[trace_type].list, list) {You might consider using
list_for_each_entry_safe(), or ...kernel/bpf/map_trace.c int bpf_map_initialize_trace_progs(struct bpf_map *map) · line 103
Quoted 4 lines
kernel/bpf/map_trace.c +4
-0+if (cur_prog->prog == link->prog) { + progs->length[trace_type] -=1 ; +list_del_rcu (&cur_prog->list); +kfree_rcu (cur_prog, rcu);or add a break; if you do not expect to find multiple entries.
Quoted 48 lines
kernel/bpf/map_trace.c +47
-0+ } + } +mutex_unlock (&progs->mutex); +bpf_map_put_with_uref (map_trace_link->map); +} + +static void bpf_map_trace_link_dealloc (struct bpf_link *link) +{ +struct bpf_map_trace_link *map_trace_link = + container_of(link, struct bpf_map_trace_link, link); + +kfree (map_trace_link); +} + +static int bpf_map_trace_link_replace (struct bpf_link *link, +struct bpf_prog *new_prog, +struct bpf_prog *old_prog) +{ +int ret =0 ; + +mutex_lock (&link_mutex); +if (old_prog && link->prog != old_prog) { + ret = -EPERM ; + goto out_unlock; + } + +if (link->prog->type != new_prog->type || + link->prog->expected_attach_type != new_prog->expected_attach_type || + link->prog->aux->attach_btf_id != new_prog->aux->attach_btf_id) { + ret = -EINVAL ; + goto out_unlock; + } + + old_prog =xchg (&link->prog, new_prog); +bpf_prog_put (old_prog); + +out_unlock: +mutex_unlock (&link_mutex); +return ret; +} + +static const struct bpf_link_ops bpf_map_trace_link_ops = { + .release = bpf_map_trace_link_release, + .dealloc = bpf_map_trace_link_dealloc, + .update_prog = bpf_map_trace_link_replace, +}; +
RFC PATCH v2 5/13 bpf: Enable creation of BPF_LINK_TYPE_MAP_TRACE
In reply to Joe BurtonFrom: Joe Burton <[email protected]>
Add new link type, BPF_LINK_TYPE_MAP_TRACE. This link attaches map tracing programs to maps. At attachment time, the program's read-write accesses are verified against the map's key/value size.
--- include/linux/bpf.h | 2 + include/linux/bpf_types.h | 1 + include/uapi/linux/bpf.h | 12 ++++ kernel/bpf/map_trace.c | 147 ++++++++++++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 3 + 5 files changed, 165 insertions(+)
include/linux/bpf.h +2 -0
@@ -1557,6 +1557,8 @@ const struct bpf_func_proto * bpf_iter_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);bool bpf_map_trace_prog_supported (struct bpf_prog *prog);int bpf_map_trace_reg_target (const struct bpf_map_trace_reg *reg_info); +int bpf_map_trace_link_attach (const union bpf_attr *attr,bpfptr_t uattr, +struct bpf_prog *prog);int bpf_iter_link_attach (const union bpf_attr *attr,bpfptr_t uattr,struct bpf_prog *prog);int bpf_iter_new_fd (struct bpf_link *link);bool bpf_link_is_iter (struct bpf_link *link);
include/linux/bpf_types.h +1 -0
@@ -139,3 +139,4 @@ BPF_LINK_TYPE(BPF_LINK_TYPE_XDP, xdp)#ifdef CONFIG_PERF_EVENTS BPF_LINK_TYPE(BPF_LINK_TYPE_PERF_EVENT, perf)#endif +BPF_LINK_TYPE (BPF_LINK_TYPE_MAP_TRACE , map_trace)
include/uapi/linux/bpf.h +12 -0
@@ -100,6 +100,11 @@ enum bpf_map_trace_type { MAX_BPF_MAP_TRACE_TYPE, }; +struct bpf_map_trace_link_info { +__u32 map_fd; +enum bpf_map_trace_type trace_type; +}; +#define BPF_MAP_TRACE_FUNC (trace_type) "bpf_map_trace__" #trace_type/* BPF syscall commands, see bpf(2) man-page for more details. */
@@ -1018,6 +1023,7 @@ enum bpf_link_type {BPF_LINK_TYPE_NETNS =5 ,BPF_LINK_TYPE_XDP =6 ,BPF_LINK_TYPE_PERF_EVENT =7 , +BPF_LINK_TYPE_MAP_TRACE =8 , MAX_BPF_LINK_TYPE, };
@@ -1465,6 +1471,12 @@ union bpf_attr {*/ __u64 bpf_cookie; } perf_event; +struct { +/* extra bpf_map_trace_link_info */ +__aligned_u64 map_trace_info; +/* map_trace_info length */ +__u32 map_trace_info_len; + }; }; } link_create;
kernel/bpf/map_trace.c +147 -0
150 lines not on this page · raw message
kernel/bpf/syscall.c +3 -0
@@ -3136,6 +3136,7 @@ attach_type_to_prog_type(enum bpf_attach_type attach_type)case BPF_CGROUP_SETSOCKOPT :return BPF_PROG_TYPE_CGROUP_SOCKOPT ;case BPF_TRACE_ITER : +case BPF_TRACE_MAP :return BPF_PROG_TYPE_TRACING ;case BPF_SK_LOOKUP :return BPF_PROG_TYPE_SK_LOOKUP ;
@@ -4192,6 +4193,8 @@ static int tracing_bpf_link_attach(const union bpf_attr *attr, bpfptr_t uattr,if (prog->expected_attach_type ==BPF_TRACE_ITER )return bpf_iter_link_attach (attr, uattr, prog); +else if (prog->expected_attach_type ==BPF_TRACE_MAP ) +return bpf_map_trace_link_attach (attr, uattr, prog);else if (prog->type ==BPF_PROG_TYPE_EXT ) return bpf_tracing_prog_attach(prog, attr->link_create.target_fd,
Signature · 1 line
2.33.0.685.g46640cef36-goog
RFC PATCH v2 6/13 bpf: Add APIs to invoke tracing programs
In reply to Joe BurtonFrom: Joe Burton <[email protected]>
The macros BPF_TRACE_MAP_{K,KV} provide a convenient way to invoke tracing programs. Maps will register themselves with a late_initcall() then use these macros to invoke tracing programs.
--- include/linux/bpf.h | 13 +++++++++++++ kernel/bpf/map_trace.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+)
include/linux/bpf.h +13 -0
@@ -1532,6 +1532,16 @@ struct bpf_map_trace_progs {struct mutex mutex;/* protects writes to progs, length */ }; +struct bpf_map_trace_ctx__update_elem { + __bpf_md_ptr(void *, key); + __bpf_md_ptr(void *, value); +u64 flags; +}; + +struct bpf_map_trace_ctx__delete_elem { + __bpf_md_ptr(void *, key); +}; +struct bpf_map_trace_reg {const char *target;enum bpf_map_trace_type trace_type;
@@ -1560,6 +1570,9 @@ int bpf_map_trace_reg_target(const struct bpf_map_trace_reg *reg_info);int bpf_map_trace_link_attach (const union bpf_attr *attr,bpfptr_t uattr,struct bpf_prog *prog);int bpf_iter_link_attach (const union bpf_attr *attr,bpfptr_t uattr,struct bpf_prog *prog); +void bpf_trace_map_update_elem (struct bpf_map *map,void *key,void *value, +u64 flags); +void bpf_trace_map_delete_elem (struct bpf_map *map,void *key);int bpf_iter_new_fd (struct bpf_link *link);bool bpf_link_is_iter (struct bpf_link *link);struct bpf_prog *bpf_iter_get_info (struct bpf_iter_meta *meta,bool in_stop);
kernel/bpf/map_trace.c +40 -0
@@ -295,3 +295,43 @@ int bpf_map_trace_link_attach(const union bpf_attr *attr, bpfptr_t uattr,return err; } +static void bpf_map_trace_run_progs (struct bpf_map *map, +enum bpf_map_trace_type trace_type, +void *ctx) +{ +struct bpf_map_trace_prog *prog; + +if (!map->trace_progs) +return ; + +rcu_read_lock (); +migrate_disable (); +list_for_each_entry_rcu (prog, + &map->trace_progs->progs[trace_type].list, + list) { +bpf_prog_run (prog->prog, ctx);/* return code is ignored */ + } +migrate_enable (); +rcu_read_unlock (); +} + +void bpf_trace_map_update_elem (struct bpf_map *map, +void *key,void *value,u64 flags) +{ +struct bpf_map_trace_ctx__update_elem ctx; + + ctx.key = key; + ctx.value = value; + ctx.flags = flags; +bpf_map_trace_run_progs (map,BPF_MAP_TRACE_UPDATE_ELEM , &ctx); +} + +void bpf_trace_map_delete_elem (struct bpf_map *map, +void *key) +{ +struct bpf_map_trace_ctx__delete_elem ctx; + + ctx.key = key; +bpf_map_trace_run_progs (map,BPF_MAP_TRACE_DELETE_ELEM , &ctx); +} +
Signature · 1 line
2.33.0.685.g46640cef36-goog
RFC PATCH v2 7/13 bpf: Register BPF_MAP_TRACE_{UPDATE,DELETE}_ELEM hooks
In reply to Joe BurtonFrom: Joe Burton <[email protected]>
Tracing programs may now load with this hook. There is not yet a way to invoke those programs.
--- kernel/bpf/map_trace.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
kernel/bpf/map_trace.c +23 -0
@@ -335,3 +335,26 @@ void bpf_trace_map_delete_elem(struct bpf_map *map,bpf_map_trace_run_progs (map,BPF_MAP_TRACE_DELETE_ELEM , &ctx); } +DEFINE_BPF_MAP_TRACE_FUNC(BPF_MAP_TRACE_UPDATE_ELEM, void *key, void *value, u64 flags); +DEFINE_BPF_MAP_TRACE_FUNC (BPF_MAP_TRACE_DELETE_ELEM , void *key); + +static struct bpf_map_trace_reg map_trace_update_elem_reg_info = { + .target =BPF_MAP_TRACE_FUNC (BPF_MAP_TRACE_UPDATE_ELEM ), + .trace_type =BPF_MAP_TRACE_UPDATE_ELEM , +}; +static struct bpf_map_trace_reg map_trace_delete_elem_reg_info = { + .target =BPF_MAP_TRACE_FUNC (BPF_MAP_TRACE_DELETE_ELEM ), + .trace_type =BPF_MAP_TRACE_DELETE_ELEM , +}; + +static int __init bpf_map_trace_init (void ) +{ +int err =bpf_map_trace_reg_target (&map_trace_update_elem_reg_info); + + err = (err ? err : +bpf_map_trace_reg_target (&map_trace_delete_elem_reg_info)); +return err; +} + +late_initcall (bpf_map_trace_init); +
Signature · 1 line
2.33.0.685.g46640cef36-goog
RFC PATCH v2 8/13 libbpf: Support BPF_TRACE_MAP
In reply to Joe BurtonFrom: Joe Burton <[email protected]>
Programs may be now loaded with libbpf. Sections with the prefix "map_trace/" will use the new attach type.
--- tools/include/uapi/linux/bpf.h | 22 ++++++ tools/lib/bpf/bpf.c | 13 ++-- tools/lib/bpf/bpf.h | 4 +- tools/lib/bpf/libbpf.c | 118 +++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 11 +++ tools/lib/bpf/libbpf.map | 1 + 6 files changed, 163 insertions(+), 6 deletions(-)
tools/include/uapi/linux/bpf.h +22 -0
@@ -93,6 +93,20 @@ union bpf_iter_link_info { } map; }; +enum bpf_map_trace_type { +BPF_MAP_TRACE_UPDATE_ELEM =0 , +BPF_MAP_TRACE_DELETE_ELEM =1 , + +MAX_BPF_MAP_TRACE_TYPE , +}; + +struct bpf_map_trace_link_info { +__u32 map_fd; +enum bpf_map_trace_type trace_type; +}; + +#define BPF_MAP_TRACE_FUNC (trace_type) "bpf_map_trace__" #trace_type +/* BPF syscall commands, see bpf(2) man-page for more details. */ /** * DOC: eBPF Syscall Preamble
@@ -994,6 +1008,7 @@ enum bpf_attach_type {BPF_SK_REUSEPORT_SELECT ,BPF_SK_REUSEPORT_SELECT_OR_MIGRATE ,BPF_PERF_EVENT , +BPF_TRACE_MAP , __MAX_BPF_ATTACH_TYPE };
@@ -1008,6 +1023,7 @@ enum bpf_link_type {BPF_LINK_TYPE_NETNS =5 ,BPF_LINK_TYPE_XDP =6 ,BPF_LINK_TYPE_PERF_EVENT =7 , +BPF_LINK_TYPE_MAP_TRACE =8 , MAX_BPF_LINK_TYPE, };
@@ -1455,6 +1471,12 @@ union bpf_attr {*/ __u64 bpf_cookie; } perf_event; +struct { +/* extra bpf_map_trace_link_info */ +__aligned_u64 map_trace_info; +/* map_trace_info length */ +__u32 map_trace_info_len; + }; }; } link_create;
tools/lib/bpf/bpf.c +8 -5
@@ -674,7 +674,7 @@ int bpf_link_create(int prog_fd, int target_fd,enum bpf_attach_type attach_type, const struct bpf_link_create_opts *opts) { - __u32 target_btf_id, iter_info_len; + __u32 target_btf_id, iter_info_len, map_trace_info_len;union bpf_attr attr;int fd;
@@ -682,13 +682,12 @@ int bpf_link_create(int prog_fd, int target_fd,return libbpf_err (-EINVAL ); iter_info_len =OPTS_GET (opts, iter_info_len,0 ); + map_trace_info_len =OPTS_GET (opts, map_trace_info_len,0 ); target_btf_id =OPTS_GET (opts, target_btf_id,0 );/* validate we don't have unexpected combinations of non-zero fields */ -if (iter_info_len || target_btf_id) { -if (iter_info_len && target_btf_id) -return libbpf_err (-EINVAL ); -if (!OPTS_ZEROED (opts, target_btf_id)) +if (iter_info_len || map_trace_info_len || target_btf_id) { +if ((iter_info_len || map_trace_info_len) && target_btf_id)return libbpf_err (-EINVAL ); }
@@ -713,6 +712,10 @@ int bpf_link_create(int prog_fd, int target_fd,if (!OPTS_ZEROED (opts, perf_event))return libbpf_err (-EINVAL );break ; +case BPF_TRACE_MAP : + attr.link_create.map_trace_info =ptr_to_u64 (OPTS_GET (opts, map_trace_info, (void *)0 )); + attr.link_create.map_trace_info_len = map_trace_info_len; +break ;default :if (!OPTS_ZEROED (opts, flags))return libbpf_err (-EINVAL );
tools/lib/bpf/bpf.h +3 -1
@@ -183,8 +183,10 @@ struct bpf_link_create_opts { } perf_event; }; size_t :0; +struct bpf_map_trace_link_info *map_trace_info; +__u32 map_trace_info_len; }; -#define bpf_link_create_opts__last_field perf_event +#define bpf_link_create_opts__last_field map_trace_info_len LIBBPF_API int bpf_link_create(int prog_fd, int target_fd, enum bpf_attach_type attach_type,
tools/lib/bpf/libbpf.c +118 -0
154 lines not on this page · raw message
tools/lib/bpf/libbpf.h +11 -0
@@ -358,6 +358,17 @@ LIBBPF_API struct bpf_link * bpf_program__attach_iter(const struct bpf_program *prog, const struct bpf_iter_attach_opts *opts); +struct bpf_map_trace_attach_opts { +size_t sz;/* size of this struct for forward/backward compatibility */ +struct bpf_map_trace_link_info *link_info; +__u32 link_info_len; +}; +#define bpf_map_trace_attach_opts__last_field link_info_len + +LIBBPF_API struct bpf_link * +bpf_program__attach_map_trace (const struct bpf_program *prog, +const struct bpf_map_trace_attach_opts *opts); +struct bpf_insn ;/*
tools/lib/bpf/libbpf.map +1 -0
@@ -384,6 +384,7 @@ LIBBPF_0.5.0 { btf__load_module_btf; btf__load_vmlinux_btf; btf_dump__dump_type_data; + bpf_program__attach_map_trace; libbpf_set_strict_mode; } LIBBPF_0.4.0;
Signature · 1 line
2.33.0.685.g46640cef36-goog
RFC PATCH v2 9/13 bpf: Add infinite loop check on map tracers
In reply to Joe BurtonFrom: Joe Burton <[email protected]>
Prevent programs from being attached to a map if that attachment could cause an infinite loop. A simple example: a program updates the same map that it's tracing. A map update would cause the program to run, which would cause another update. A more complex example: an update to map M0 triggers tracer P0. P0 updates map M1. M1 is being traced by tracer T1. T1 updates M0.
We prevent this situation by enforcing that all programs "reachable" from a given map do not include the proposed tracer.
--- kernel/bpf/map_trace.c | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)
kernel/bpf/map_trace.c +46 -0
@@ -148,6 +148,48 @@ static const struct bpf_link_ops bpf_map_trace_link_ops = { .update_prog = bpf_map_trace_link_replace, }; +/* Determine whether attaching "prog" to "map" would create an infinite loop. +* If "prog" updates "map", then running "prog" again on a map update would +* loop. +*/ +static int bpf_map_trace_would_loop (struct bpf_prog *prog, +struct bpf_map *map) +{ +struct bpf_map_trace_prog *item; +struct bpf_prog_aux *aux; +struct bpf_map *aux_map; +int i, j, err =0 ; + + aux = prog->aux; +if (!aux) +return 0 ; +mutex_lock (&aux->used_maps_mutex); +for (i =0 ; i < aux->used_map_cnt && !err; i++) { + aux_map = aux->used_maps[i]; +if (aux_map == map) { + err = -EINVAL ; +break ; + } +for (j =0 ; j <MAX_BPF_MAP_TRACE_TYPE && !err; j++) { +if (!aux_map->trace_progs) +continue ; +rcu_read_lock (); +list_for_each_entry_rcu (item, + &aux_map->trace_progs->progs[i].list, + list) { + err =bpf_map_trace_would_loop ( + item->prog, map); +if (err) +break ; + } +rcu_read_unlock (); + } + } +mutex_unlock (&prog->aux->used_maps_mutex); +return err; +} + +int bpf_map_attach_trace (struct bpf_prog *prog,struct bpf_map *map,struct bpf_map_trace_link_info *linfo)
@@ -180,6 +222,10 @@ int bpf_map_attach_trace(struct bpf_prog *prog, goto put_map; } + err =bpf_map_trace_would_loop (prog, map); +if (err) + goto put_map; + trace_prog =kmalloc (sizeof (*trace_prog),GFP_KERNEL );if (!trace_prog) { err = -ENOMEM ;
Signature · 1 line
2.33.0.685.g46640cef36-goog
RFC PATCH v2 10/13 Add bpf_map_trace_{update,delete}_elem() helper functions
In reply to Joe BurtonFrom: Joe Burton <[email protected]>
These helpers invoke tracing programs attached to a given map.
--- include/linux/bpf.h | 2 ++ kernel/bpf/helpers.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
include/linux/bpf.h +2 -0
@@ -2148,6 +2148,8 @@ extern const struct bpf_func_proto bpf_for_each_map_elem_proto;extern const struct bpf_func_proto bpf_btf_find_by_name_kind_proto;extern const struct bpf_func_proto bpf_sk_setsockopt_proto;extern const struct bpf_func_proto bpf_sk_getsockopt_proto; +extern const struct bpf_func_proto bpf_map_trace_update_elem_proto; +extern const struct bpf_func_proto bpf_map_trace_delete_elem_proto;const struct bpf_func_proto *tracing_prog_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog);
kernel/bpf/helpers.c +29 -0
@@ -1328,6 +1328,35 @@ void bpf_timer_cancel_and_free(void *val)kfree (t); } +BPF_CALL_4(bpf_map_trace_update_elem, struct bpf_map *, map, + void *, key, void *, value, u64, flags) +{ +bpf_trace_map_update_elem (map, key, value, flags); +return 0 ; +} + +const struct bpf_func_proto bpf_map_trace_update_elem_proto = { + .func = bpf_map_trace_update_elem, + .ret_type =RET_VOID , + .arg1_type =ARG_CONST_MAP_PTR , + .arg2_type =ARG_PTR_TO_MAP_KEY , + .arg3_type =ARG_PTR_TO_MAP_VALUE , + .arg4_type =ARG_ANYTHING , +}; + +BPF_CALL_2(bpf_map_trace_delete_elem, struct bpf_map *, map, void *, key) +{ +bpf_trace_map_delete_elem (map, key); +return 0 ; +} + +const struct bpf_func_proto bpf_map_trace_delete_elem_proto = { + .func = bpf_map_trace_delete_elem, + .ret_type =RET_VOID , + .arg1_type =ARG_CONST_MAP_PTR , + .arg2_type =ARG_PTR_TO_MAP_KEY , +}; +const struct bpf_func_proto bpf_get_current_task_proto__weak ;const struct bpf_func_proto bpf_get_current_task_btf_proto__weak ;const struct bpf_func_proto bpf_probe_read_user_proto__weak ;
Signature · 1 line
2.33.0.685.g46640cef36-goog
RFC PATCH v2 11/13 bpf: verifier inserts map tracing helper call
In reply to Joe BurtonFrom: Joe Burton <[email protected]>
The verifier will automatically insert a map tracing helper call after each bpf_map_{update,delete}_elem(). Registers are preserved appropriately to make this transparent to applications.
--- kernel/bpf/verifier.c | 75 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 5 deletions(-)
kernel/bpf/verifier.c +70 -5
@@ -12678,6 +12678,48 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env,return 0 ; } +int get_map_tracing_patchlet ( +void *map_func, +void *map_trace_func, +const int nregs, +struct bpf_prog *prog, +struct bpf_insn *insn_buf, +int *extra_stack) +{ +const int stack_offset =-1 * (int16_t ) prog->aux->stack_depth; +const int reg_size_bytes =8 ; +int cnt =0 , i; + +/* push args onto the stack so that we can invoke the tracer after */ +for (i =0 ; i < nregs; i++) + insn_buf[cnt++] =BPF_STX_MEM ( +BPF_DW ,BPF_REG_FP , +BPF_REG_1 + i, + stack_offset - (i +1 ) * reg_size_bytes); + + insn_buf[cnt++] =BPF_EMIT_CALL (map_func); + +for (i =0 ; i < nregs; i++) + insn_buf[cnt++] =BPF_LDX_MEM ( +BPF_DW ,BPF_REG_1 + i, +BPF_REG_FP , + stack_offset - (i +1 ) * reg_size_bytes); + +/* save return code from map update */ + insn_buf[cnt++] =BPF_STX_MEM (BPF_DW ,BPF_REG_FP ,BPF_REG_0 , + stack_offset - reg_size_bytes); + +/* invoke tracing helper */ + insn_buf[cnt++] =BPF_EMIT_CALL (map_trace_func); + +/* restore return code from map update */ + insn_buf[cnt++] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_FP , + stack_offset - reg_size_bytes); + + *extra_stack =max_t (int, *extra_stack, nregs * reg_size_bytes); +return cnt; +} +/* Do various post-verification rewrites in a single program pass. * These rewrites simplify JIT and interpreter implementations. */
@@ -12694,7 +12736,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env)struct bpf_insn insn_buf[16 ];struct bpf_prog *new_prog;struct bpf_map *map_ptr; -int i, ret, cnt, delta =0 ; +int i, ret, cnt, delta =0 , extra_stack =0 ;for (i =0 ; i < insn_cnt; i++, insn++) {/* Make divide-by-zero exceptions impossible. */
@@ -12998,11 +13040,23 @@ static int do_misc_fixups(struct bpf_verifier_env *env) insn->imm =BPF_CALL_IMM (ops->map_lookup_elem);continue ;case BPF_FUNC_map_update_elem: - insn->imm =BPF_CALL_IMM (ops->map_update_elem); -continue ; + cnt =get_map_tracing_patchlet ( + ops->map_update_elem, + bpf_trace_map_update_elem, +/*nregs=*/ 4 , prog, insn_buf, + &extra_stack); +if (cnt <0 ) +return cnt; + goto patch_map_ops_tracing;case BPF_FUNC_map_delete_elem: - insn->imm =BPF_CALL_IMM (ops->map_delete_elem); -continue ; + cnt =get_map_tracing_patchlet ( + ops->map_delete_elem, + bpf_trace_map_delete_elem, +/*nregs=*/ 2 , prog, insn_buf, + &extra_stack); +if (cnt <0 ) +return cnt; + goto patch_map_ops_tracing;case BPF_FUNC_map_push_elem: insn->imm =BPF_CALL_IMM (ops->map_push_elem);continue ;
@@ -13018,6 +13072,16 @@ static int do_misc_fixups(struct bpf_verifier_env *env) } goto patch_call_imm; +patch_map_ops_tracing: + new_prog =bpf_patch_insn_data (env, i + delta, + insn_buf, cnt); +if (!new_prog) +return -ENOMEM ; + + delta += cnt -1 ; + env->prog = prog = new_prog; + insn = new_prog->insnsi + i + delta; +continue ; }/* Implement bpf_jiffies64 inline. */
@@ -13092,6 +13156,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env) }sort_kfunc_descs_by_imm (env->prog); + prog->aux->stack_depth += extra_stack;return 0 ; }
Signature · 1 line
2.33.0.685.g46640cef36-goog
RFC PATCH v2 12/13 bpf: Add selftests for map tracing
In reply to Joe BurtonFrom: Joe Burton <[email protected]>
Add selftests for intended usage and infinite loop detection.
--- .../selftests/bpf/prog_tests/bpf_map_trace.c | 144 ++++++++++++++++++ .../bpf/progs/bpf_map_trace_delete_elem.c | 49 ++++++ .../selftests/bpf/progs/bpf_map_trace_loop0.c | 26 ++++ .../selftests/bpf/progs/bpf_map_trace_loop1.c | 43 ++++++ .../bpf/progs/bpf_map_trace_update_elem.c | 51 +++++++ .../selftests/bpf/verifier/map_trace.c | 40 +++++ 6 files changed, 353 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_map_trace.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_map_trace_delete_elem.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_map_trace_loop0.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_map_trace_loop1.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_map_trace_update_elem.c create mode 100644 tools/testing/selftests/bpf/verifier/map_trace.c
tools/testing/selftests/bpf/prog_tests/bpf_map_trace.c new file +144 -0
@@ -0,0 +1,144 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Google */ +#include <test_progs.h> + +#include <assert.h> +#include <asm/unistd.h> +#include <errno.h> +#include <fcntl.h> +#include <bpf/bpf.h> +#include <bpf/libbpf.h> +#include <linux/bpf.h> +#include <sys/stat.h> +#include <sys/mount.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include "bpf_map_trace_delete_elem.skel.h" +#include "bpf_map_trace_loop0.skel.h" +#include "bpf_map_trace_loop1.skel.h" +#include "bpf_map_trace_update_elem.skel.h" + +uint32_t collatz (uint32_t x) +{ +return x %2 ? x *3 +1 : x /2 ; +} + +void update_elem__basic (void ) +{ +const uint32_t tracer_value =collatz (0xdeadbeef ); +struct bpf_map_trace_update_elem *skel; +const uint32_t tracer_key =0x5 ; +uint32_t value; +int rc; + + skel =bpf_map_trace_update_elem__open_and_load (); +if (!ASSERT_NEQ (skel,NULL ,"open/load skeleton failure" )) +return ; + rc =bpf_map_trace_update_elem__attach (skel); +if (!ASSERT_EQ (rc,0 ,"attach skeleton failure" )) { +fprintf (stderr,"Failed to attach skeleton: %d\n" , errno); + goto out; + } + +/* The kprobe will place (0x5, 0xdeadbeef) in its map. The tracer will +* place (0x5, collatz(0xdeadbeef)) in its map. This map lookup will +* trigger the kprobe. +*/ + rc =bpf_map_lookup_elem (bpf_map__fd (skel->maps.tracer_map), + &tracer_key, &value); +if (!ASSERT_EQ (rc,0 ,"map lookup failure" )) { +fprintf (stderr,"Failed to lookup tracer map: %s\n" , +strerror (errno)); + goto out; + } +if (!ASSERT_EQ (value, tracer_value,"map lookup mismatch" )) + goto out; + +out: +bpf_map_trace_update_elem__destroy (skel); +} + +void delete_elem__basic (void ) +{ +const uint32_t tracer_key =collatz (0x5 ); +struct bpf_map_trace_delete_elem *skel; +uint32_t value =0 ; +int rc; + + skel =bpf_map_trace_delete_elem__open_and_load (); +if (!ASSERT_NEQ (skel,NULL ,"open/load skeleton failure" )) +return ; + rc =bpf_map_trace_delete_elem__attach (skel); +if (!ASSERT_EQ (rc,0 ,"attach skeleton failure" )) { +fprintf (stderr,"Failed to attach skeleton: %d\n" , errno); + goto out; + } + +/* The kprobe will delete (0x5) from its map. The tracer will +* place (collatz(0x5), pid) in its map. This map lookup will trigger +* the kprobe. +*/ + rc =bpf_map_lookup_elem (bpf_map__fd (skel->maps.tracer_map), + &tracer_key, &value); +if (!ASSERT_EQ (rc,0 ,"map lookup failure" )) { +fprintf (stderr,"Failed to lookup tracer map: %s\n" , +strerror (errno)); + goto out; + } +if (!ASSERT_EQ (value,getpid (),"map lookup mismatch" )) + goto out; + +out: +bpf_map_trace_delete_elem__destroy (skel); +} + +void infinite_loop__direct (void ) +{ +struct bpf_map_trace_loop0 *skel; +struct bpf_link *tracer_link; + + skel =bpf_map_trace_loop0__open_and_load (); +if (!ASSERT_NEQ (skel,NULL ,"open/load skeleton failure" )) + goto out; + tracer_link =bpf_program__attach (skel->progs.tracer); +if (!ASSERT_ERR_PTR (tracer_link,"link creation success" )) + goto out; + +out: +bpf_map_trace_loop0__destroy (skel); +} + +void infinite_loop__indirect (void ) +{ +struct bpf_map_trace_loop1 *skel; +struct bpf_link *tracer_link; + + skel =bpf_map_trace_loop1__open_and_load (); +if (!ASSERT_NEQ (skel,NULL ,"open/load skeleton failure" )) +return ; + tracer_link =bpf_program__attach (skel->progs.tracer0); +if (!ASSERT_OK_PTR (tracer_link,"link creation failure" )) + goto out; + tracer_link =bpf_program__attach (skel->progs.tracer1); +if (!ASSERT_ERR_PTR (tracer_link,"link creation success" )) + goto out; + +out: +bpf_map_trace_loop1__destroy (skel); +} + +void test_bpf_map_trace (void ) +{ +if (test__start_subtest ("update_elem__basic" )) +update_elem__basic (); +if (test__start_subtest ("delete_elem__basic" )) +delete_elem__basic (); +if (test__start_subtest ("infinite_loop__direct" )) +infinite_loop__direct (); +if (test__start_subtest ("infinite_loop__indirect" )) +infinite_loop__indirect (); +} +
tools/testing/selftests/bpf/progs/bpf_map_trace_delete_elem.c new file +49 -0
49 lines not on this page · raw message
tools/testing/selftests/bpf/progs/bpf_map_trace_loop0.c new file +26 -0
@@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Google */ +#include "vmlinux.h" + +#include <bpf/bpf_helpers.h> + +struct bpf_map_def SEC("maps") traced_map = { + .type = BPF_MAP_TYPE_HASH, + .key_size = sizeof(uint32_t), + .value_size = sizeof(uint32_t), + .max_entries = 64, +}; + +/* This traces traced_map and updates it, creating an (invalid) infinite loop. +*/ +SEC("map_trace/traced_map/UPDATE_ELEM") +int tracer (struct bpf_map_trace_ctx__update_elem *ctx) +{ +uint32_t key =0 , val =0 ; + +bpf_map_update_elem (&traced_map, &key, &val,/*flags=*/ 0 ); +return 0 ; +} + +char _license[] SEC("license") = "GPL"; +
tools/testing/selftests/bpf/progs/bpf_map_trace_loop1.c new file +43 -0
43 lines not on this page · raw message
tools/testing/selftests/bpf/progs/bpf_map_trace_update_elem.c new file +51 -0
51 lines not on this page · raw message
tools/testing/selftests/bpf/verifier/map_trace.c new file +40 -0
@@ -0,0 +1,40 @@ +{ + "map tracing: full stack is accepted", + .insns = { +BPF_ST_MEM (BPF_DW ,BPF_REG_10 ,-512 ,0 ), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-512 ), +BPF_LD_MAP_FD (BPF_REG_1 ,0 ), +BPF_MOV64_REG (BPF_REG_3 ,BPF_REG_2 ), +BPF_MOV64_IMM (BPF_REG_4 ,0 ), +BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , + BPF_FUNC_map_update_elem), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .fixup_map_hash_8b = { 3 }, + .result_unpriv = ACCEPT, + .result = ACCEPT, + .prog_type = BPF_PROG_TYPE_CGROUP_SKB, +}, +{ +"map tracing: overfull stack is not accepted" , + .insns = { +BPF_ST_MEM (BPF_DW ,BPF_REG_10 ,-520 ,0 ), +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ), +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-520 ), +BPF_LD_MAP_FD (BPF_REG_1 ,0 ), +BPF_MOV64_REG (BPF_REG_3 ,BPF_REG_2 ), +BPF_MOV64_IMM (BPF_REG_4 ,0 ), +BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , + BPF_FUNC_map_update_elem), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .fixup_map_hash_8b = { 3 }, + .result_unpriv = REJECT, + .result = REJECT, + .errstr = "invalid write to stack R10 off=-520 size=8", + .errstr_unpriv = "invalid write to stack R10 off=-520 size=8", + .prog_type = BPF_PROG_TYPE_CGROUP_SKB, +},
Signature · 1 line
2.33.0.685.g46640cef36-goog
RFC PATCH v2 13/13 bpf: Add real world example for map tracing
In reply to Joe BurtonFrom: Joe Burton <[email protected]>
Add an example that demonstrates how map tracing helps us avoid race conditions while upgrading stateful program.
--- .../selftests/bpf/prog_tests/bpf_map_trace.c | 257 ++++++++++++++++++ .../progs/bpf_map_trace_real_world_common.h | 125 +++++++++ .../bpf_map_trace_real_world_migration.c | 96 +++++++ .../bpf/progs/bpf_map_trace_real_world_new.c | 4 + .../bpf/progs/bpf_map_trace_real_world_old.c | 5 + 5 files changed, 487 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/bpf_map_trace_real_world_common.h create mode 100644 tools/testing/selftests/bpf/progs/bpf_map_trace_real_world_migration.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_map_trace_real_world_new.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_map_trace_real_world_old.c
tools/testing/selftests/bpf/prog_tests/bpf_map_trace.c +257 -0
@@ -19,6 +19,9 @@#include "bpf_map_trace_delete_elem.skel.h" #include "bpf_map_trace_loop0.skel.h" #include "bpf_map_trace_loop1.skel.h" +#include "bpf_map_trace_real_world_migration.skel.h" +#include "bpf_map_trace_real_world_new.skel.h" +#include "bpf_map_trace_real_world_old.skel.h" #include "bpf_map_trace_update_elem.skel.h" uint32_t collatz (uint32_t x)
@@ -130,8 +133,262 @@ void infinite_loop__indirect(void)bpf_map_trace_loop1__destroy (skel); } +int real_world_example__attach_migration ( +struct bpf_map_trace_real_world_migration *migration_skel, +struct bpf_link **iter_link, +struct bpf_link **map_trace_link_update, +struct bpf_link **map_trace_link_delete) +{ +union bpf_iter_link_info iter_link_info; +struct bpf_iter_attach_opts iter_opts; +int64_t error; + + *map_trace_link_update =bpf_program__attach ( + migration_skel->progs.copy_on_write__update); + error =libbpf_get_error (map_trace_link_update); +if (!ASSERT_EQ (error,0 , +"copy_on_write update bpf_program__attach failure" )) +return 1 ; + + *map_trace_link_delete =bpf_program__attach ( + migration_skel->progs.copy_on_write__delete); + error =libbpf_get_error (map_trace_link_delete); +if (!ASSERT_EQ (error,0 , +"copy_on_write update bpf_program__delete failure" )) +return 1 ; + +memset (&iter_link_info,0 ,sizeof (iter_link_info)); + iter_link_info.map.map_fd =bpf_map__fd (migration_skel->maps.old_map); + +memset (&iter_opts,0 ,sizeof (iter_opts)); + iter_opts.sz =sizeof (iter_opts); + iter_opts.link_info = &iter_link_info; + iter_opts.link_info_len =sizeof (iter_link_info); + *iter_link =bpf_program__attach_iter ( + migration_skel->progs.bulk_migration, &iter_opts); + error =libbpf_get_error (iter_link); +if (!ASSERT_EQ (error,0 ,"bpf_program__attach_iter failure" )) +return 1 ; + +return 0 ; +} + +int open_and_write_files (const char *path,size_t num_files) +{ +int *fds =malloc (sizeof (int ) * num_files); +ssize_t bytes_written; +const char buf ='a' ; +size_t i, j; +int ret =0 ; + +if (fds ==NULL ) +return 1 ; + +for (i =0 ; i < num_files; i++) { + fds[i] =open (path,O_WRONLY |O_CREAT ); + +if (fds[i] <0 ) { + ret =2 ; +break ; + } + bytes_written =write (fds[i], &buf,sizeof (buf)); +if (bytes_written !=sizeof (buf)) { + ret =3 ; +break ; + } + } +for (j =0 ; j < i; j++) +close (fds[j]); +return ret; +} + +void real_world_example (void ) +{ +struct bpf_map_trace_real_world_migration *migration_skel =NULL ; +int file_fd_should_write =-1 , file_fd_should_not_write =-1 ; +struct bpf_map_trace_real_world_new *new_skel =NULL ; +struct bpf_map_trace_real_world_old *old_skel =NULL ; +struct bpf_link *map_trace_link_update =NULL ; +struct bpf_link *map_trace_link_delete =NULL ; +struct bpf_link *iter_link =NULL ; +const bool enable_filtering =1 ; +const uint32_t pid =getpid (); +uint32_t max_open_files; +char file_buf ='a' ; +int iter_fd =-1 ; +char iter_buf[1 ]; +int rc; + +/* +* Begin by loading and attaching the old version of our program. +*/ + old_skel =bpf_map_trace_real_world_old__open_and_load (); +if (!ASSERT_NEQ (old_skel,NULL ,"open/load old skeleton" )) +return ; + rc =bpf_map_trace_real_world_old__attach (old_skel); +if (!ASSERT_EQ (rc,0 ,"attach old skeleton" )) { +fprintf (stderr,"Failed to attach skeleton: %d\n" , errno); + goto out; + } + rc =bpf_map_update_elem (bpf_map__fd (old_skel->maps.filtered_pids), + &pid, &enable_filtering,/*flags=*/ 0 ); +if (!ASSERT_EQ (rc,0 ,"configure process to be filtered" )) +return ; +if (!ASSERT_EQ (open_and_write_files ("/tmp/tst_file" ,1 ),0 , +"program allows writing a single new file" )) + goto out; + max_open_files =bpf_map__max_entries (old_skel->maps.allow_reads); +if (!ASSERT_NEQ (open_and_write_files ("/tmp/tst_file" , + max_open_files +1 ),0 , +"program blocks writing too many new files" )) + goto out; + +/* +* Then load the new version of the program. +*/ + new_skel =bpf_map_trace_real_world_new__open_and_load (); +if (!ASSERT_NEQ (new_skel,NULL ,"open/load new skeleton" )) + goto out; + +/* +* Hook up the migration programs. This gives the old map +* copy-on-write semantics. +*/ + migration_skel =bpf_map_trace_real_world_migration__open (); +if (!ASSERT_NEQ (migration_skel,NULL ,"open migration skeleton" )) + goto out; + rc =bpf_map__reuse_fd (migration_skel->maps.old_map, +bpf_map__fd (old_skel->maps.allow_reads)); +if (!ASSERT_EQ (rc,0 ,"reuse old map fd" )) + goto out; + rc =bpf_map__reuse_fd (migration_skel->maps.new_map, +bpf_map__fd (new_skel->maps.allow_reads)); +if (!ASSERT_EQ (rc,0 ,"reuse new map fd" )) + goto out; + rc =bpf_map_trace_real_world_migration__load (migration_skel); +if (!ASSERT_EQ (rc,0 ,"load migration skeleton" )) + goto out; + rc =real_world_example__attach_migration (migration_skel, + &iter_link, + &map_trace_link_update, + &map_trace_link_delete); +if (!ASSERT_EQ (rc,0 ,"attach migration programs" )) + goto out; + +/* +* Simulated race condition type 1: An application opens an fd before +* bulk transfer and closes it after. +*/ + file_fd_should_not_write =open ("/tmp/tst_file" ,O_WRONLY |O_CREAT ); +if (!ASSERT_GE (file_fd_should_not_write,0 , +"open file before bulk migration" )) + goto out; + +/* +* Perform bulk transfer. +*/ + iter_fd =bpf_iter_create (bpf_link__fd (iter_link)); +if (!ASSERT_GE (iter_fd,0 ,"create iterator" )) + goto out; + rc =read (iter_fd, &iter_buf,sizeof (iter_buf)); +if (!ASSERT_EQ (rc,0 ,"execute map iterator" )) + goto out; + rc =bpf_map_update_elem (bpf_map__fd (new_skel->maps.filtered_pids), + &pid, &enable_filtering,/*flags=*/ 0 ); +if (!ASSERT_EQ (rc,0 ,"configure process to be filtered" )) + goto out; + +/* +* Simulated race condition type 1 (continued). This close() does not +* propagate to the new map without copy-on-write semantics, so it +* would occupy a spot in the map until our app happens to close an fd +* with the same number. This would subtly degrade the contract with +* the application. +*/ +close (file_fd_should_not_write); + file_fd_should_not_write =-1 ; + +/* +* Simulated race condition type 2: An application opens a file +* descriptor after bulk transfer. This openat() does not propagate to +* the new map without copy-on-write, so our app would not be able to +* write to it. +*/ + file_fd_should_write =open ("/tmp/tst_file" ,O_WRONLY |O_CREAT ); +if (!ASSERT_GE (file_fd_should_write,0 , +"open file after bulk migration" )) + goto out; + +/* +* State is migrated. Load new programs. +*/ + rc =bpf_map_trace_real_world_new__attach (new_skel); +if (!ASSERT_EQ (rc,0 ,"failed to attach new programs" )) + goto out; + +/* +* Unload migration progs. +*/ +close (iter_fd); + iter_fd =-1 ; +bpf_link__destroy (map_trace_link_update); + map_trace_link_update =NULL ; +bpf_link__destroy (map_trace_link_delete); + map_trace_link_delete =NULL ; +bpf_link__destroy (iter_link); + iter_link =NULL ; +bpf_map_trace_real_world_migration__destroy (migration_skel); + migration_skel =NULL ; + +/* +* Unload old programs. +*/ +bpf_map_trace_real_world_old__destroy (old_skel); + old_skel =NULL ; + +if (!ASSERT_EQ (open_and_write_files ("/tmp/tst_file" ,1 ),0 , +"program allows writing a single new file" )) + goto out; + max_open_files =bpf_map__max_entries (new_skel->maps.allow_reads); +if (!ASSERT_NEQ (open_and_write_files ("/tmp/tst_file" , + max_open_files +1 ),0 , +"program blocks writing too many new files" )) + goto out; +/* +* Simulated race condition type 2 (continued): If we didn't do +* copy-on-write, this would be expected to fail, since the FD would +* not be in the new map. +*/ + rc =write (file_fd_should_write, &file_buf,sizeof (file_buf)); +if (!ASSERT_EQ (rc,sizeof (file_buf), +"migrated program allows writing to file opened before migration" )) + goto out; + +out: +if (old_skel) +bpf_map_trace_real_world_old__destroy (old_skel); +if (new_skel) +bpf_map_trace_real_world_new__destroy (new_skel); +if (migration_skel) +bpf_map_trace_real_world_migration__destroy (migration_skel); +if (map_trace_link_update) +bpf_link__destroy (map_trace_link_update); +if (map_trace_link_delete) +bpf_link__destroy (map_trace_link_delete); +if (iter_link) +bpf_link__destroy (iter_link); +if (iter_fd >-1 ) +close (iter_fd); +if (file_fd_should_write >-1 ) +close (file_fd_should_write); +if (file_fd_should_not_write >-1 ) +close (file_fd_should_not_write); +} +void test_bpf_map_trace (void ) { +if (test__start_subtest ("real_world_example" )) +real_world_example ();if (test__start_subtest ("update_elem__basic" ))update_elem__basic ();if (test__start_subtest ("delete_elem__basic" ))
tools/testing/selftests/bpf/progs/bpf_map_trace_real_world_common.h new file +125 -0
125 lines not on this page · raw message
tools/testing/selftests/bpf/progs/bpf_map_trace_real_world_migration.c new file +96 -0
96 lines not on this page · raw message
tools/testing/selftests/bpf/progs/bpf_map_trace_real_world_new.c new file +4 -0
@@ -0,0 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Google */ +#include "bpf_map_trace_real_world_common.h" +
tools/testing/selftests/bpf/progs/bpf_map_trace_real_world_old.c new file +5 -0
@@ -0,0 +1,5 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Google */ +#define OLD_VERSION +#include "bpf_map_trace_real_world_common.h" +
Signature · 1 line
2.33.0.685.g46640cef36-goog
Quoted 13 lines · Joe Burton
From: Joe Burton <[email protected]>
This patch introduces 'map tracing': the capability to execute a tracing program after updating a map.
Map tracing enables upgrades of stateful programs with fewer race conditions than otherwise possible. We use a tracing program to imbue a map with copy-on-write semantics, then use an iterator to perform a bulk copy of data in the map. After bulk copying concludes, updates to that map automatically propagate via the tracing program, avoiding a class of race conditions. This use case is demonstrated in the new '
real_world_example' selftest.
Quoted 7 lines
Extend
BPF_PROG_TYPE_TRACINGwith a new attach type,BPF_TRACE_MAP, and allow linking these programs to arbitrary maps.Extend the verifier to invoke helper calls directly after
bpf_map_update_elem()andbpf_map_delete_elem(). The helpers have the exact same signature as the functions they trace, and simply pass those arguments to the list of tracing programs attached to the map.
It's a neat idea to user verifier powers for this job, but I wonder why simple tracepoint in map ops was not used instead? With BTF the bpf progs see the actual types of raw tracepoints. If tracepoint has map, key, value pointers the prog will be able to access them in read-only mode.
Such map pointer will be PTR_TO_BTF_ID, so the prog won't be able to recursively do lookup/update on this map pointer,
but that's what you need anyway, right?
If not we can extend this part of the tracepoint/verifier.
Instead of tracepoint it could have been an empty noinline function and fentry/fexit would see all arguments as well.
Quoted 22 lines
One open question is how to handle pointer-based map updates. For example:
int *x =bpf_map_lookup_elem(...);
if (...) *x++;
if (...) *x--;
We can't just call a helper function right after thebpf_map_lookup_elem(), since the updates occur later on. We also can't determine where the last modification to the pointer occurs, due to branch instructions. I would therefore consider a pattern where we 'flush' pointers at the end of a BPF program:
int *x =bpf_map_lookup_elem(...);
...
/* Execute tracing programs for this cell in this map. */bpf_map_trace_pointer_update(x);
return 0;
We can't necessarily do this in the verifier, since 'x' may no longer be in a register or on the stack. Thus we might introduce a helper to save pointers that should be flushed, then flush all registered pointers at every exit point:
int *x =bpf_map_lookup_elem(...);
/*
* Saves 'x' somewhere in kernel memory. Does nothing if no
Quoted 8 lines
* corresponding tracing progs are attached to the map. */
bpf_map_trace_register_pointer(x);
...
/* flush all registered pointers */bpf_map_trace_pointer_update();
return 0;
This should be easy to implement in the verifier.
I don't think the "solution" for lookup operation is worth pursuing. The bpf prog that needs this map tracing is completely in your control. So just don't do writes after lookup.
Quoted 4 lines
In addition, we use the verifier to instrument certain map update calls. This requires saving arguments onto the stack, which means that a program using
MAX_BPF_STACKbytes of stack could exceed the limit. I don't know whether this actually causes any problems.
Extra 8*4 bytes of stack is not a deal breaker.
Quoted 2 lines
It's a neat idea to user verifier powers for this job, but I wonder why simple tracepoint in map ops was not used instead?
My concern with tracepoints is that they execute for all map updates, not for a particular map. Ideally performing an upgrade of program X should not affect the performance characteristics of program Y.
If n programs are opted into this model, then upgrading any of them affects the performance characteristics of every other. There's also the (very remote) possibility of multiple simultaneous upgrades tracing map updates at the same time, causing a greater performance hit.
Quoted 3 lines
I don't think the "solution" for lookup operation is worth pursuing. The bpf prog that needs this map tracing is completely in your control. So just don't do writes after lookup.
I eventually want to support apps that use local storage. Those APIs generally only allow updates via a pointer. E.g. bpf_sk_storage_get() only allows updates via the returned pointer and via bpf_sk_storage_delete().
Since I eventually have to solve this problem to handle local storage, then it seems worth solving it for normal maps as well. They seem like isomorphic problems.
Quoted 77 lines · Alexei Starovoitov
Quoted 13 lines · Joe Burton
From: Joe Burton <[email protected]>
This patch introduces 'map tracing': the capability to execute a tracing program after updating a map.
Map tracing enables upgrades of stateful programs with fewer race conditions than otherwise possible. We use a tracing program to imbue a map with copy-on-write semantics, then use an iterator to perform a bulk copy of data in the map. After bulk copying concludes, updates to that map automatically propagate via the tracing program, avoiding a class of race conditions. This use case is demonstrated in the new '
real_world_example' selftest.Quoted 7 lines
Extend
BPF_PROG_TYPE_TRACINGwith a new attach type,BPF_TRACE_MAP, and allow linking these programs to arbitrary maps.Extend the verifier to invoke helper calls directly after
bpf_map_update_elem()andbpf_map_delete_elem(). The helpers have the exact same signature as the functions they trace, and simply pass those arguments to the list of tracing programs attached to the map.It's a neat idea to user verifier powers for this job, but I wonder why simple tracepoint in map ops was not used instead? With BTF the bpf progs see the actual types of raw tracepoints. If tracepoint has map, key, value pointers the prog will be able to access them in read-only mode.
Such map pointer will bePTR_TO_BTF_ID, so the prog won't be able to recursively do lookup/update on this map pointer,
but that's what you need anyway, right?
If not we can extend this part of the tracepoint/verifier.Instead of tracepoint it could have been an empty noinline function and fentry/fexit would see all arguments as well.
Quoted 22 lines
One open question is how to handle pointer-based map updates. For example:
int *x =bpf_map_lookup_elem(...);
if (...) *x++;
if (...) *x--;
We can't just call a helper function right after thebpf_map_lookup_elem(), since the updates occur later on. We also can't determine where the last modification to the pointer occurs, due to branch instructions. I would therefore consider a pattern where we 'flush' pointers at the end of a BPF program:
int *x =bpf_map_lookup_elem(...);
...
/* Execute tracing programs for this cell in this map. */bpf_map_trace_pointer_update(x);
return 0;
We can't necessarily do this in the verifier, since 'x' may no longer be in a register or on the stack. Thus we might introduce a helper to save pointers that should be flushed, then flush all registered pointers at every exit point:
int *x =bpf_map_lookup_elem(...);
/*
* Saves 'x' somewhere in kernel memory. Does nothing if noQuoted 8 lines
* corresponding tracing progs are attached to the map. */
bpf_map_trace_register_pointer(x);
...
/* flush all registered pointers */bpf_map_trace_pointer_update();
return 0;
This should be easy to implement in the verifier.I don't think the "solution" for lookup operation is worth pursuing. The bpf prog that needs this map tracing is completely in your control. So just don't do writes after lookup.
Quoted 4 lines
In addition, we use the verifier to instrument certain map update calls. This requires saving arguments onto the stack, which means that a program using
MAX_BPF_STACKbytes of stack could exceed the limit. I don't know whether this actually causes any problems.Extra 8*4 bytes of stack is not a deal breaker.
Quoted 6 lines · Joe Burton
Quoted 2 lines
It's a neat idea to user verifier powers for this job, but I wonder why simple tracepoint in map ops was not used instead?
My concern with tracepoints is that they execute for all map updates, not for a particular map. Ideally performing an upgrade of program X should not affect the performance characteristics of program Y.
Right, but single 'if (map == map_ptr_being_traced)'
won't really affect update() speed of maps.
For hash maps the update/delete are heavy operations with a bunch of checks and spinlocks.
Just to make sure we're on the same patch I'm proposing something like the patch below...
Quoted 4 lines
If n programs are opted into this model, then upgrading any of them affects the performance characteristics of every other. There's also the (very remote) possibility of multiple simultaneous upgrades tracing map updates at the same time, causing a greater performance hit.
Also consider that the verifier fixup of update/delete in the code is permanent whereas attaching fentry or fmod_ret to a nop function is temporary. Once tracing of the map is no longer necessary that fentry program will be detached and overhead will go back to zero.
Which is not the case for 'fixup' approach.
With fmod_ret the tracing program might be the enforcing program. It could be used to disallow certain map access in a generic way.
Quoted 4 lines
Quoted 3 lines
I don't think the "solution" for lookup operation is worth pursuing. The bpf prog that needs this map tracing is completely in your control. So just don't do writes after lookup.
Quoted 8 lines
I eventually want to support apps that use local storage. Those APIs generally only allow updates via a pointer. E.g.
bpf_sk_storage_get()only allows updates via the returned pointer and viabpf_sk_storage_delete().Since I eventually have to solve this problem to handle local storage, then it seems worth solving it for normal maps as well. They seem like isomorphic problems.
Especially for local storage... doing tracing from bpf program itself seems to make the most sense.
From c7b6ec4488ee50ebbca61c22c6837fd6fe7007bf Mon Sep 17 00:00:00 2001
From: Alexei Starovoitov <[email protected]>
Date: Wed, 6 Oct 2021 09:30:21 -0700
Subject: [PATCH] bpf: trace array map update
--- kernel/bpf/arraymap.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
kernel/bpf/arraymap.c +11 -0
@@ -293,6 +293,13 @@ static void check_and_free_timer_in_array(struct bpf_array *arr, void *val)bpf_timer_cancel_and_free (val + arr->map.timer_off); } +noinline int bpf_array_map_trace_update(struct bpf_map *map, void *key, +void *value,u64 map_flags) +{ +return 0 ; +} +ALLOW_ERROR_INJECTION (bpf_array_map_trace_update,ERRNO ); +/* Called from syscall or from eBPF program */ static int array_map_update_elem (struct bpf_map *map,void *key,void *value,u64 map_flags)
@@ -300,6 +307,7 @@ static int array_map_update_elem(struct bpf_map *map, void *key, void *value, struct bpf_array *array = container_of(map, struct bpf_array, map);u32 index = *(u32 *)key;char *val; +int err;if (unlikely ((map_flags & ~BPF_F_LOCK ) >BPF_EXIST ))/* unknown flags */
@@ -317,6 +325,9 @@ static int array_map_update_elem(struct bpf_map *map, void *key, void *value, !map_value_has_spin_lock(map)))return -EINVAL ; +if (unlikely (err =bpf_array_map_trace_update (map, key, value, map_flags))) +return err; +if (array->map.map_type ==BPF_MAP_TYPE_PERCPU_ARRAY ) {memcpy (this_cpu_ptr (array->pptrs[index & array->index_mask]), value, map->value_size);
Signature · 1 line
2.30.2
Quoted 2 lines
Just to make sure we're on the same patch I'm proposing something like the patch below...
The proposed patch seems reasonable overall:
+ eliminates a lot of boilerplate
+ enables map update filtering
+ minimal perf cost when not tracing maps
+ avoids adding complexity to verifier
- requires touching every map type's implementation
- tracing one map implies tracing all maps
I can rev this RFC with hooks inside the common map types' update() and delete() methods.
Quoted 2 lines
Especially for local storage... doing tracing from bpf program itself seems to make the most sense.
I'm a little unclear on how this should work. There's no off-the-shelf solution that can do this for us, right?
In particular I think we're looking for an interface like this:
/* This is a BPF program */
int my_prog(struct bpf_sock *sk) {
struct MyValue *v = bpf_sk_storage_get(&my_map, sk, ...);
...
bpf_sk_storage_trace(&my_map, sk, v);
return 0;
}I.e. we need some way of triggering a tracing hook from a BPF program. For non-local storage maps we can achieve this with a bpf_map_update_elem(). For local storage I suspect we need something new.
Assuming there's no off-the-shelf hook that I'm missing, we can do some brainstorming internally and come back with a proposal or two.
Quoted 95 lines · Alexei Starovoitov
Quoted 6 lines · Joe Burton
Quoted 2 lines
It's a neat idea to user verifier powers for this job, but I wonder why simple tracepoint in map ops was not used instead?
My concern with tracepoints is that they execute for all map updates, not for a particular map. Ideally performing an upgrade of program X should not affect the performance characteristics of program Y.
Right, but single 'if (map ==
map_ptr_being_traced)'
won't really affectupdate()speed of maps.
For hash maps the update/delete are heavy operations with a bunch of checks and spinlocks.
Just to make sure we're on the same patch I'm proposing something like the patch below...Quoted 4 lines
If n programs are opted into this model, then upgrading any of them affects the performance characteristics of every other. There's also the (very remote) possibility of multiple simultaneous upgrades tracing map updates at the same time, causing a greater performance hit.
Also consider that the verifier fixup of update/delete in the code is permanent whereas attaching fentry or
fmod_retto a nop function is temporary. Once tracing of the map is no longer necessary that fentry program will be detached and overhead will go back to zero.
Which is not the case for 'fixup' approach.With
fmod_retthe tracing program might be the enforcing program. It could be used to disallow certain map access in a generic way.Quoted 4 lines
Quoted 3 lines
I don't think the "solution" for lookup operation is worth pursuing. The bpf prog that needs this map tracing is completely in your control. So just don't do writes after lookup.
Quoted 8 lines
I eventually want to support apps that use local storage. Those APIs generally only allow updates via a pointer. E.g.
bpf_sk_storage_get()only allows updates via the returned pointer and viabpf_sk_storage_delete().Since I eventually have to solve this problem to handle local storage, then it seems worth solving it for normal maps as well. They seem like isomorphic problems.
Especially for local storage... doing tracing from bpf program itself seems to make the most sense.
From
c7b6ec4488ee50ebbca61c22c6837fd6fe7007bfMon Sep 17 00:00:00 2001
From: Alexei Starovoitov <[email protected]>
Date: Wed, 6 Oct 2021 09:30:21 -0700
Subject: [PATCH] bpf: trace array map updateSigned-off-by Alexei Starovoitov <[email protected]>--- kernel/bpf/arraymap.c | 11 +++++++++++ 1 file changed, 11 insertions(+)kernel/bpf/arraymap.c +11
-1@@ -293,6 +293,13 @@ static void check_and_free_timer_in_array(struct bpf_array *arr, void *val)bpf_timer_cancel_and_free (val + arr->map.timer_off); } +noinline int bpf_array_map_trace_update(struct bpf_map *map, void *key, +void *value,u64 map_flags) +{ +return 0 ; +} +ALLOW_ERROR_INJECTION (bpf_array_map_trace_update,ERRNO ); +/* Called from syscall or from eBPF program */ static int array_map_update_elem (struct bpf_map *map,void *key,void *value,u64 map_flags)@@ -300,6 +307,7 @@ static int array_map_update_elem(struct bpf_map *map, void *key, void *value, struct bpf_array *array = container_of(map, struct bpf_array, map);u32 index = *(u32 *)key;char *val; +int err;if (unlikely ((map_flags & ~BPF_F_LOCK ) >BPF_EXIST ))/* unknown flags */ @@ -317,6 +325,9 @@ static int array_map_update_elem(struct bpf_map *map, void *key, void *value, !map_value_has_spin_lock(map)))return -EINVAL ; +if (unlikely (err =bpf_array_map_trace_update (map, key, value, map_flags))) +return err; +if (array->map.map_type ==BPF_MAP_TYPE_PERCPU_ARRAY ) {memcpy (this_cpu_ptr (array->pptrs[index & array->index_mask]), value, map->value_size); --2.30.2
Quoted 10 lines · Joe Burton
Quoted 2 lines
Just to make sure we're on the same patch I'm proposing something like the patch below...
The proposed patch seems reasonable overall:
+ eliminates a lot of boilerplate
+ enables map update filtering
+ minimal perf cost when not tracing maps
+ avoids adding complexity to verifier
- requires touching every map type's implementation
- tracing one map implies tracing all maps
right. The single 'if' filter inside attached bpf prog should be fast enough.
Quoted 20 lines
I can rev this RFC with hooks inside the common map types'
update()anddelete()methods.Quoted 2 lines
Especially for local storage... doing tracing from bpf program itself seems to make the most sense.
I'm a little unclear on how this should work. There's no off-the-shelf solution that can do this for us, right?
In particular I think we're looking for an interface like this:
/* This is a BPF program */ int my_prog(struct bpf_sock *sk) { struct MyValue *v = bpf_sk_storage_get(&my_map, sk, ...); ... bpf_sk_storage_trace(&my_map, sk, v); return 0; }
Quoted 1 line
I.e. we need some way of triggering a tracing hook from a BPF program.
I mean that above can be done as bpf prog. bpf_sk_storage_trace() can be an empty global function inside a bpf program. The attach to it is either fentry or even freplace.