When the comparator unwinds the node is not in the tree, and the caller has let go of it: the kfunc releases the node whichever way it returns. The pad drops it, as the error path for a node that is already in a tree does, and leaves. The lock on the root is the caller's, and its pad is checked for it like any exit. Both spellings of the kfunc get it, each with its own copy of the body: the pad leaves from the frame that the BPF code called, and bpf_rbtree_add_impl() calling bpf_rbtree_add() is a tail jump on x86-64 but a call with a frame of its own on arm64, where the pad would rewrite a return address into that frame and the exception would come back as a return of 0. The kfunc returns long in both spellings: the pad leaves with the r0 of the comparator, and an int would cut it to 32 bits. The change shows in BTF, and libbpf takes integers of any width as compatible, so a declaration that says int keeps loading. On the revisit of the call after the comparator unwound, the node is not in the tree: the kfunc dropped it. So the verifier releases the reference, and every register that held the node is gone with it, rather than turning it into a non-owning reference to a node in the tree. The comparator was handed the node too, as a non-owning reference, and may have unwound with it in r0, so every non-owning reference goes, as it does at an unlock. With that the verifier lets the comparator of bpf_rbtree_add() unwind, in both spellings. Signed-off-by: Kumar Kartikeya Dwivedi --- kernel/bpf/helpers.c | 50 +++++++++++++++++++++++++++++-------------- kernel/bpf/verifier.c | 20 ++++++++++++++--- 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 648abe1ba63a..f8708b7181d7 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2,6 +2,7 @@ /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com */ #include +#include #include #include #include @@ -2761,17 +2762,20 @@ __bpf_kfunc struct bpf_rb_node *bpf_rbtree_remove(struct bpf_rb_root *root, return (struct bpf_rb_node *)n; } -/* Need to copy rbtree_add_cached's logic here because our 'less' is a BPF - * program +/* + * Need to copy rbtree_add_cached's logic here because our 'less' is a BPF + * program. The pad leaves from the frame that the BPF code called, so the body + * is inlined into both spellings of the kfunc. */ -static int __bpf_rbtree_add(struct bpf_rb_root *root, - struct bpf_rb_node_kern *node, - void *less, struct btf_record *rec, u64 off) +static __always_inline long __bpf_rbtree_add(struct bpf_rb_root *root, + struct bpf_rb_node_kern *node, + void *less, struct btf_record *rec, u64 off) { struct rb_node **link = &((struct rb_root_cached *)root)->rb_root.rb_node; struct rb_node *parent = NULL, *n = &node->rb_node; bpf_callback_t cb = (bpf_callback_t)less; bool leftmost = true; + u64 r0; /* node->owner != NULL implies !RB_EMPTY_NODE(n), no need to separately * check the latter @@ -2784,7 +2788,8 @@ static int __bpf_rbtree_add(struct bpf_rb_root *root, while (*link) { parent = *link; - if (cb((uintptr_t)node, (uintptr_t)parent, 0, 0, 0)) { + bpf_call_cb(cb, node, parent, 0, 0, 0, r0, unwound); + if (r0) { link = &parent->rb_left; } else { link = &parent->rb_right; @@ -2796,6 +2801,15 @@ static int __bpf_rbtree_add(struct bpf_rb_root *root, rb_insert_color_cached(n, (struct rb_root_cached *)root, leftmost); WRITE_ONCE(node->owner, root); return 0; + +unwound: + /* + * The comparator left with unwind_exit. The node is not in the tree, and + * the caller has let go of it: drop it, as the error path above does. + */ + __bpf_obj_drop_impl((void *)n - off, rec, false); + bpf_unwind_leave(); + return r0; } /** @@ -2809,24 +2823,28 @@ static int __bpf_rbtree_add(struct bpf_rb_root *root, * Insert @node into @root using @less. The verifier rewrites @meta and @off; * BPF programs do not set them. * - * Return: 0 on success, or %-EINVAL if @node is already linked in a tree. + * Return: 0 on success, or %-EINVAL if @node is already linked in a tree. When + * @less unwinds, what it unwound with, to the pad after the call. */ -__bpf_kfunc int bpf_rbtree_add(struct bpf_rb_root *root, - struct bpf_rb_node *node, - bool (less)(struct bpf_rb_node *a, const struct bpf_rb_node *b), - struct btf_struct_meta *meta, - u64 off) +__bpf_kfunc long bpf_rbtree_add(struct bpf_rb_root *root, + struct bpf_rb_node *node, + bool (less)(struct bpf_rb_node *a, const struct bpf_rb_node *b), + struct btf_struct_meta *meta, + u64 off) { struct bpf_rb_node_kern *n = (void *)node; return __bpf_rbtree_add(root, n, (void *)less, meta ? meta->record : NULL, off); } -__bpf_kfunc int bpf_rbtree_add_impl(struct bpf_rb_root *root, struct bpf_rb_node *node, - bool (less)(struct bpf_rb_node *a, const struct bpf_rb_node *b), - void *meta__ign, u64 off) +__bpf_kfunc long bpf_rbtree_add_impl(struct bpf_rb_root *root, struct bpf_rb_node *node, + bool (less)(struct bpf_rb_node *a, const struct bpf_rb_node *b), + void *meta__ign, u64 off) { - return bpf_rbtree_add(root, node, less, meta__ign, off); + struct btf_struct_meta *meta = meta__ign; + struct bpf_rb_node_kern *n = (void *)node; + + return __bpf_rbtree_add(root, n, (void *)less, meta ? meta->record : NULL, off); } __bpf_kfunc struct bpf_rb_node *bpf_rbtree_first(struct bpf_rb_root *root) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index fd63ff36eb90..50c3d44c37e1 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -11416,10 +11416,10 @@ static bool helper_has_cb_pad(enum bpf_func_id func_id) } } -/* The kfunc calls its callback with a pad. None yet. */ +/* The kfunc calls its callback with a pad */ static bool kfunc_has_cb_pad(u32 btf_id) { - return false; + return is_bpf_rbtree_add_kfunc(btf_id); } static bool insn_has_cb_pad(struct bpf_insn *insn) @@ -15738,7 +15738,21 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, id = regs[BPF_REG_2].id; insn_aux->insert_off = regs[BPF_REG_2].var_off.value; insn_aux->kptr_struct_meta = btf_find_struct_meta(meta.arg_btf, meta.arg_btf_id); - ref_convert_owning_non_owning(env, id); + /* + * The comparator unwound: the node never went in, and the + * kfunc dropped it on its way to the pad. The comparator was + * handed the node too, and may have unwound with it in r0, so + * every non-owning reference goes. Otherwise the node is in + * the tree now. + */ + if (env->cur_state->unwound) { + invalidate_non_owning_refs(env); + err = release_reference(env, id); + if (err) + return err; + } else { + ref_convert_owning_non_owning(env, id); + } } if (meta.func_id == special_kfunc_list[KF_bpf_throw]) { -- 2.53.0