Notify user space when a struct_ops object is detached/unregistered
Subsystems (6)
v2 cover Notify user space when a struct_ops object is detached/unregistered
The subsystems managing struct_ops objects may need to detach a struct_ops object due to errors or other reasons. It would be useful to notify user space programs so that error recovery or logging can be carried out.
This patch set enables the detach feature for struct_ops links and send an event to epoll when a link is detached. Subsystems could call link->ops->detach() to detach a link and notify user space programs through epoll.
The signatures of callback functions in "struct bpf_struct_ops" have been changed as well to pass an extra link argument to subsystems. Subsystems could detach the links received from reg() and update() callbacks if there is. This also provides a way that subsystems can distinguish registrations for an object that has been registered multiple times for several links.
However, bpf struct_ops maps without BPF_F_LINK have no any link. Subsystems will receive NULL link pointer for this case.
---
Changes from v1:
- Pass a link to reg, unreg, and update callbacks.
- Provide a function to detach a link from underlying subsystems.
- Add a kfunc to minic detachments from subsystems, and provide a flexible way to control when to do detachments.
- Add two tests to detach a link from the subsystem after the refcount of the link drops to zero.
v1: https://lore.kernel.org/all/[email protected]/
Kui-Feng Lee (6):
bpf: pass bpf_struct_ops_link to callbacks in bpf_struct_ops.
bpf: enable detaching links of struct_ops objects.
bpf: support epoll from bpf struct_ops links. selftests/bpf: test struct_ops with epoll
selftests/bpf: detach a struct_ops link from the subsystem managing
it.
selftests/bpf: make sure bpf_testmod handling racing link destroying
well.
include/linux/bpf.h | 7 +- kernel/bpf/bpf_struct_ops.c | 77 ++++++-- kernel/bpf/syscall.c | 11 ++ net/bpf/bpf_dummy_struct_ops.c | 4 +- net/ipv4/bpf_tcp_ca.c | 6 +- .../bpf/bpf_test_no_cfi/bpf_test_no_cfi.c | 4 +- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 40 ++++- .../bpf/bpf_testmod/bpf_testmod_kfunc.h | 1 + .../bpf/prog_tests/test_struct_ops_module.c | 166 ++++++++++++++++++ .../selftests/bpf/progs/struct_ops_detach.c | 37 ++++ 10 files changed, 329 insertions(+), 24 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_detach.c
Signature · 1 line
2.34.1
PATCH v2 1/6 bpf: pass bpf_struct_ops_link to callbacks in bpf_struct_ops.
Pass an additional pointer of bpf_struct_ops_link to callback function reg, unreg, and update provided by subsystems defined in bpf_struct_ops. A bpf_struct_ops_map can be registered for multiple links. Passing a pointer of bpf_struct_ops_link helps subsystems to distinguish them.
This pointer will be used in the later patches to let the subsystem initiate a detachment on a link that was registered to it previously.
--- include/linux/bpf.h | 6 +++--- kernel/bpf/bpf_struct_ops.c | 10 +++++----- net/bpf/bpf_dummy_struct_ops.c | 4 ++-- net/ipv4/bpf_tcp_ca.c | 6 +++--- .../selftests/bpf/bpf_test_no_cfi/bpf_test_no_cfi.c | 4 ++-- tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c | 6 +++--- 6 files changed, 18 insertions(+), 18 deletions(-)
include/linux/bpf.h +3 -3
@@ -1730,9 +1730,9 @@ struct bpf_struct_ops {int (*init_member)(const struct btf_type *t,const struct btf_member *member,void *kdata,const void *udata); -int (*reg)(void *kdata); -void (*unreg)(void *kdata); -int (*update)(void *kdata,void *old_kdata); +int (*reg)(void *kdata,struct bpf_link *link); +void (*unreg)(void *kdata,struct bpf_link *link); +int (*update)(void *kdata,void *old_kdata,struct bpf_link *link);int (*validate)(void *kdata);void *cfi_stubs;struct module *owner;
kernel/bpf/bpf_struct_ops.c +5 -5
@@ -757,7 +757,7 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, goto unlock; } - err = st_ops->reg (kdata); + err = st_ops->reg (kdata,NULL );if (likely (!err)) {/* This refcnt increment on the map here after * 'st_ops->reg()' is secure since the state of the
@@ -805,7 +805,7 @@ static long bpf_struct_ops_map_delete_elem(struct bpf_map *map, void *key) BPF_STRUCT_OPS_STATE_TOBEFREE);switch (prev_state) {case BPF_STRUCT_OPS_STATE_INUSE : - st_map->st_ops_desc->st_ops->unreg (&st_map->kvalue.data); + st_map->st_ops_desc->st_ops->unreg (&st_map->kvalue.data,NULL );bpf_map_put (map);return 0 ;case BPF_STRUCT_OPS_STATE_TOBEFREE :
@@ -1060,7 +1060,7 @@ static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link)/* st_link->map can be NULL if * bpf_struct_ops_link_create() fails to register. */ - st_map->st_ops_desc->st_ops->unreg (&st_map->kvalue.data); + st_map->st_ops_desc->st_ops->unreg (&st_map->kvalue.data, st_link);bpf_map_put (&st_map->map); }kfree (st_link);
@@ -1125,7 +1125,7 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map goto err_out; } - err = st_map->st_ops_desc->st_ops->update (st_map->kvalue.data, old_st_map->kvalue.data); + err = st_map->st_ops_desc->st_ops->update (st_map->kvalue.data, old_st_map->kvalue.data, link);if (err) goto err_out;
@@ -1176,7 +1176,7 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)if (err) goto err_out; - err = st_map->st_ops_desc->st_ops->reg (st_map->kvalue.data); + err = st_map->st_ops_desc->st_ops->reg (st_map->kvalue.data, &link->link);if (err) {bpf_link_cleanup (&link_primer); link =NULL ;
net/bpf/bpf_dummy_struct_ops.c +2 -2
@@ -272,12 +272,12 @@ static int bpf_dummy_init_member(const struct btf_type *t,return -EOPNOTSUPP ; } -static int bpf_dummy_reg (void *kdata) +static int bpf_dummy_reg (void *kdata,struct bpf_link *link) {return -EOPNOTSUPP ; } -static void bpf_dummy_unreg (void *kdata) +static void bpf_dummy_unreg (void *kdata,struct bpf_link *link) { }
net/ipv4/bpf_tcp_ca.c +3 -3
@@ -257,17 +257,17 @@ static int bpf_tcp_ca_check_member(const struct btf_type *t,return 0 ; } -static int bpf_tcp_ca_reg (void *kdata) +static int bpf_tcp_ca_reg (void *kdata,struct bpf_link *link) {return tcp_register_congestion_control (kdata); } -static void bpf_tcp_ca_unreg (void *kdata) +static void bpf_tcp_ca_unreg (void *kdata,struct bpf_link *link) {tcp_unregister_congestion_control (kdata); } -static int bpf_tcp_ca_update (void *kdata,void *old_kdata) +static int bpf_tcp_ca_update (void *kdata,void *old_kdata,struct bpf_link *link) {return tcp_update_congestion_control (kdata, old_kdata); }
tools/testing/selftests/bpf/bpf_test_no_cfi/bpf_test_no_cfi.c +2 -2
@@ -22,12 +22,12 @@ static int dummy_init_member(const struct btf_type *t,return 0 ; } -static int dummy_reg (void *kdata) +static int dummy_reg (void *kdata,struct bpf_link *link) {return 0 ; } -static void dummy_unreg (void *kdata) +static void dummy_unreg (void *kdata,struct bpf_link *link) { }
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +3 -3
@@ -565,7 +565,7 @@ static const struct bpf_verifier_ops bpf_testmod_verifier_ops = { .is_valid_access = bpf_testmod_ops_is_valid_access, }; -static int bpf_dummy_reg (void *kdata) +static int bpf_dummy_reg (void *kdata,struct bpf_link *link) {struct bpf_testmod_ops *ops = kdata;
@@ -580,7 +580,7 @@ static int bpf_dummy_reg(void *kdata)return 0 ; } -static void bpf_dummy_unreg (void *kdata) +static void bpf_dummy_unreg (void *kdata,struct bpf_link *link) { }
@@ -616,7 +616,7 @@ struct bpf_struct_ops bpf_bpf_testmod_ops = { .owner = THIS_MODULE, }; -static int bpf_dummy_reg2 (void *kdata) +static int bpf_dummy_reg2 (void *kdata,struct bpf_link *link) {struct bpf_testmod_ops2 *ops = kdata;
Signature · 1 line
2.34.1
PATCH v2 2/6 bpf: enable detaching links of struct_ops objects.
In reply to Kui-Feng LeeImplement the detach callback in bpf_link_ops for struct_ops. The subsystems that struct_ops objects are registered to can use this callback to detach the links being passed to them.
--- kernel/bpf/bpf_struct_ops.c | 50 ++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 6 deletions(-)
kernel/bpf/bpf_struct_ops.c +44 -6
@@ -1057,9 +1057,6 @@ static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link) st_map = (struct bpf_struct_ops_map *)rcu_dereference_protected (st_link->map, true);if (st_map) { -/* st_link->map can be NULL if -* bpf_struct_ops_link_create() fails to register. -*/ st_map->st_ops_desc->st_ops->unreg (&st_map->kvalue.data, st_link);bpf_map_put (&st_map->map); }
@@ -1075,7 +1072,8 @@ static void bpf_struct_ops_map_link_show_fdinfo(const struct bpf_link *link, st_link = container_of(link, struct bpf_struct_ops_link, link);rcu_read_lock (); map =rcu_dereference (st_link->map); -seq_printf (seq,"map_id:\t%d\n" , map->id); +if (map) +seq_printf (seq,"map_id:\t%d\n" , map->id);rcu_read_unlock (); }
@@ -1088,7 +1086,8 @@ static int bpf_struct_ops_map_link_fill_link_info(const struct bpf_link *link, st_link = container_of(link, struct bpf_struct_ops_link, link);rcu_read_lock (); map =rcu_dereference (st_link->map); - info->struct_ops.map_id = map->id; +if (map) + info->struct_ops.map_id = map->id;rcu_read_unlock ();return 0 ; }
@@ -1113,6 +1112,10 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_mapmutex_lock (&update_mutex); old_map =rcu_dereference_protected (st_link->map,lockdep_is_held (&update_mutex)); +if (!old_map) { + err = -EINVAL ; + goto err_out; + }if (expected_old_map && old_map != expected_old_map) { err = -EPERM ; goto err_out;
@@ -1139,8 +1142,37 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_mapreturn err; } +static int bpf_struct_ops_map_link_detach (struct bpf_link *link) +{ + struct bpf_struct_ops_link *st_link = container_of(link, struct bpf_struct_ops_link, link); +struct bpf_struct_ops_map *st_map; +struct bpf_map *map; + +mutex_lock (&update_mutex); + + map =rcu_dereference_protected (st_link->map, true); +if (!map) { +mutex_unlock (&update_mutex); +return -EINVAL ; + } + st_map = container_of(map, struct bpf_struct_ops_map, map); + + st_map->st_ops_desc->st_ops->unreg (&st_map->kvalue.data, link); + +rcu_assign_pointer (st_link->map,NULL ); +/* Pair with bpf_map_get() in bpf_struct_ops_link_create() or +* bpf_map_inc() in bpf_struct_ops_map_link_update(). +*/ +bpf_map_put (&st_map->map); + +mutex_unlock (&update_mutex); + +return 0 ; +} +static const struct bpf_link_ops bpf_struct_ops_map_lops = { .dealloc = bpf_struct_ops_map_link_dealloc, + .detach = bpf_struct_ops_map_link_detach, .show_fdinfo = bpf_struct_ops_map_link_show_fdinfo, .fill_link_info = bpf_struct_ops_map_link_fill_link_info, .update_map = bpf_struct_ops_map_link_update,
@@ -1176,13 +1208,19 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)if (err) goto err_out; +/* Init link->map before calling reg() in case being detached +* immediately. +*/ +RCU_INIT_POINTER (link->map, map); + err = st_map->st_ops_desc->st_ops->reg (st_map->kvalue.data, &link->link);if (err) { +rcu_assign_pointer (link->map,NULL );bpf_link_cleanup (&link_primer); +/* The link has been free by bpf_link_cleanup() */ link =NULL ; goto err_out; } -RCU_INIT_POINTER (link->map, map);return bpf_link_settle (&link_primer);
Signature · 1 line
2.34.1
PATCH v2 3/6 bpf: support epoll from bpf struct_ops links.
In reply to Kui-Feng LeeAdd epoll support to bpf struct_ops links to trigger EPOLLHUP event upon detachment.
--- include/linux/bpf.h | 1 + kernel/bpf/bpf_struct_ops.c | 17 +++++++++++++++++ kernel/bpf/syscall.c | 11 +++++++++++ 3 files changed, 29 insertions(+)
include/linux/bpf.h +1 -0
@@ -1612,6 +1612,7 @@ struct bpf_link_ops { struct bpf_link_info *info);int (*update_map)(struct bpf_link *link,struct bpf_map *new_map,struct bpf_map *old_map); +__poll_t (*poll)(struct file *file,struct poll_table_struct *pts); };struct bpf_tramp_link {
kernel/bpf/bpf_struct_ops.c +17 -0
@@ -12,6 +12,7 @@#include <linux/mutex.h> #include <linux/btf_ids.h> #include <linux/rcupdate_wait.h> +#include <linux/poll.h> struct bpf_struct_ops_value {struct bpf_struct_ops_common_value common;
@@ -56,6 +57,7 @@ struct bpf_struct_ops_map {struct bpf_struct_ops_link {struct bpf_link link;struct bpf_map __rcu *map; +wait_queue_head_t wait_hup; };static DEFINE_MUTEX (update_mutex);
@@ -1167,15 +1169,28 @@ static int bpf_struct_ops_map_link_detach(struct bpf_link *link)mutex_unlock (&update_mutex); +wake_up_interruptible_poll (&st_link->wait_hup,EPOLLHUP ); +return 0 ; } +static __poll_t bpf_struct_ops_map_link_poll (struct file *file, +struct poll_table_struct *pts) +{ +struct bpf_struct_ops_link *st_link = file->private_data; + +poll_wait (file, &st_link->wait_hup, pts); + +return (st_link->map) ?0 :EPOLLHUP ; +} +static const struct bpf_link_ops bpf_struct_ops_map_lops = { .dealloc = bpf_struct_ops_map_link_dealloc, .detach = bpf_struct_ops_map_link_detach, .show_fdinfo = bpf_struct_ops_map_link_show_fdinfo, .fill_link_info = bpf_struct_ops_map_link_fill_link_info, .update_map = bpf_struct_ops_map_link_update, + .poll = bpf_struct_ops_map_link_poll, };int bpf_struct_ops_link_create (union bpf_attr *attr)
@@ -1213,6 +1228,8 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)*/ RCU_INIT_POINTER (link->map, map); +init_waitqueue_head (&link->wait_hup); + err = st_map->st_ops_desc->st_ops->reg (st_map->kvalue.data, &link->link);if (err) {rcu_assign_pointer (link->map,NULL );
kernel/bpf/syscall.c +11 -0
@@ -3150,6 +3150,16 @@ static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp) }#endif +static __poll_t bpf_link_poll (struct file *file,struct poll_table_struct *pts) +{ +struct bpf_link *link = file->private_data; + +if (link->ops->poll) +return link->ops->poll (file, pts); + +return 0 ; +} +static const struct file_operations bpf_link_fops = { #ifdef CONFIG_PROC_FS .show_fdinfo = bpf_link_show_fdinfo,
@@ -3157,6 +3167,7 @@ static const struct file_operations bpf_link_fops = { .release = bpf_link_release, .read = bpf_dummy_read, .write = bpf_dummy_write, + .poll = bpf_link_poll, };static int bpf_link_alloc_id (struct bpf_link *link)
Signature · 1 line
2.34.1
PATCH v2 4/6 selftests/bpf: test struct_ops with epoll
In reply to Kui-Feng LeeVerify whether a user space program is informed through epoll with EPOLLHUP when a struct_ops object is detached.
--- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 13 +++++ .../bpf/bpf_testmod/bpf_testmod_kfunc.h | 1 + .../bpf/prog_tests/test_struct_ops_module.c | 57 +++++++++++++++++++ .../selftests/bpf/progs/struct_ops_detach.c | 31 ++++++++++ 4 files changed, 102 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_detach.c
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +13 -0
@@ -10,6 +10,7 @@#include <linux/percpu-defs.h> #include <linux/sysfs.h> #include <linux/tracepoint.h> +#include <linux/workqueue.h> #include "bpf_testmod.h" #include "bpf_testmod_kfunc.h"
@@ -498,6 +499,9 @@ __bpf_kfunc void bpf_kfunc_call_test_sleepable(void) { } +static DEFINE_MUTEX (detach_mutex); +static struct bpf_link *link_to_detach; + BTF_KFUNCS_START(bpf_testmod_check_kfunc_ids) BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc) BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
@@ -577,11 +581,20 @@ static int bpf_dummy_reg(void *kdata, struct bpf_link *link)if (ops->test_2) ops->test_2 (4 , ops->data); +mutex_lock (&detach_mutex); +if (!link_to_detach) + link_to_detach = link; +mutex_unlock (&detach_mutex); +return 0 ; }static void bpf_dummy_unreg (void *kdata,struct bpf_link *link) { +mutex_lock (&detach_mutex); +if (link == link_to_detach) + link_to_detach =NULL ; +mutex_unlock (&detach_mutex); }static int bpf_testmod_test_1 (void )
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h +1 -0
@@ -105,6 +105,7 @@ void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p);void bpf_kfunc_call_test_fail2 (struct prog_test_fail2 *p);void bpf_kfunc_call_test_fail3 (struct prog_test_fail3 *p);void bpf_kfunc_call_test_mem_len_fail1 (void *mem,int len); +int bpf_dummy_do_link_detach(void) __ksym; void bpf_kfunc_common_test(void) __ksym;#endif /* _BPF_TESTMOD_KFUNC_H */
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +57 -0
76 lines not on this page · raw message
tools/testing/selftests/bpf/progs/struct_ops_detach.c new file +31 -0
@@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> +#include "../bpf_testmod/bpf_testmod.h" +#include "../bpf_testmod/bpf_testmod_kfunc.h" + +char _license[] SEC("license") = "GPL"; + +int test_1_result =0 ; +int test_2_result =0 ; + +SEC("struct_ops/test_1") +int BPF_PROG (test_1 ) +{ + test_1_result =0xdeadbeef ; +return 0 ; +} + +SEC("struct_ops/test_2") +void BPF_PROG (test_2 ,int a,int b) +{ + test_2_result = a + b; +} + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_do_detach = { + .test_1 = (void *)test_1, + .test_2 = (void *)test_2, +};
Signature · 1 line
2.34.1
PATCH v2 5/6 selftests/bpf: detach a struct_ops link from the subsystem managing it.
In reply to Kui-Feng LeeNot only a user space program can detach a struct_ops link, the subsystem managing a link can also detach the link. This patch add a kfunc to simulate detaching a link by the subsystem managing it.
--- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 21 ++++++ .../bpf/prog_tests/test_struct_ops_module.c | 65 +++++++++++++++++++ .../selftests/bpf/progs/struct_ops_detach.c | 6 ++ 3 files changed, 92 insertions(+)
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +21 -0
@@ -502,6 +502,26 @@ __bpf_kfunc void bpf_kfunc_call_test_sleepable(void)static DEFINE_MUTEX (detach_mutex);static struct bpf_link *link_to_detach; +__bpf_kfunc int bpf_dummy_do_link_detach(void) +{ +struct bpf_link *link; +int ret = -ENOENT ; + +mutex_lock (&detach_mutex); + link = link_to_detach; +/* Make sure the link is still valid by increasing its refcnt */ +if (link && !atomic64_inc_not_zero (&link->refcnt)) + link =NULL ; +mutex_unlock (&detach_mutex); + +if (link) { + ret = link->ops->detach (link); +bpf_link_put (link); + } + +return ret; +} + BTF_KFUNCS_START(bpf_testmod_check_kfunc_ids) BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc) BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
@@ -529,6 +549,7 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE) BTF_ID_FLAGS(func, bpf_kfunc_call_test_static_unused_arg) BTF_ID_FLAGS(func, bpf_kfunc_call_test_offset) BTF_ID_FLAGS(func, bpf_kfunc_call_test_sleepable, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_dummy_do_link_detach)BTF_KFUNCS_END (bpf_testmod_check_kfunc_ids) static int bpf_testmod_ops_init(struct btf *btf)
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +65 -0
76 lines not on this page · raw message
tools/testing/selftests/bpf/progs/struct_ops_detach.c +6 -0
@@ -29,3 +29,9 @@ struct bpf_testmod_ops testmod_do_detach = { .test_1 = (void *)test_1, .test_2 = (void *)test_2, }; + +SEC("tc") +int start_detach (void *skb) +{ +return bpf_dummy_do_link_detach (); +}
Signature · 1 line
2.34.1
PATCH v2 6/6 selftests/bpf: make sure bpf_testmod handling racing link destroying well.
In reply to Kui-Feng LeeSubsystems that manage struct_ops objects may attempt to detach a link when the link has been released or is about to be released. The test in this patch demonstrate to developers the correct way to handle this situation using a locking mechanism and atomic64_inc_not_zero().
A subsystem must ensure that a link is valid when detaching the link. In order to achieve that, the subsystem may need to obtain a lock to safeguard a table that holds the pointer to the link being detached. However, the subsystem cannot invoke link->ops->detach() while holding the lock because other tasks may be in the process of unregistering, which could lead to a deadlock. This is why atomic64_inc_not_zero() is used to maintain the link's validity. (Refer to bpf_dummy_do_link_detach() in the previous patch for more details.)
This test make sure the pattern mentioned above work correctly.
--- .../bpf/prog_tests/test_struct_ops_module.c | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+)
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +44 -0
@@ -292,6 +292,48 @@ static void test_subsystem_detach(void)struct_ops_detach__destroy (skel); } +/* A subsystem detachs a link while the link is going to be free. */ +static void test_subsystem_detach_free (void ) +{ +LIBBPF_OPTS (bpf_test_run_opts, topts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4)); +struct struct_ops_detach *skel; +struct bpf_link *link =NULL ; +int prog_fd; +int err; + + skel =struct_ops_detach__open_and_load (); +if (!ASSERT_OK_PTR (skel,"struct_ops_detach_open_and_load" )) +return ; + + link =bpf_map__attach_struct_ops (skel->maps.testmod_do_detach); +if (!ASSERT_OK_PTR (link,"attach_struct_ops" )) + goto cleanup; + +bpf_link__destroy (link); + + prog_fd =bpf_program__fd (skel->progs.start_detach); +if (!ASSERT_GE (prog_fd,0 ,"start_detach_fd" )) + goto cleanup; + +/* Do detachment from the registered subsystem */ + err =bpf_prog_test_run_opts (prog_fd, &topts); +if (!ASSERT_OK (err,"start_detach_run" )) + goto cleanup; + +/* The link may have zero refcount value and may have been +* unregistered, so the detachment from the subsystem should fail. +*/ +ASSERT_EQ (topts.retval, (u32)-ENOENT ,"start_detach_run retval" ); + +/* Sync RCU to make sure the link is freed without any crash */ +ASSERT_OK (kern_sync_rcu (),"sync rcu" ); + +cleanup: +struct_ops_detach__destroy (skel); +} +void serial_test_struct_ops_module (void ) {if (test__start_subtest ("test_struct_ops_load" ))
@@ -304,5 +346,7 @@ void serial_test_struct_ops_module(void)test_detach_link ();if (test__start_subtest ("test_subsystem_detach" ))test_subsystem_detach (); +if (test__start_subtest ("test_subsystem_detach_free" )) +test_subsystem_detach_free (); }
Signature · 1 line
2.34.1
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v2 2/6] bpf: enable detaching links of struct_ops objects.
In reply to Kui-Feng LeeQuoted 3 lines · Kui-Feng Lee
Implement the detach callback in
bpf_link_opsforstruct_ops. The subsystems thatstruct_opsobjects are registered to can use this callback to detach the links being passed to them.
The user space can also use the detach. The subsystem is merely reusing the similar detach callback if it stores the link during reg().
Quoted 54 lines
Signed-off-by Kui-Feng Lee <[email protected]>--- kernel/bpf/bpf_struct_ops.c | 50 ++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 6 deletions(-)kernel/bpf/bpf_struct_ops.c +8
-5@@ -1057,9 +1057,6 @@ static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link) st_map = (struct bpf_struct_ops_map *)rcu_dereference_protected (st_link->map, true);if (st_map) { -/* st_link->map can be NULL if -* bpf_struct_ops_link_create() fails to register. -*/ st_map->st_ops_desc->st_ops->unreg (&st_map->kvalue.data, st_link);bpf_map_put (&st_map->map); }@@ -1075,7 +1072,8 @@ static void bpf_struct_ops_map_link_show_fdinfo(const struct bpf_link *link, st_link = container_of(link, struct bpf_struct_ops_link, link);rcu_read_lock (); map =rcu_dereference (st_link->map); -seq_printf (seq,"map_id:\t%d\n" , map->id); +if (map) +seq_printf (seq,"map_id:\t%d\n" , map->id);rcu_read_unlock (); }@@ -1088,7 +1086,8 @@ static int bpf_struct_ops_map_link_fill_link_info(const struct bpf_link *link, st_link = container_of(link, struct bpf_struct_ops_link, link);rcu_read_lock (); map =rcu_dereference (st_link->map); - info->struct_ops.map_id = map->id; +if (map) + info->struct_ops.map_id = map->id;rcu_read_unlock ();return 0 ; }@@ -1113,6 +1112,10 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_mapmutex_lock (&update_mutex); old_map =rcu_dereference_protected (st_link->map,lockdep_is_held (&update_mutex)); +if (!old_map) { + err = -EINVAL ; + goto err_out; + }if (expected_old_map && old_map != expected_old_map) { err = -EPERM ; goto err_out;@@ -1139,8 +1142,37 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_mapreturn err; }
kernel/bpf/bpf_struct_ops.c static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map · line 1153
Quoted 10 lines
kernel/bpf/bpf_struct_ops.c +9
-0+static int bpf_struct_ops_map_link_detach (struct bpf_link *link) +{ + struct bpf_struct_ops_link *st_link = container_of(link, struct bpf_struct_ops_link, link); +struct bpf_struct_ops_map *st_map; +struct bpf_map *map; + +mutex_lock (&update_mutex); + + map =rcu_dereference_protected (st_link->map, true);
nit. s/true/lockdep_is_held(&update_mutex)/
Quoted 27 lines
kernel/bpf/bpf_struct_ops.c +20
-0+if (!map) { +mutex_unlock (&update_mutex); +return -EINVAL ; + } + st_map = container_of(map, struct bpf_struct_ops_map, map); + + st_map->st_ops_desc->st_ops->unreg (&st_map->kvalue.data, link); + +rcu_assign_pointer (st_link->map,NULL ); +/* Pair with bpf_map_get() in bpf_struct_ops_link_create() or +* bpf_map_inc() in bpf_struct_ops_map_link_update(). +*/ +bpf_map_put (&st_map->map); + +mutex_unlock (&update_mutex); + +return 0 ; +} +static const struct bpf_link_ops bpf_struct_ops_map_lops = { .dealloc = bpf_struct_ops_map_link_dealloc, + .detach = bpf_struct_ops_map_link_detach, .show_fdinfo = bpf_struct_ops_map_link_show_fdinfo, .fill_link_info = bpf_struct_ops_map_link_fill_link_info, .update_map = bpf_struct_ops_map_link_update,@@ -1176,13 +1208,19 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)if (err)
kernel/bpf/bpf_struct_ops.c int bpf_struct_ops_link_create(union bpf_attr *attr) · line 1218
Quoted 10 lines
kernel/bpf/bpf_struct_ops.c +6
-0goto err_out; +/* Init link->map before calling reg() in case being detached +* immediately. +*/ +RCU_INIT_POINTER (link->map, map); + err = st_map->st_ops_desc->st_ops->reg (st_map->kvalue.data, &link->link);if (err) { +rcu_assign_pointer (link->map,NULL );
nit. RCU_INIT_POINTER(link->map, NULL) is fine.
There is a merge conflict with patch 4 also.
pw-bot: cr
Quoted 9 lines
kernel/bpf/bpf_struct_ops.c +1
-1bpf_link_cleanup (&link_primer); +/* The link has been free by bpf_link_cleanup() */ link =NULL ; goto err_out; } -RCU_INIT_POINTER (link->map, map);return bpf_link_settle (&link_primer);
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v2 4/6] selftests/bpf: test struct_ops with epoll
In reply to Kui-Feng LeeQuoted 42 lines · Kui-Feng Lee
Verify whether a user space program is informed through epoll with EPOLLHUP when a
struct_opsobject is detached.Signed-off-by Kui-Feng Lee <[email protected]>--- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 13 +++++ .../bpf/bpf_testmod/bpf_testmod_kfunc.h | 1 + .../bpf/prog_tests/test_struct_ops_module.c | 57 +++++++++++++++++++ .../selftests/bpf/progs/struct_ops_detach.c | 31 ++++++++++ 4 files changed, 102 insertions(+)create mode 100644
tools/testing/selftests/bpf/progs/struct_ops_detach.ctools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +8
-0@@ -10,6 +10,7 @@#include <linux/percpu-defs.h> #include <linux/sysfs.h> #include <linux/tracepoint.h> +#include <linux/workqueue.h> #include "bpf_testmod.h" #include "bpf_testmod_kfunc.h" @@ -498,6 +499,9 @@ __bpf_kfunc void bpf_kfunc_call_test_sleepable(void) { } +static DEFINE_MUTEX (detach_mutex); +static struct bpf_link *link_to_detach; + BTF_KFUNCS_START(bpf_testmod_check_kfunc_ids) BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc) BTF_ID_FLAGS(func, bpf_kfunc_call_test1)@@ -577,11 +581,20 @@ static int bpf_dummy_reg(void *kdata, struct bpf_link *link)if (ops->test_2) ops->test_2 (4 , ops->data); +mutex_lock (&detach_mutex); +if (!link_to_detach) + link_to_detach = link; +mutex_unlock (&detach_mutex);
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c static int bpf_dummy_reg(void *kdata, struct bpf_link *link) · line 597
Quoted 10 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +5
-0+return 0 ; }static void bpf_dummy_unreg (void *kdata,struct bpf_link *link) { +mutex_lock (&detach_mutex); +if (link == link_to_detach) + link_to_detach =NULL ; +mutex_unlock (&detach_mutex);
The reg/unreg changes should belong to the next patch.
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p); · line 108
Quoted 12 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +0
-0}static int bpf_testmod_test_1 (void )tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h +1
-0@@ -105,6 +105,7 @@ void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p);void bpf_kfunc_call_test_fail2 (struct prog_test_fail2 *p);void bpf_kfunc_call_test_fail3 (struct prog_test_fail3 *p);void bpf_kfunc_call_test_mem_len_fail1 (void *mem,int len); +int bpf_dummy_do_link_detach(void) __ksym;
The kfunc is not added in this patch either.
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c line 5
Quoted 12 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h +0
-0void bpf_kfunc_common_test(void) __ksym;#endif /* _BPF_TESTMOD_KFUNC_H */ tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +1
-0@@ -2,8 +2,12 @@/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ #include <test_progs.h> #include <time.h> +#include <network_helpers.h>
What is needed from network_herlpers.h?
Quoted 49 lines
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +40
-0+ +#include <sys/epoll.h> #include "struct_ops_module.skel.h" +#include "struct_ops_detach.skel.h" static void check_map_info (struct bpf_map_info *info) {@@ -174,6 +178,57 @@ static void test_struct_ops_incompatible(void)struct_ops_module__destroy (skel); } +/* Detach a link from a user space program */ +static void test_detach_link (void ) +{ +struct epoll_event ev, events[2 ]; +struct struct_ops_detach *skel; +struct bpf_link *link =NULL ; +int fd, epollfd =-1 , nfds; +int err; + + skel =struct_ops_detach__open_and_load (); +if (!ASSERT_OK_PTR (skel,"struct_ops_detach__open_and_load" )) +return ; + + link =bpf_map__attach_struct_ops (skel->maps.testmod_do_detach); +if (!ASSERT_OK_PTR (link,"attach_struct_ops" )) + goto cleanup; + + fd =bpf_link__fd (link); +if (!ASSERT_GE (fd,0 ,"link_fd" )) + goto cleanup; + + epollfd =epoll_create1 (0 ); +if (!ASSERT_GE (epollfd,0 ,"epoll_create1" )) + goto cleanup; + + ev.events =EPOLLHUP ; + ev.data.fd = fd; + err =epoll_ctl (epollfd,EPOLL_CTL_ADD , fd, &ev); +if (!ASSERT_OK (err,"epoll_ctl" )) + goto cleanup; + + err =bpf_link__detach (link); +if (!ASSERT_OK (err,"detach_link" )) + goto cleanup; + +/* Wait for EPOLLHUP */ + nfds =epoll_wait (epollfd, events,2 ,500 );
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c static void test_struct_ops_incompatible(void) · line 227
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +10
-0+if (!ASSERT_EQ (nfds,1 ,"epoll_wait" )) + goto cleanup; + +if (!ASSERT_EQ (events[0 ].data.fd, fd,"epoll_wait_fd" )) + goto cleanup; +if (!ASSERT_TRUE (events[0 ].events &EPOLLHUP ,"events[0].events" )) + goto cleanup; + +cleanup: +close (epollfd);
Better check epollfd since it is init to -1. There are cases that epollfd is -1 here.
Quoted 21 lines
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +6
-0+bpf_link__destroy (link); +struct_ops_detach__destroy (skel); +} +void serial_test_struct_ops_module (void ) {if (test__start_subtest ("test_struct_ops_load" ))@@ -182,5 +237,7 @@ void serial_test_struct_ops_module(void)test_struct_ops_not_zeroed ();if (test__start_subtest ("test_struct_ops_incompatible" ))test_struct_ops_incompatible (); +if (test__start_subtest ("test_detach_link" )) +test_detach_link (); }tools/testing/selftests/bpf/progs/struct_ops_detach.c new file +0
-0
tools/testing/selftests/bpf/progs/struct_ops_detach.c line 7
Quoted 7 lines
tools/testing/selftests/bpf/progs/struct_ops_detach.c new file +7
-0@@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> +#include "../bpf_testmod/bpf_testmod.h" +#include "../bpf_testmod/bpf_testmod_kfunc.h"
The _kfunc.h should not be needed in this patch either.
tools/testing/selftests/bpf/progs/struct_ops_detach.c line 12
Quoted 5 lines
tools/testing/selftests/bpf/progs/struct_ops_detach.c new file +5
-0+ +char _license[] SEC("license") = "GPL"; + +int test_1_result =0 ; +int test_2_result =0 ;
Are these global vars tested? If not, can the test_1 and test_2 programs be removed? or some of them is not optional?
Quoted 19 lines
tools/testing/selftests/bpf/progs/struct_ops_detach.c new file +19
-0+ +SEC("struct_ops/test_1") +int BPF_PROG (test_1 ) +{ + test_1_result =0xdeadbeef ; +return 0 ; +} + +SEC("struct_ops/test_2") +void BPF_PROG (test_2 ,int a,int b) +{ + test_2_result = a + b; +} + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_do_detach = { + .test_1 = (void *)test_1, + .test_2 = (void *)test_2, +};
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v2 5/6] selftests/bpf: detach a struct_ops link from the subsystem managing it.
In reply to Kui-Feng LeeQuoted 18 lines · Kui-Feng Lee
Not only a user space program can detach a
struct_opslink, the subsystem managing a link can also detach the link. This patch add a kfunc to simulate detaching a link by the subsystem managing it.Signed-off-by Kui-Feng Lee <[email protected]>--- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 21 ++++++ .../bpf/prog_tests/test_struct_ops_module.c | 65 +++++++++++++++++++ .../selftests/bpf/progs/struct_ops_detach.c | 6 ++ 3 files changed, 92 insertions(+)tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +0
-0@@ -502,6 +502,26 @@ __bpf_kfunc void bpf_kfunc_call_test_sleepable(void)static DEFINE_MUTEX (detach_mutex);static struct bpf_link *link_to_detach;
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c __bpf_kfunc void bpf_kfunc_call_test_sleepable(void) · line 513
Quoted 10 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +9
-0+__bpf_kfunc int bpf_dummy_do_link_detach(void) +{ +struct bpf_link *link; +int ret = -ENOENT ; + +mutex_lock (&detach_mutex); + link = link_to_detach; +/* Make sure the link is still valid by increasing its refcnt */ +if (link && !atomic64_inc_not_zero (&link->refcnt))
It is better to reuse the bpf_link_inc_not_zero().
Quoted 15 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +11
-0+ link =NULL ; +mutex_unlock (&detach_mutex); + +if (link) { + ret = link->ops->detach (link); +bpf_link_put (link); + } + +return ret; +} + BTF_KFUNCS_START(bpf_testmod_check_kfunc_ids) BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc) BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE) · line 552
Quoted 4 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +1
-0@@ -529,6 +549,7 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE) BTF_ID_FLAGS(func, bpf_kfunc_call_test_static_unused_arg) BTF_ID_FLAGS(func, bpf_kfunc_call_test_offset) BTF_ID_FLAGS(func, bpf_kfunc_call_test_sleepable, KF_SLEEPABLE) +BTF_ID_FLAGS (func, bpf_dummy_do_link_detach)
It should need KF_SLEEPABLE. mutex is used.
Quoted 90 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +0
-0BTF_KFUNCS_END (bpf_testmod_check_kfunc_ids) static int bpf_testmod_ops_init(struct btf *btf)tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +65
-0@@ -229,6 +229,69 @@ static void test_detach_link(void)struct_ops_detach__destroy (skel); } +/* Detach a link from the subsystem that the link was registered to */ +static void test_subsystem_detach (void ) +{ +LIBBPF_OPTS (bpf_test_run_opts, topts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4)); +struct epoll_event ev, events[2 ]; +struct struct_ops_detach *skel; +struct bpf_link *link =NULL ; +int fd, epollfd =-1 , nfds; +int prog_fd; +int err; + + skel =struct_ops_detach__open_and_load (); +if (!ASSERT_OK_PTR (skel,"struct_ops_detach_open_and_load" )) +return ; + + link =bpf_map__attach_struct_ops (skel->maps.testmod_do_detach); +if (!ASSERT_OK_PTR (link,"attach_struct_ops" )) + goto cleanup; + + fd =bpf_link__fd (link); +if (!ASSERT_GE (fd,0 ,"link_fd" )) + goto cleanup; + + prog_fd =bpf_program__fd (skel->progs.start_detach); +if (!ASSERT_GE (prog_fd,0 ,"start_detach_fd" )) + goto cleanup; + +/* Do detachment from the registered subsystem */ + err =bpf_prog_test_run_opts (prog_fd, &topts); +if (!ASSERT_OK (err,"start_detach_run" )) + goto cleanup; + +if (!ASSERT_EQ (topts.retval,0 ,"start_detach_run retval" )) + goto cleanup; + + epollfd =epoll_create1 (0 ); +if (!ASSERT_GE (epollfd,0 ,"epoll_create1" )) + goto cleanup; + + ev.events =EPOLLHUP ; + ev.data.fd = fd; + err =epoll_ctl (epollfd,EPOLL_CTL_ADD , fd, &ev); +if (!ASSERT_OK (err,"epoll_ctl" )) + goto cleanup; + +/* Wait for EPOLLHUP */ + nfds =epoll_wait (epollfd, events,2 ,5000 ); +if (!ASSERT_EQ (nfds,1 ,"epoll_wait" )) + goto cleanup; + +if (!ASSERT_EQ (events[0 ].data.fd, fd,"epoll_wait_fd" )) + goto cleanup; +if (!ASSERT_TRUE (events[0 ].events &EPOLLHUP ,"events[0].events" )) + goto cleanup; + +cleanup: +close (epollfd); +bpf_link__destroy (link); +struct_ops_detach__destroy (skel); +} +void serial_test_struct_ops_module (void ) {if (test__start_subtest ("test_struct_ops_load" ))@@ -239,5 +302,7 @@ void serial_test_struct_ops_module(void)test_struct_ops_incompatible ();if (test__start_subtest ("test_detach_link" ))test_detach_link (); +if (test__start_subtest ("test_subsystem_detach" )) +test_subsystem_detach (); }tools/testing/selftests/bpf/progs/struct_ops_detach.c +0
-0
tools/testing/selftests/bpf/progs/struct_ops_detach.c struct bpf_testmod_ops testmod_do_detach = { · line 33
Quoted 5 lines
tools/testing/selftests/bpf/progs/struct_ops_detach.c +2
-0@@ -29,3 +29,9 @@ struct bpf_testmod_ops testmod_do_detach = { .test_1 = (void *)test_1, .test_2 = (void *)test_2, }; + +SEC ("tc" )
The bpf_dummy_do_link_detach() uses a mutex. There is no lockdep splat in the test?
Quoted 4 lines
tools/testing/selftests/bpf/progs/struct_ops_detach.c +4
-0+int start_detach (void *skb) +{ +return bpf_dummy_do_link_detach (); +}
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v2 6/6] selftests/bpf: make sure bpf_testmod handling racing link destroying well.
In reply to Kui-Feng LeeQuoted 11 lines · Kui-Feng Lee
Subsystems that manage
struct_opsobjects may attempt to detach a link when the link has been released or is about to be released. The test in this patch demonstrate to developers the correct way to handle this situation using a locking mechanism andatomic64_inc_not_zero().A subsystem must ensure that a link is valid when detaching the link. In order to achieve that, the subsystem may need to obtain a lock to safeguard a table that holds the pointer to the link being detached. However, the subsystem cannot invoke
link->ops->detach()while holding the lock because other tasks may be in the process of unregistering, which could lead to a deadlock. This is whyatomic64_inc_not_zero()is used to maintain the
Other tasks un-registering in parallel is not the reason for deadlock. The deadlock is because the link detach will call unreg() which usually will acquire the same lock (the detach_mutex here) and there is lock ordering with the update_mutex also. Hence, the link detach must be done after releasing the detach_mutex. After releasing the detach_mutex, the link is protected by its refcnt.
I think the above should be put as comments in bpf_dummy_do_link_detach for the subsystem to reference later.
Quoted 71 lines
link's validity. (Refer to
bpf_dummy_do_link_detach()in the previous patch for more details.)This test make sure the pattern mentioned above work correctly.
Signed-off-by Kui-Feng Lee <[email protected]>--- .../bpf/prog_tests/test_struct_ops_module.c | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+)tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +44
-0@@ -292,6 +292,48 @@ static void test_subsystem_detach(void)struct_ops_detach__destroy (skel); } +/* A subsystem detachs a link while the link is going to be free. */ +static void test_subsystem_detach_free (void ) +{ +LIBBPF_OPTS (bpf_test_run_opts, topts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4)); +struct struct_ops_detach *skel; +struct bpf_link *link =NULL ; +int prog_fd; +int err; + + skel =struct_ops_detach__open_and_load (); +if (!ASSERT_OK_PTR (skel,"struct_ops_detach_open_and_load" )) +return ; + + link =bpf_map__attach_struct_ops (skel->maps.testmod_do_detach); +if (!ASSERT_OK_PTR (link,"attach_struct_ops" )) + goto cleanup; + +bpf_link__destroy (link); + + prog_fd =bpf_program__fd (skel->progs.start_detach); +if (!ASSERT_GE (prog_fd,0 ,"start_detach_fd" )) + goto cleanup; + +/* Do detachment from the registered subsystem */ + err =bpf_prog_test_run_opts (prog_fd, &topts); +if (!ASSERT_OK (err,"start_detach_run" )) + goto cleanup; + +/* The link may have zero refcount value and may have been +* unregistered, so the detachment from the subsystem should fail. +*/ +ASSERT_EQ (topts.retval, (u32)-ENOENT ,"start_detach_run retval" ); + +/* Sync RCU to make sure the link is freed without any crash */ +ASSERT_OK (kern_sync_rcu (),"sync rcu" ); + +cleanup: +struct_ops_detach__destroy (skel); +} +void serial_test_struct_ops_module (void ) {if (test__start_subtest ("test_struct_ops_load" ))@@ -304,5 +346,7 @@ void serial_test_struct_ops_module(void)test_detach_link ();if (test__start_subtest ("test_subsystem_detach" ))test_subsystem_detach (); +if (test__start_subtest ("test_subsystem_detach_free" )) +test_subsystem_detach_free (); }
Re: [PATCH bpf-next v2 2/6] bpf: enable detaching links of struct_ops objects.
In reply to Martin KaFai LauQuoted 6 lines · Martin KaFai Lau
Quoted 3 lines · Kui-Feng Lee
Implement the detach callback in
bpf_link_opsforstruct_ops. The subsystems thatstruct_opsobjects are registered to can use this callback to detach the links being passed to them.The user space can also use the detach. The subsystem is merely reusing the similar detach callback if it stores the link during
reg().
Sure!
Quoted 62 lines
Quoted 62 lines
Signed-off-by Kui-Feng Lee <[email protected]>---
kernel/bpf/bpf_struct_ops.c| 50 ++++++++++++++++++++++++++++++++-----1 file changed, 44 insertions(+), 6 deletions(-)kernel/bpf/bpf_struct_ops.c +0
-3@@ -1057,9 +1057,6 @@ static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link) st_map = (struct bpf_struct_ops_map *) rcu_dereference_protected(st_link->map, true); if (st_map) { - /* st_link->map can be NULL if - * bpf_struct_ops_link_create() fails to register. - */ st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, st_link); bpf_map_put(&st_map->map);}
kernel/bpf/bpf_struct_ops.c +4
-2@@ -1075,7 +1072,8 @@ static void bpf_struct_ops_map_link_show_fdinfo(const struct bpf_link *link, st_link = container_of(link, struct bpf_struct_ops_link, link); rcu_read_lock(); map = rcu_dereference(st_link->map); - seq_printf(seq, "map_id:\t%d\n", map->id); + if (map) + seq_printf(seq, "map_id:\t%d\n", map->id); rcu_read_unlock(); }@@ -1088,7 +1086,8 @@ static int bpf_struct_ops_map_link_fill_link_info(const struct bpf_link *link, st_link = container_of(link, struct bpf_struct_ops_link, link); rcu_read_lock(); map = rcu_dereference(st_link->map); - info->struct_ops.map_id = map->id; + if (map) + info->struct_ops.map_id = map->id; rcu_read_unlock(); return 0;}
kernel/bpf/bpf_struct_ops.c +0
-0@@ -1113,6 +1112,10 @@ static intbpf_struct_ops_map_link_update(struct
bpf_link*link, structbpf_mapmutex_lock(&update_mutex);
old_map=rcu_dereference_protected(st_link->map, lockdep_is_held(&update_mutex)); + if (!old_map) {
+ err =-EINVAL;
+ gotoerr_out;
+ }
if (expected_old_map&&old_map!=expected_old_map) {
err =-EPERM;
gotoerr_out;kernel/bpf/bpf_struct_ops.c +0
-0@@ -1139,8 +1142,37 @@ static intbpf_struct_ops_map_link_update(struct
bpf_link*link, structbpf_mapreturn err;
}kernel/bpf/bpf_struct_ops.c +3
-0+static int bpf_struct_ops_map_link_detach (struct bpf_link *link) +{ + struct bpf_struct_ops_link *st_link = container_of(link, struct
bpf_struct_ops_link, link);
Quoted 8 lines
kernel/bpf/bpf_struct_ops.c static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map · line 1153
Quoted 6 lines
+ struct
bpf_struct_ops_map*st_map;
+ structbpf_map*map;kernel/bpf/bpf_struct_ops.c +4
-0+ + mutex_lock(&update_mutex); + + map = rcu_dereference_protected(st_link->map, true);nit. s/true/lockdep_is_held(&update_mutex)/
I thought it is protected by the refcount holding by the caller. WDYT?
Quoted 32 lines
Quoted 32 lines
+ if (!map) {
+mutex_unlock(&update_mutex);
+ return-EINVAL;
+ }
+st_map=container_of(map, struct bpf_struct_ops_map, map);kernel/bpf/bpf_struct_ops.c +14
-0+ + st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, link); + + rcu_assign_pointer(st_link->map, NULL); + /* Pair with bpf_map_get() in bpf_struct_ops_link_create() or + * bpf_map_inc() in bpf_struct_ops_map_link_update(). + */ + bpf_map_put(&st_map->map); + + mutex_unlock(&update_mutex); + + return 0; +} +static const struct
bpf_link_opsbpf_struct_ops_map_lops= { .dealloc =bpf_struct_ops_map_link_dealloc,
+ .detach =bpf_struct_ops_map_link_detach, .show_fdinfo =bpf_struct_ops_map_link_show_fdinfo, .fill_link_info =bpf_struct_ops_map_link_fill_link_info, .update_map =bpf_struct_ops_map_link_update,kernel/bpf/bpf_struct_ops.c +2
-0@@ -1176,13 +1208,19 @@ int bpf_struct_ops_link_create(union bpf_attr *attr) if (err) goto err_out; + /* Init link->map before calling reg() in case being detached +* immediately.
Quoted 8 lines
kernel/bpf/bpf_struct_ops.c int bpf_struct_ops_link_create(union bpf_attr *attr) · line 1218
Quoted 6 lines
kernel/bpf/bpf_struct_ops.c +4
-0+*/ + RCU_INIT_POINTER(link->map, map); + err = st_map->st_ops_desc->st_ops->reg(st_map->kvalue.data, &link->link); if (err) { + rcu_assign_pointer(link->map, NULL);nit.
RCU_INIT_POINTER(link->map, NULL)is fine.
Got it!
Quoted 2 lines
There is a merge conflict with patch 4 also.
What do you mean here? Do you mean the patch 4 can not be applied on top of the patch 2?
Quoted 11 lines
pw-bot: cr
Quoted 7 lines
kernel/bpf/bpf_struct_ops.c +1
-1bpf_link_cleanup(&link_primer); + /* The link has been free by bpf_link_cleanup() */ link = NULL; goto err_out; } - RCU_INIT_POINTER(link->map, map); return bpf_link_settle(&link_primer);
Re: [PATCH bpf-next v2 4/6] selftests/bpf: test struct_ops with epoll
In reply to Martin KaFai LauQuoted 44 lines · Martin KaFai Lau
Quoted 43 lines · Kui-Feng Lee
Verify whether a user space program is informed through epoll with EPOLLHUP when a
struct_opsobject is detached.Signed-off-by Kui-Feng Lee <[email protected]>---
.../selftests/bpf/bpf_testmod/bpf_testmod.c | 13 +++++ .../bpf/bpf_testmod/bpf_testmod_kfunc.h | 1 + .../bpf/prog_tests/test_struct_ops_module.c | 57 +++++++++++++++++++ .../selftests/bpf/progs/struct_ops_detach.c | 31 ++++++++++4 files changed, 102 insertions(+)create mode 100644
tools/testing/selftests/bpf/progs/struct_ops_detach.ctools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +9
-0@@ -10,6 +10,7 @@ #include <linux/percpu-defs.h> #include <linux/sysfs.h> #include <linux/tracepoint.h> +#include <linux/workqueue.h> #include "bpf_testmod.h" #include "bpf_testmod_kfunc.h"@@ -498,6 +499,9 @@ __bpf_kfunc void bpf_kfunc_call_test_sleepable(void) { } +static DEFINE_MUTEX (detach_mutex); +static struct bpf_link *link_to_detach; + BTF_KFUNCS_START(bpf_testmod_check_kfunc_ids) BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc) BTF_ID_FLAGS(func, bpf_kfunc_call_test1)@@ -577,11 +581,20 @@ static int bpf_dummy_reg(void *kdata, struct bpf_link *link) if (ops->test_2) ops->test_2(4, ops->data); + mutex_lock(&detach_mutex); + if (!link_to_detach) + link_to_detach = link; + mutex_unlock(&detach_mutex); + return 0; }
Quoted 8 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c static int bpf_dummy_reg(void *kdata, struct bpf_link *link) · line 597
Quoted 6 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +4
-0static void bpf_dummy_unreg(void *kdata, struct bpf_link *link) { + mutex_lock(&detach_mutex); + if (link == link_to_detach) + link_to_detach = NULL; + mutex_unlock(&detach_mutex);The reg/unreg changes should belong to the next patch.
Sure!
Quoted 15 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h void bpf_kfunc_call_test_fail1(struct · line 109
Quoted 13 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +0
-0} static int bpf_testmod_test_1(void)tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h +1
-0@@ -105,6 +105,7 @@ void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p); void bpf_kfunc_call_test_fail2(struct prog_test_fail2 *p); void bpf_kfunc_call_test_fail3(struct prog_test_fail3 *p); void bpf_kfunc_call_test_mem_len_fail1(void *mem, int len); +int bpf_dummy_do_link_detach(void) __ksym;The kfunc is not added in this patch either.
Sure!
Quoted 65 lines
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c line 5
Quoted 12 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h +0
-0void bpf_kfunc_common_test(void) __ksym; #endif /* _BPF_TESTMOD_KFUNC_H */tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +1
-0@@ -2,8 +2,12 @@ /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ #include <test_progs.h> #include <time.h> +#include <network_helpers.h> What is needed from
network_herlpers.h?Quoted 50 lines
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +44
-0+ +#include <sys/epoll.h> #include "struct_ops_module.skel.h" +#include "struct_ops_detach.skel.h" static void check_map_info(struct bpf_map_info *info) {@@ -174,6 +178,57 @@ static void test_struct_ops_incompatible(void) struct_ops_module__destroy(skel); } +/* Detach a link from a user space program */ +static void test_detach_link (void ) +{ + struct epoll_event ev, events[2]; + struct struct_ops_detach *skel; + struct bpf_link *link = NULL; + int fd, epollfd = -1, nfds; + int err; + + skel = struct_ops_detach__open_and_load(); + if (!ASSERT_OK_PTR(skel, "struct_ops_detach__open_and_load")) + return; + + link = bpf_map__attach_struct_ops(skel->maps.testmod_do_detach); + if (!ASSERT_OK_PTR(link, "attach_struct_ops")) + goto cleanup; + + fd = bpf_link__fd(link); + if (!ASSERT_GE(fd, 0, "link_fd")) + goto cleanup; + + epollfd = epoll_create1(0); + if (!ASSERT_GE(epollfd, 0, "epoll_create1")) + goto cleanup; + + ev.events = EPOLLHUP; + ev.data.fd = fd; + err = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev); + if (!ASSERT_OK(err, "epoll_ctl")) + goto cleanup; + + err = bpf_link__detach(link); + if (!ASSERT_OK(err, "detach_link")) + goto cleanup; + + /* Wait for EPOLLHUP */ + nfds = epoll_wait(epollfd, events, 2, 500); + if (!ASSERT_EQ(nfds, 1, "epoll_wait")) + goto cleanup; + + if (!ASSERT_EQ(events[0].data.fd, fd, "epoll_wait_fd"))
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c static void test_struct_ops_incompatible(void) · line 227
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +6
-0+ goto cleanup; + if (!ASSERT_TRUE(events[0].events & EPOLLHUP, "events[0].events")) + goto cleanup; + +cleanup: + close(epollfd);Better check epollfd since it is init to -1. There are cases that epollfd is -1 here.
Ok! Although close(-1) doesn't cause any issue, it makes sense doing check before calling it.
Quoted 22 lines
Quoted 22 lines
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +6
-0+ bpf_link__destroy(link); + struct_ops_detach__destroy(skel); +} + void serial_test_struct_ops_module(void) { if (test__start_subtest("test_struct_ops_load"))@@ -182,5 +237,7 @@ void serial_test_struct_ops_module(void) test_struct_ops_not_zeroed(); if (test__start_subtest("test_struct_ops_incompatible")) test_struct_ops_incompatible(); + if (test__start_subtest("test_detach_link")) + test_detach_link(); }tools/testing/selftests/bpf/progs/struct_ops_detach.c new file +1
-0@@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0
Quoted 8 lines
tools/testing/selftests/bpf/progs/struct_ops_detach.c line 7
Quoted 6 lines
tools/testing/selftests/bpf/progs/struct_ops_detach.c new file +6
-0+/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> +#include "../bpf_testmod/bpf_testmod.h" +#include "../bpf_testmod/bpf_testmod_kfunc.h" The
_kfunc.hshould not be needed in this patch either.
Sure!
Quoted 8 lines
tools/testing/selftests/bpf/progs/struct_ops_detach.c line 12
Quoted 6 lines
tools/testing/selftests/bpf/progs/struct_ops_detach.c new file +5
-0+ +char _license[] SEC("license") = "GPL"; + +int test_1_result =0 ; +int test_2_result =0 ;Are these global vars tested? If not, can the
test_1andtest_2programs be removed? or some of them is not optional?
Yes, they are optional. I will remove these functions.
Quoted 21 lines
Quoted 20 lines
tools/testing/selftests/bpf/progs/struct_ops_detach.c new file +19
-0+ +SEC("struct_ops/test_1") +int BPF_PROG (test_1 ) +{ + test_1_result = 0xdeadbeef; + return 0; +} + +SEC("struct_ops/test_2") +void BPF_PROG (test_2 ,int a,int b) +{ + test_2_result = a + b; +} + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_do_detach = { + .test_1 = (void *)test_1, + .test_2 = (void *)test_2, +};
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v2 2/6] bpf: enable detaching links of struct_ops objects.
In reply to Kui-Feng LeeQuoted 84 lines · Kui-Feng Lee
Quoted 6 lines · Martin KaFai Lau
Quoted 3 lines · Kui-Feng Lee
Implement the detach callback in
bpf_link_opsforstruct_ops. The subsystems thatstruct_opsobjects are registered to can use this callback to detach the links being passed to them.The user space can also use the detach. The subsystem is merely reusing the similar detach callback if it stores the link during
reg().Sure!
Quoted 62 lines
Quoted 62 lines
Signed-off-by Kui-Feng Lee <[email protected]>---
kernel/bpf/bpf_struct_ops.c| 50 ++++++++++++++++++++++++++++++++-----1 file changed, 44 insertions(+), 6 deletions(-)kernel/bpf/bpf_struct_ops.c +0
-3@@ -1057,9 +1057,6 @@ static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link) st_map = (struct bpf_struct_ops_map *) rcu_dereference_protected(st_link->map, true); if (st_map) { - /* st_link->map can be NULL if - * bpf_struct_ops_link_create() fails to register. - */ st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, st_link); bpf_map_put(&st_map->map);}
kernel/bpf/bpf_struct_ops.c +4
-2@@ -1075,7 +1072,8 @@ static void bpf_struct_ops_map_link_show_fdinfo(conststruct bpf_link *link, st_link = container_of(link, struct bpf_struct_ops_link, link); rcu_read_lock(); map = rcu_dereference(st_link->map); - seq_printf(seq, "map_id:\t%d\n", map->id); + if (map) + seq_printf(seq, "map_id:\t%d\n", map->id); rcu_read_unlock(); }@@ -1088,7 +1086,8 @@ static int bpf_struct_ops_map_link_fill_link_info(conststruct bpf_link *link, st_link = container_of(link, struct bpf_struct_ops_link, link); rcu_read_lock(); map = rcu_dereference(st_link->map); - info->struct_ops.map_id = map->id; + if (map) + info->struct_ops.map_id = map->id; rcu_read_unlock(); return 0;}
kernel/bpf/bpf_struct_ops.c +0
-0@@ -1113,6 +1112,10 @@ static int bpf_struct_ops_map_link_update(struct
bpf_link*link, structbpf_map
mutex_lock(&update_mutex);
old_map=rcu_dereference_protected(st_link->map, lockdep_is_held(&update_mutex)); + if (!old_map) {
+ err =-EINVAL;
+ gotoerr_out;
+ }
if (expected_old_map&&old_map!=expected_old_map) {
err =-EPERM;
gotoerr_out;kernel/bpf/bpf_struct_ops.c +0
-0@@ -1139,8 +1142,37 @@ static int bpf_struct_ops_map_link_update(struct
bpf_link*link, structbpf_map
return err;
}kernel/bpf/bpf_struct_ops.c +3
-0+static int bpf_struct_ops_map_link_detach (struct bpf_link *link) +{ + struct bpf_struct_ops_link *st_link = container_of(link, struct
bpf_struct_ops_link, link);Quoted 8 lines
kernel/bpf/bpf_struct_ops.c static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map · line 1153
Quoted 6 lines
+ struct
bpf_struct_ops_map*st_map;
+ structbpf_map*map;kernel/bpf/bpf_struct_ops.c +4
-0+ + mutex_lock(&update_mutex); + + map = rcu_dereference_protected(st_link->map, true);nit. s/true/lockdep_is_held(&update_mutex)/
Quoted 2 lines
I thought it is protected by the refcount holding by the caller. WDYT?
st_link->map is the one with __rcu tag and "!map" is tested next. I don't see how these imply the map pointer is protected by refcount. Can you explain?
Quoted 42 lines
Quoted 33 lines
Quoted 33 lines
+ if (!map) {
+mutex_unlock(&update_mutex);
+ return-EINVAL;
+ }
+st_map=container_of(map, struct bpf_struct_ops_map, map);kernel/bpf/bpf_struct_ops.c +14
-0+ + st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, link); + + rcu_assign_pointer(st_link->map, NULL); + /* Pair with bpf_map_get() in bpf_struct_ops_link_create() or + * bpf_map_inc() in bpf_struct_ops_map_link_update(). + */ + bpf_map_put(&st_map->map); + + mutex_unlock(&update_mutex); + + return 0; +} +static const struct
bpf_link_opsbpf_struct_ops_map_lops= { .dealloc =bpf_struct_ops_map_link_dealloc,
+ .detach =bpf_struct_ops_map_link_detach, .show_fdinfo =bpf_struct_ops_map_link_show_fdinfo, .fill_link_info =bpf_struct_ops_map_link_fill_link_info, .update_map =bpf_struct_ops_map_link_update,kernel/bpf/bpf_struct_ops.c +2
-0@@ -1176,13 +1208,19 @@ int bpf_struct_ops_link_create(union bpf_attr *attr) if (err) goto err_out; + /* Init link->map before calling reg() in case being detached +* immediately. Quoted 8 lines
kernel/bpf/bpf_struct_ops.c int bpf_struct_ops_link_create(union bpf_attr *attr) · line 1218
Quoted 6 lines
kernel/bpf/bpf_struct_ops.c +4
-0+*/ + RCU_INIT_POINTER(link->map, map); + err = st_map->st_ops_desc->st_ops->reg(st_map->kvalue.data, &link->link); if (err) { + rcu_assign_pointer(link->map, NULL);nit.
RCU_INIT_POINTER(link->map, NULL)is fine.
Quoted 7 lines
Got it!
Quoted 2 lines
There is a merge conflict with patch 4 also.
What do you mean here? Do you mean the patch 4 can not be applied on top of the patch 2?
Please monitor the bpf CI report.
bpf CI complains: https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
snippet of the error:
Applying: bpf: enable detaching links of struct_ops objects.
Applying: bpf: support epoll from bpf struct_ops links.
Applying: selftests/bpf: test struct_ops with epoll Patch failed at 0004 selftests/bpf: test struct_ops with epoll
Quoted 12 lines
Quoted 12 lines
pw-bot: cr
Quoted 7 lines
kernel/bpf/bpf_struct_ops.c +1
-1bpf_link_cleanup(&link_primer); + /* The link has been free by bpf_link_cleanup() */ link = NULL; goto err_out; } - RCU_INIT_POINTER(link->map, map); return bpf_link_settle(&link_primer);
Re: [PATCH bpf-next v2 2/6] bpf: enable detaching links of struct_ops objects.
In reply to Kui-Feng LeeQuoted 129 lines · Kui-Feng Lee
Quoted 6 lines · Martin KaFai Lau
Quoted 3 lines · Kui-Feng Lee
Implement the detach callback in
bpf_link_opsforstruct_ops. The subsystems thatstruct_opsobjects are registered to can use this callback to detach the links being passed to them.The user space can also use the detach. The subsystem is merely reusing the similar detach callback if it stores the link during
reg().Sure!
Quoted 62 lines
Quoted 62 lines
Signed-off-by Kui-Feng Lee <[email protected]>---
kernel/bpf/bpf_struct_ops.c| 50 ++++++++++++++++++++++++++++++++-----1 file changed, 44 insertions(+), 6 deletions(-)kernel/bpf/bpf_struct_ops.c +0
-3@@ -1057,9 +1057,6 @@ static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link) st_map = (struct bpf_struct_ops_map *) rcu_dereference_protected(st_link->map, true); if (st_map) { - /* st_link->map can be NULL if - * bpf_struct_ops_link_create() fails to register. - */ st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, st_link); bpf_map_put(&st_map->map);}
kernel/bpf/bpf_struct_ops.c +4
-2@@ -1075,7 +1072,8 @@ static void bpf_struct_ops_map_link_show_fdinfo(const struct bpf_link *link, st_link = container_of(link, struct bpf_struct_ops_link, link); rcu_read_lock(); map = rcu_dereference(st_link->map); - seq_printf(seq, "map_id:\t%d\n", map->id); + if (map) + seq_printf(seq, "map_id:\t%d\n", map->id); rcu_read_unlock(); }@@ -1088,7 +1086,8 @@ static int bpf_struct_ops_map_link_fill_link_info(const struct bpf_link *link, st_link = container_of(link, struct bpf_struct_ops_link, link); rcu_read_lock(); map = rcu_dereference(st_link->map); - info->struct_ops.map_id = map->id; + if (map) + info->struct_ops.map_id = map->id; rcu_read_unlock(); return 0;}
kernel/bpf/bpf_struct_ops.c +0
-0@@ -1113,6 +1112,10 @@ static intbpf_struct_ops_map_link_update(struct
bpf_link*link, structbpf_mapmutex_lock(&update_mutex);
old_map=rcu_dereference_protected(st_link->map, lockdep_is_held(&update_mutex)); + if (!old_map) {
+ err =-EINVAL;
+ gotoerr_out;
+ }
if (expected_old_map&&old_map!=expected_old_map) {
err =-EPERM;
gotoerr_out;kernel/bpf/bpf_struct_ops.c +0
-0@@ -1139,8 +1142,37 @@ static intbpf_struct_ops_map_link_update(struct
bpf_link*link, structbpf_mapreturn err;
}kernel/bpf/bpf_struct_ops.c +3
-0+static int bpf_struct_ops_map_link_detach (struct bpf_link *link) +{ + struct bpf_struct_ops_link *st_link = container_of(link, struct
bpf_struct_ops_link, link);Quoted 8 lines
kernel/bpf/bpf_struct_ops.c static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map · line 1153
Quoted 6 lines
+ struct
bpf_struct_ops_map*st_map;
+ structbpf_map*map;kernel/bpf/bpf_struct_ops.c +4
-0+ + mutex_lock(&update_mutex); + + map = rcu_dereference_protected(st_link->map, true);nit. s/true/lockdep_is_held(&update_mutex)/
I thought it is protected by the refcount holding by the caller. WDYT?
Quoted 32 lines
Quoted 32 lines
+ if (!map) {
+mutex_unlock(&update_mutex);
+ return-EINVAL;
+ }
+st_map=container_of(map, struct bpf_struct_ops_map, map);kernel/bpf/bpf_struct_ops.c +14
-0+ + st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, link); + + rcu_assign_pointer(st_link->map, NULL); + /* Pair with bpf_map_get() in bpf_struct_ops_link_create() or + * bpf_map_inc() in bpf_struct_ops_map_link_update(). + */ + bpf_map_put(&st_map->map); + + mutex_unlock(&update_mutex); + + return 0; +} +static const struct
bpf_link_opsbpf_struct_ops_map_lops= { .dealloc =bpf_struct_ops_map_link_dealloc,
+ .detach =bpf_struct_ops_map_link_detach, .show_fdinfo =bpf_struct_ops_map_link_show_fdinfo, .fill_link_info =bpf_struct_ops_map_link_fill_link_info, .update_map =bpf_struct_ops_map_link_update,kernel/bpf/bpf_struct_ops.c +2
-0@@ -1176,13 +1208,19 @@ int bpf_struct_ops_link_create(union bpf_attr *attr) if (err) goto err_out; + /* Init link->map before calling reg() in case being detached +* immediately. Quoted 8 lines
kernel/bpf/bpf_struct_ops.c int bpf_struct_ops_link_create(union bpf_attr *attr) · line 1218
Quoted 6 lines
kernel/bpf/bpf_struct_ops.c +4
-0+*/ + RCU_INIT_POINTER(link->map, map); + err = st_map->st_ops_desc->st_ops->reg(st_map->kvalue.data, &link->link); if (err) { + rcu_assign_pointer(link->map, NULL);nit.
RCU_INIT_POINTER(link->map, NULL)is fine.
Quoted 7 lines
Got it!
Quoted 2 lines
There is a merge conflict with patch 4 also.
What do you mean here? Do you mean the patch 4 can not be applied on top of the patch 2?
I have seen it after rebasing my local repository.
Quoted 12 lines
Quoted 12 lines
pw-bot: cr
Quoted 7 lines
kernel/bpf/bpf_struct_ops.c +1
-1bpf_link_cleanup(&link_primer); + /* The link has been free by bpf_link_cleanup() */ link = NULL; goto err_out; } - RCU_INIT_POINTER(link->map, map); return bpf_link_settle(&link_primer);
Re: [PATCH bpf-next v2 5/6] selftests/bpf: detach a struct_ops link from the subsystem managing it.
In reply to Martin KaFai LauQuoted 22 lines · Martin KaFai Lau
Quoted 21 lines · Kui-Feng Lee
Not only a user space program can detach a
struct_opslink, the subsystem managing a link can also detach the link. This patch add a kfunc to simulate detaching a link by the subsystem managing it.Signed-off-by Kui-Feng Lee <[email protected]>---
.../selftests/bpf/bpf_testmod/bpf_testmod.c | 21 ++++++ .../bpf/prog_tests/test_struct_ops_module.c | 65 +++++++++++++++++++ .../selftests/bpf/progs/struct_ops_detach.c | 6 ++ 3 files changed, 92 insertions(+)tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +3
-0@@ -502,6 +502,26 @@ __bpf_kfunc void bpf_kfunc_call_test_sleepable(void) static DEFINE_MUTEX(detach_mutex); static struct bpf_link *link_to_detach; +__bpf_kfunc int bpf_dummy_do_link_detach(void) +{ + struct bpf_link *link;
Quoted 8 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c __bpf_kfunc void bpf_kfunc_call_test_sleepable(void) · line 513
Quoted 6 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +6
-0+ int ret = -ENOENT; + + mutex_lock(&detach_mutex); + link = link_to_detach; + /* Make sure the link is still valid by increasing its refcnt */ + if (link && !atomic64_inc_not_zero(&link->refcnt))It is better to reuse the
bpf_link_inc_not_zero().
I will export this function to be used by modules.
Quoted 15 lines
Quoted 15 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +11
-0+ link = NULL; + mutex_unlock(&detach_mutex); + + if (link) { + ret = link->ops->detach(link); + bpf_link_put(link); + } + + return ret; +} + BTF_KFUNCS_START(bpf_testmod_check_kfunc_ids) BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc) BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
Quoted 8 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c BTF_ID_FLAGS(func, · line 553
Quoted 6 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +1
-0@@ -529,6 +549,7 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE) BTF_ID_FLAGS(func, bpf_kfunc_call_test_static_unused_arg) BTF_ID_FLAGS(func, bpf_kfunc_call_test_offset) BTF_ID_FLAGS(func, bpf_kfunc_call_test_sleepable, KF_SLEEPABLE) +BTF_ID_FLAGS (func, bpf_dummy_do_link_detach)It should need
KF_SLEEPABLE. mutex is used.
To simplify it, spinlock will be used instead.
Quoted 101 lines
Quoted 88 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +0
-0BTF_KFUNCS_END(bpf_testmod_check_kfunc_ids) static int bpf_testmod_ops_init(struct btf *btf)tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +65
-0@@ -229,6 +229,69 @@ static void test_detach_link(void) struct_ops_detach__destroy(skel); } +/* Detach a link from the subsystem that the link was registered to */ +static void test_subsystem_detach (void ) +{ + LIBBPF_OPTS(bpf_test_run_opts, topts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4)); + struct epoll_event ev, events[2]; + struct struct_ops_detach *skel; + struct bpf_link *link = NULL; + int fd, epollfd = -1, nfds; + int prog_fd; + int err; + + skel = struct_ops_detach__open_and_load(); + if (!ASSERT_OK_PTR(skel, "struct_ops_detach_open_and_load")) + return; + + link = bpf_map__attach_struct_ops(skel->maps.testmod_do_detach); + if (!ASSERT_OK_PTR(link, "attach_struct_ops")) + goto cleanup; + + fd = bpf_link__fd(link); + if (!ASSERT_GE(fd, 0, "link_fd")) + goto cleanup; + + prog_fd = bpf_program__fd(skel->progs.start_detach); + if (!ASSERT_GE(prog_fd, 0, "start_detach_fd")) + goto cleanup; + + /* Do detachment from the registered subsystem */ + err = bpf_prog_test_run_opts(prog_fd, &topts); + if (!ASSERT_OK(err, "start_detach_run")) + goto cleanup; + + if (!ASSERT_EQ(topts.retval, 0, "start_detach_run retval")) + goto cleanup; + + epollfd = epoll_create1(0); + if (!ASSERT_GE(epollfd, 0, "epoll_create1")) + goto cleanup; + + ev.events = EPOLLHUP; + ev.data.fd = fd; + err = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev); + if (!ASSERT_OK(err, "epoll_ctl")) + goto cleanup; + + /* Wait for EPOLLHUP */ + nfds = epoll_wait(epollfd, events, 2, 5000); + if (!ASSERT_EQ(nfds, 1, "epoll_wait")) + goto cleanup; + + if (!ASSERT_EQ(events[0].data.fd, fd, "epoll_wait_fd")) + goto cleanup; + if (!ASSERT_TRUE(events[0].events & EPOLLHUP, "events[0].events")) + goto cleanup; + +cleanup: + close(epollfd); + bpf_link__destroy(link); + struct_ops_detach__destroy(skel); +} + void serial_test_struct_ops_module(void) { if (test__start_subtest("test_struct_ops_load"))@@ -239,5 +302,7 @@ void serial_test_struct_ops_module(void) test_struct_ops_incompatible(); if (test__start_subtest("test_detach_link")) test_detach_link(); + if (test__start_subtest("test_subsystem_detach")) + test_subsystem_detach(); }tools/testing/selftests/bpf/progs/struct_ops_detach.c +0
-0tools/testing/selftests/bpf/progs/struct_ops_detach.c struct bpf_testmod_ops testmod_do_detach = { · line 33
Quoted 5 lines
tools/testing/selftests/bpf/progs/struct_ops_detach.c +2
-0@@ -29,3 +29,9 @@ struct bpf_testmod_ops testmod_do_detach = { .test_1 = (void *)test_1, .test_2 = (void *)test_2, }; + +SEC ("tc" )The
bpf_dummy_do_link_detach()uses a mutex. There is no lockdep splat in the test?Quoted 4 lines
tools/testing/selftests/bpf/progs/struct_ops_detach.c +4
-0+int start_detach (void *skb) +{ + return bpf_dummy_do_link_detach(); +}
Re: [PATCH bpf-next v2 2/6] bpf: enable detaching links of struct_ops objects.
In reply to Martin KaFai LauQuoted 86 lines · Martin KaFai Lau
Quoted 85 lines · Kui-Feng Lee
Quoted 6 lines · Martin KaFai Lau
Quoted 3 lines · Kui-Feng Lee
Implement the detach callback in
bpf_link_opsforstruct_ops. The subsystems thatstruct_opsobjects are registered to can use this callback to detach the links being passed to them.The user space can also use the detach. The subsystem is merely reusing the similar detach callback if it stores the link during
reg().Sure!
Quoted 71 lines
Quoted 65 lines
Signed-off-by Kui-Feng Lee <[email protected]>---
kernel/bpf/bpf_struct_ops.c| 50
++++++++++++++++++++++++++++++++-----1 file changed, 44 insertions(+), 6 deletions(-)kernel/bpf/bpf_struct_ops.c +0
-3@@ -1057,9 +1057,6 @@ static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link) st_map = (struct bpf_struct_ops_map *) rcu_dereference_protected(st_link->map, true); if (st_map) { - /* st_link->map can be NULL if - * bpf_struct_ops_link_create() fails to register. - */ st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, st_link); bpf_map_put(&st_map->map);}
kernel/bpf/bpf_struct_ops.c +4
-2@@ -1075,7 +1072,8 @@ static void bpf_struct_ops_map_link_show_fdinfo(const struct bpf_link *link, st_link = container_of(link, struct bpf_struct_ops_link, link); rcu_read_lock(); map = rcu_dereference(st_link->map); - seq_printf(seq, "map_id:\t%d\n", map->id); + if (map) + seq_printf(seq, "map_id:\t%d\n", map->id); rcu_read_unlock(); }@@ -1088,7 +1086,8 @@ static int bpf_struct_ops_map_link_fill_link_info(const struct bpf_link *link, st_link = container_of(link, struct bpf_struct_ops_link, link); rcu_read_lock(); map = rcu_dereference(st_link->map); - info->struct_ops.map_id = map->id; + if (map) + info->struct_ops.map_id = map->id; rcu_read_unlock(); return 0;}
kernel/bpf/bpf_struct_ops.c +0
-0@@ -1113,6 +1112,10 @@ static intbpf_struct_ops_map_link_update(struct
bpf_link*link, structbpf_mapmutex_lock(&update_mutex);
old_map=rcu_dereference_protected(st_link->map, lockdep_is_held(&update_mutex)); + if (!old_map) {
+ err =-EINVAL;
+ gotoerr_out;
+ }
if (expected_old_map&&old_map!=expected_old_map) {
err =-EPERM;
gotoerr_out;kernel/bpf/bpf_struct_ops.c +0
-0@@ -1139,8 +1142,37 @@ static intbpf_struct_ops_map_link_update(struct
bpf_link*link, structbpf_mapreturn err;
}kernel/bpf/bpf_struct_ops.c +3
-0+static int bpf_struct_ops_map_link_detach (struct bpf_link *link) +{ + struct bpf_struct_ops_link *st_link = container_of(link, struct
bpf_struct_ops_link, link);
+ structbpf_struct_ops_map*st_map;
+ structbpf_map*map;kernel/bpf/bpf_struct_ops.c static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map · line 1153
Quoted 4 lines
kernel/bpf/bpf_struct_ops.c +4
-0+ + mutex_lock(&update_mutex); + + map = rcu_dereference_protected(st_link->map, true);nit. s/true/lockdep_is_held(&update_mutex)/
Quoted 5 lines
Quoted 2 lines
I thought it is protected by the refcount holding by the caller. WDYT?
st_link->mapis the one with__rcutag and "!map" is tested next. I don't see how these imply the map pointer is protected by refcount. Can you explain?
Ok! You are right. I confused links with maps.
Quoted 53 lines
Quoted 43 lines
Quoted 34 lines
Quoted 34 lines
+ if (!map) {
+mutex_unlock(&update_mutex);
+ return-EINVAL;
+ }
+st_map=container_of(map, struct bpf_struct_ops_map, map);kernel/bpf/bpf_struct_ops.c +14
-0+ + st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, link); + + rcu_assign_pointer(st_link->map, NULL); + /* Pair with bpf_map_get() in bpf_struct_ops_link_create() or + * bpf_map_inc() in bpf_struct_ops_map_link_update(). + */ + bpf_map_put(&st_map->map); + + mutex_unlock(&update_mutex); + + return 0; +} +static const struct
bpf_link_opsbpf_struct_ops_map_lops= { .dealloc =bpf_struct_ops_map_link_dealloc,
+ .detach =bpf_struct_ops_map_link_detach, .show_fdinfo =bpf_struct_ops_map_link_show_fdinfo, .fill_link_info =bpf_struct_ops_map_link_fill_link_info, .update_map =bpf_struct_ops_map_link_update,kernel/bpf/bpf_struct_ops.c +2
-0@@ -1176,13 +1208,19 @@ int bpf_struct_ops_link_create(union bpf_attr *attr) if (err) goto err_out; + /* Init link->map before calling reg() in case being detached +* immediately. Quoted 8 lines
kernel/bpf/bpf_struct_ops.c int bpf_struct_ops_link_create(union bpf_attr *attr) · line 1218
Quoted 6 lines
kernel/bpf/bpf_struct_ops.c +4
-0+*/ + RCU_INIT_POINTER(link->map, map); + err = st_map->st_ops_desc->st_ops->reg(st_map->kvalue.data, &link->link); if (err) { + rcu_assign_pointer(link->map, NULL);nit.
RCU_INIT_POINTER(link->map, NULL)is fine.Quoted 7 lines
Got it!
Quoted 2 lines
There is a merge conflict with patch 4 also.
What do you mean here? Do you mean the patch 4 can not be applied on top of the patch 2?
Please monitor the bpf CI report.
Quoted 8 lines
bpf CI complains: https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
snippet of the error:
Applying: bpf: enable detaching links of
struct_opsobjects.
Applying: bpf: support epoll from bpfstruct_opslinks.
Applying: selftests/bpf: teststruct_opswith epoll Patch failed at 0004 selftests/bpf: teststruct_opswith epoll
Yes! I found it when I rebased local repository.
Quoted 14 lines
Quoted 13 lines
Quoted 13 lines
pw-bot: cr
Quoted 7 lines
kernel/bpf/bpf_struct_ops.c +1
-1bpf_link_cleanup(&link_primer); + /* The link has been free by bpf_link_cleanup() */ link = NULL; goto err_out; } - RCU_INIT_POINTER(link->map, map); return bpf_link_settle(&link_primer);
Re: [PATCH bpf-next v2 6/6] selftests/bpf: make sure bpf_testmod handling racing link destroying well.
In reply to Martin KaFai LauQuoted 6 lines · Martin KaFai Lau
Quoted 5 lines · Kui-Feng Lee
Subsystems that manage
struct_opsobjects may attempt to detach a link when the link has been released or is about to be released. The test in this patch demonstrate to developers the correct way to handle this situation using a locking mechanism andatomic64_inc_not_zero().
Quoted 8 lines
Quoted 6 lines
A subsystem must ensure that a link is valid when detaching the link. In order to achieve that, the subsystem may need to obtain a lock to safeguard a table that holds the pointer to the link being detached. However, the subsystem cannot invoke
link->ops->detach()while holding the lock because other tasks may be in the process of unregistering, which could lead to a deadlock. This is whyatomic64_inc_not_zero()is used to maintain theOther tasks un-registering in parallel is not the reason for deadlock. The deadlock is because the link detach will call
unreg()which usually will acquire the same lock (thedetach_mutexhere) and there is lock ordering with theupdate_mutexalso. Hence, the link detach must be done after releasing thedetach_mutex. After releasing thedetach_mutex, the link is protected by its refcnt.
It is what I mean in the commit log. I will rephrase it to emphasize holding the same lock.
Quoted 2 lines
I think the above should be put as comments in
bpf_dummy_do_link_detachfor the subsystem to reference later.
ok!
Quoted 71 lines
Quoted 70 lines
link's validity. (Refer to
bpf_dummy_do_link_detach()in the previous patch for more details.)This test make sure the pattern mentioned above work correctly.
Signed-off-by Kui-Feng Lee <[email protected]>---
.../bpf/prog_tests/test_struct_ops_module.c | 44 +++++++++++++++++++1 file changed, 44 insertions(+)tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +44
-0@@ -292,6 +292,48 @@ static void test_subsystem_detach(void) struct_ops_detach__destroy(skel); } +/* A subsystem detachs a link while the link is going to be free. */ +static void test_subsystem_detach_free (void ) +{ + LIBBPF_OPTS(bpf_test_run_opts, topts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4)); + struct struct_ops_detach *skel; + struct bpf_link *link = NULL; + int prog_fd; + int err; + + skel = struct_ops_detach__open_and_load(); + if (!ASSERT_OK_PTR(skel, "struct_ops_detach_open_and_load")) + return; + + link = bpf_map__attach_struct_ops(skel->maps.testmod_do_detach); + if (!ASSERT_OK_PTR(link, "attach_struct_ops")) + goto cleanup; + + bpf_link__destroy(link); + + prog_fd = bpf_program__fd(skel->progs.start_detach); + if (!ASSERT_GE(prog_fd, 0, "start_detach_fd")) + goto cleanup; + + /* Do detachment from the registered subsystem */ + err = bpf_prog_test_run_opts(prog_fd, &topts); + if (!ASSERT_OK(err, "start_detach_run")) + goto cleanup; + + /* The link may have zero refcount value and may have been + * unregistered, so the detachment from the subsystem should fail. + */ + ASSERT_EQ(topts.retval, (u32)-ENOENT, "start_detach_run retval"); + + /* Sync RCU to make sure the link is freed without any crash */ + ASSERT_OK(kern_sync_rcu(), "sync rcu"); + +cleanup: + struct_ops_detach__destroy(skel); +} + void serial_test_struct_ops_module(void) { if (test__start_subtest("test_struct_ops_load"))@@ -304,5 +346,7 @@ void serial_test_struct_ops_module(void) test_detach_link(); if (test__start_subtest("test_subsystem_detach")) test_subsystem_detach(); + if (test__start_subtest("test_subsystem_detach_free")) + test_subsystem_detach_free(); }