Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.134 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100065; b=KnmYB75Aj1BlUtDmPEvhEwtA9HhyflcF2bo+v/nRRTSTcHXAqBdmvB+91x7YgQTndj5p SACnGOchcXfntcuBx8rlOzfFDENA8o8NNfBtAywWcW9W+/9RDGBOen4kb34nm8ndntFXT FQvOHkxVOn7pDh9IlSo500lSgTTHaXO8C0vMNBsLWY10tkTt9AFUkNzWe5FmoBWKEZ2UN ya/P4a1dc1Nn4g9oTsdlba21ppzgvdtBvvuZ8jQJDMbSywkeyQnZOSQ38B0btKJagIIS7 4k/39cAodTP8AMNstS7yd9rHThghb1SCPMFeSkFfgAWWKNiOtZ4lfsQ9Y5mv6sVhLlQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100065; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: X-Developer-Signature:X-Developer-Key:Content-Transfer-Encoding; bh=QbfXvXyVTuRv5DQf5CEPwv7yP8CVf28/9YC3QWTKJog=; b=EYxNwdvhOEiwCMxy7QJc1wLvSY9FxRrvmonczLx8A8h17PiNmmoyO0VrGVOhEuR8VT6Q W1d/MepaPId0567l5AaHL8ULk/J4q6ShilSCIiQbqu0HfN7WCPJLd5a7NGMQTw8X31/ri a0B5O261ttn/CJnmXy1qHj0mH9ViNnsg8nKi/4BW4cwu+IxU0ParbV74HTgvoFbiQuvrh dKm4ZELUUCIZD0KJHLpR4cR+yuVIfYJPcU92BhMqAlgu96eojD6aM/c40maLiFyyqf0Rt 9DcbU0pVQVKXuWFK6mbNK2IVzjrRCn2bUo4QpP3jJyi4h7ya6HmN/+QzpA01ppbGhUg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=let9XU5U; arc=none smtp.remote-ip=74.125.231.134 Received: from mail-oo2-f6.google.com (mail-oo2-f6.google.com [74.125.231.134]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A18A606A320 for ; Tue, 1 Dec 2026 04:41:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2A18A606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=let9XU5U Received: by mail-oo2-f6.google.com with SMTP id 46e09a7af769-8629eb92bebso3903621a34.1 for ; Mon, 30 Nov 2026 20:41:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796100064; x=1796704864; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=QbfXvXyVTuRv5DQf5CEPwv7yP8CVf28/9YC3QWTKJog=; b=let9XU5UEoc1Sgr5myFE+mmwyfH2lXxAsM36B8cz58pyy89g2lrsAM4Ujghj8uaUcO O6QFoNc4DKIQoJL1JXNsuooNLlFESPVFhnGPmTPaXx/qkIJ06zqiCwUCzI8uL/8cbQqg SqRDZqWRISAUjZmmca+vNtMEt8MIq1VRsQn5o2MvdSznQZlhP1FnpwjlVUNmPAa8Pvj/ eqIqfCQeDczSKe9QAi4bHPGAyZQsaDKBejmvw0vMQn3R5CYVTSNsE7yyNZiUJY5RH9OL Rim8dR3gCCDEQN4Kl9+Uvue+lbaZZISD6whokra2LllNnaKus+hyMR2q3k33EHL1/Qsl RsLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100064; x=1796704864; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=QbfXvXyVTuRv5DQf5CEPwv7yP8CVf28/9YC3QWTKJog=; b=eEvcFw8q4ShsERB/96jpSRfq7w2SmdvLu06l4HPcp5B/7Qf+RhhHrB1Yu87z5hTql3 WWuLo1a3dqhyZ02dKDVYOO7IiT3ZkNNigMYcwIFhSKIQSBexpoK8QYuA843w0kHRx/eG htgGUBAaVJm5UVItnIWggwdepA6IJmK892SlDUeoEA7bh6esJUpm+kacBZHocu5nVIc4 ipZnJXY3l2fbiJElCRNi9C0uN1m1hMsLDnEckqu8uNiSVA0QsjAcjrhp+pNUEW42PhPM 7O7qWszxYogBzurAAexSEGEBQlUnXG+ED2ICJnvndxQmSYr//Rvt93fvjCnZZofUmxWi QgCQ== X-Gm-Message-State: APuU4Z8u0FJ2Rxqpi46Ir6vXEmPg7MIJ0zzb1GSYle6bulfag9unpmPg QY/E1CsrsojfLWme6+KHCX+5q0f02T40poj8jrg4qlZZofekhCpHFHj9mINbqydMfHw= X-Gm-Gg: AZFdTZc69VCKYbdZac3ObqTwXbI/Zgn7SAaId4LceA/DBmnAHg1lGsBZMTfdHrDXbXt 5uwgPLxK245IMGt5mx2pCBPEc1LD2WpbsZ+AUzykk1dQ/5wiqEV7DGNP48cVybxde5S90vlsKan jr+RfDLCnXAxNO81PMxIc9M6UBIKgYEaidOProsqJRGAudB6qGWFNZiWtWRyNFNsBNz3xEbsgYK 28YCchLizPTqC0tXQgYs15AO6/gxLer+OD8qVmLV10L50rpBc7hsHxVaU5L4YwqgwS/d9uNjz60 Y0kzte0xeLnDI+wYqL3VJW51ffwirBTipPo+CP7Hl8a1W8B8yrqR2l+Aor84cdMt6N+fnRxqmkT hkQ+B/PHzrn0v2nsE7F42cFobXjzD8f5YmZzMgAA3HwsvYBvLDkFptV2VvpWkq3JsFrsAsXmY/G f6BsjNKfxf8vXmUNXWwuoIyJRcuInudbu2xL+KXYapqZDXa36mad2ara7NEdO2OZQtrKJrqySz1 xnBRFNBC5FWG92wUp2UPGsWkdgQY1LHLqy+JypP8Uw9od0cx81nJmo44LWsijI1KmZ/Ua0ShX2o nXbK7+1G2EON8pZFCGqU3S7RdWCFNpQybXU2blJr/ytZ6y26r53EReIfbP3LxuxsaOdUClSCgLy 1c8e/UHGX6ZhdK/96K4kPNNAKrxCJZSemI/ow X-Received: by 2002:a05:6830:913:b0:862:9ff6:53b6 with SMTP id 46e09a7af769-86315a6d2aemr41992319a34.23.1796100063883; Mon, 30 Nov 2026 20:41:03 -0800 (PST) Received: from localhost ([2a03:2880:10ff:57::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863168dd6besm25097297a34.11.2026.11.30.20.41.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:41:02 -0800 (PST) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Emil Tsalapatis , Ihor Solodrai , kkd@meta.com, kernel-team@meta.com Subject: [PATCH bpf-next v1 21/73] bpf: Add unwind_goto after helpers with a callback Date: Tue, 1 Dec 2026 05:38:48 +0100 Message-ID: <5f8f3b50bf8a5eb438f1f0a17f314d5ebcb9d5aa.1796099737.git.memxor@gmail.com> X-Mailer: git-send-email 2.53.1 In-Reply-To: References: X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=17863; h=from:subject:message-id; bh=zygmzSYf6dqp0JT2PUrVDxpFSOHjFj+KFH0kodaYgRQ=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZvPb+6Uv39vni18N+8Ud+HTu0Z2Luc3LMuN+f6Ccc9n+dPZ h2x+dZSyMIhxMciKKbKU/N/HZHyi8neg7TJumDmsTCBDGLg4BWAivlcY/qm1zz9Y0P/lmeL0kB82lz 4q+p/5mLv6z+Edb7amN129+tqfkWF1yqT2C06/BR//f77Zh+Uet8uUia5ZJacdTX5u8ZKyW84BAA== X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit Currently the kernel cannot cancel a program inside a callback of bpf_loop() or of another helper. A callback may leave with unwind_exit only if the program has an unwind_goto after the call of the helper, and the verifier treats a callback and what it calls as functions that cannot leave with unwind_exit. So a may_goto that it adds there leaves the loop. Add an unwind_goto after the call of a helper that has an unwind label for its callback, with a landing pad, as after the call of a subprog. '+' marks what the verifier adds: cb: caller: call bpf_rcu_read_lock call bpf_loop 1: ... .-> + unwind_goto pad --. + may_goto pad --------------. | ... | if r0 == 0 goto 1b | | exit | ... | | + pad: r0 = 0 <----------' + pad: call bpf_rcu_read_unlock <-' | + exit + r0 = 0 | + unwind_exit --> bpf_loop() ---' The unwind_exit of the callback goes to the unwind label of bpf_loop(), and from there to the unwind_goto after the call. For the rules about which function can leave with unwind_exit, such a helper is like callx. Three kinds of callback stay as they are: - A subprog whose address goes only to a helper that keeps it for later (a timer, a work queue, call_rcu) is called by the kernel alone. Its landing pads end in exit. - The callback of a helper without an unwind label cannot be cancelled, for example that of any helper outside x86-64 and arm64. - bpf_rbtree_add() drops the node at its unwind label. It must not do that for a type with NMI unsafe fields in a program that may run in NMI context, nor for a type with a list head, an rb root or a referenced kptr. A comparator of such a type cannot be cancelled, and one that has unwind_exit or is recursive is rejected as before. What changes for programs that load today: - When a loop of a callback has run too long, the program ends. Before, may_goto left the loop and the callback continued. - Only fentry can attach to a callback in which the program can be cancelled. - A program without unwind_goto after the call of a helper whose callback has unwind_exit loads. Co-developed-by: Alexei Starovoitov Signed-off-by: Alexei Starovoitov Signed-off-by: Kumar Kartikeya Dwivedi --- include/linux/bpf.h | 1 + include/linux/bpf_verifier.h | 2 +- kernel/bpf/pads.c | 2 + kernel/bpf/verifier.c | 140 ++++++++++-------- .../selftests/bpf/progs/verifier_unwind.c | 4 +- 5 files changed, 81 insertions(+), 68 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 4f9c2bf64643..915034e8acc2 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -182,6 +182,7 @@ struct bpf_map_ops { int (*map_set_for_each_callback_args)(struct bpf_verifier_env *env, struct bpf_func_state *caller, struct bpf_func_state *callee); + /* It has an unwind label for callback_fn, which may unwind, see bpf_call_cb() */ long (*map_for_each_callback)(struct bpf_map *map, bpf_callback_t callback_fn, void *callback_ctx, u64 flags); diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 5c34ace286df..2853a463df52 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -1361,7 +1361,7 @@ struct bpf_subprog_info { bool on_its_own: 1; /* * It can leave with unwind_exit: the verifier adds the unwind_goto that its calls lack. Not - * if a helper calls it, see subprog_cannot_unwind(). + * if a helper without an unwind label calls it, see subprog_cannot_unwind(). */ bool bail_unwinds: 1; /* diff --git a/kernel/bpf/pads.c b/kernel/bpf/pads.c index 6027e9cca9cc..4e3a7894af84 100644 --- a/kernel/bpf/pads.c +++ b/kernel/bpf/pads.c @@ -65,6 +65,8 @@ * + pad: r0 = 0 <-----------' | + pad: call bpf_rcu_read_unlock <-' * + unwind_exit ------------------' + r0 = 0; exit * + * A helper that calls a callback is a C frame between the callback and its caller. It passes a + * cancellation on from its unwind label to the unwind_goto after its own call, see bpf_call_cb(). * The frame that the kernel called leaves with exit, and the program returns the value of the * cancellation: bpf_cancelled_retval(), which goes by the program type. */ diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index fc5934aa9d1d..21c90ab2972d 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -538,6 +538,7 @@ static bool is_ptr_cast_function(enum bpf_func_id func_id) } static bool is_sync_callback_calling_kfunc(u32 btf_id); +static bool insn_has_cb_pad(const struct bpf_insn *insn); static bool is_async_callback_calling_kfunc(u32 btf_id); static bool is_call_rcu_kfunc(u32 btf_id); static bool is_callback_calling_kfunc(u32 btf_id); @@ -3368,6 +3369,15 @@ static int record_callx_edge(struct bpf_verifier_env *env, int caller, int calle return 0; } +/* + * callx, or the call of a helper that has an unwind label for its callback. Both leave the way + * their callee leaves, and only the main pass knows the callee, see record_callx_edge(). + */ +static bool is_indirect_call(const struct bpf_insn *insn) +{ + return bpf_is_callx(insn) || insn_has_cb_pad(insn); +} + /* * The helper or kfunc at this insn cannot pass on a callback that unwinds, so its callback @subprog * must not leave with unwind_exit or cancel_exit. Mark @subprog 'stuck': its may_goto jumps and its @@ -3386,22 +3396,39 @@ static int subprog_cannot_unwind(struct bpf_verifier_env *env, int subprog) return 0; } +static bool callee_did_unwind(struct bpf_verifier_env *env, int insn_idx) +{ + const struct bpf_insn *insn = &env->prog->insnsi[insn_idx]; + struct bpf_subprog_info *si = env->subprog_info; + u32 e; + + if (bpf_pseudo_call(insn)) + return si[bpf_find_subprog(env, insn_idx + insn->imm + 1)].did_unwind; + if (!is_indirect_call(insn)) + return false; + /* the edges that the pass has so far */ + for (e = bpf_find_containing_subprog(env, insn_idx)->callx_edge; e; + e = env->callx_edges[e - 1].next) + if (si[env->callx_edges[e - 1].callee].did_unwind) + return true; + return false; +} + /* * A subprog unwinds. What calls it without unwind_goto is unwound in turn, and so on. The callers * of a global subprog among them have to know, see unwind_frame(): one more round will do for all. */ static void mark_unwind_above(struct bpf_verifier_env *env) { - struct bpf_subprog_info *si = env->subprog_info, *caller; const struct bpf_insn *insn = env->prog->insnsi; + struct bpf_subprog_info *caller; bool changed; int i; do { changed = false; for (i = 0; i < env->prog->len; i++) { - if (!bpf_pseudo_call(&insn[i]) || bpf_is_unwind_goto(&insn[i + 1]) || - !si[bpf_find_subprog(env, i + insn[i].imm + 1)].did_unwind) + if (!callee_did_unwind(env, i) || bpf_is_unwind_goto(&insn[i + 1])) continue; caller = bpf_find_containing_subprog(env, i); changed |= !caller->did_unwind; @@ -6034,8 +6061,8 @@ static int mark_recursive_subprogs(struct bpf_verifier_env *env) struct bpf_insn *insn = env->prog->insnsi; int cnt = env->subprog_cnt, i, j, s, err; struct bpf_call_graph cg; - /* the program calls the subprog, or callx may | a helper may | such a subprog gets to it */ - enum { CALLED = 1, CB = 2, BELOW_CB = 4 }; + /* CALLED: the program can call the subprog, with a call insn or through its address */ + enum { CALLED = 1 }; /* callx calls node 'cnt', which calls each subprog whose address is taken */ err = bpf_call_graph_init(&cg, cnt + 1); @@ -6063,7 +6090,7 @@ static int mark_recursive_subprogs(struct bpf_verifier_env *env) if (bpf_pseudo_call(&insn[i]) || !func_is_kept_for_later(env, i, si[s + 1].start)) { err = bpf_call_graph_add(&cg, s, j); - cg.marks[j] |= bpf_pseudo_call(&insn[i]) ? CALLED : CB; + cg.marks[j] |= CALLED; } if (bpf_pseudo_func(&insn[i])) si[j].addr_taken = true; @@ -6087,12 +6114,6 @@ static int mark_recursive_subprogs(struct bpf_verifier_env *env) bpf_call_graph_sort(&cg, 0); - /* A helper doesn't expect its callback to unwind, so what the callback calls cannot either */ - for (s = 0; s < cnt; s++) - if (cg.marks[s] & CB) - cg.marks[s] |= BELOW_CB; - bpf_call_graph_mark_callees(&cg, BELOW_CB); - /* * A function that the program calls can leave with unwind_exit or cancel_exit, if the JIT * supports them and the loader has CAP_PERFMON. A function that only the kernel calls, like @@ -6102,7 +6123,7 @@ static int mark_recursive_subprogs(struct bpf_verifier_env *env) si[s].bail_unwinds = si[s].can_unwind || (s && env->prog->jit_requested && bpf_jit_supports_unwind() && !bpf_prog_is_offloaded(env->prog->aux) && bpf_may_unwind(env) && - (cg.marks[s] & (CALLED | BELOW_CB)) == CALLED); + (cg.marks[s] & CALLED)); si[s].did_unwind = si[s].can_unwind; } @@ -11530,42 +11551,24 @@ static bool kfunc_has_cb_pad(u32 btf_id) return is_bpf_rbtree_add_kfunc(btf_id); } -static bool insn_has_cb_pad(struct bpf_insn *insn) +static bool insn_has_cb_pad(const struct bpf_insn *insn) { - return (bpf_helper_call(insn) && helper_has_cb_pad(insn->imm)) || - (bpf_pseudo_kfunc_call(insn) && kfunc_has_cb_pad(insn->imm)); + return BPF_HAS_CB_PADS && + ((bpf_helper_call(insn) && helper_has_cb_pad(insn->imm)) || + (bpf_pseudo_kfunc_call(insn) && kfunc_has_cb_pad(insn->imm))); } /* - * A sync callback is called by a helper or a kfunc, a C frame between the - * callback and the BPF code that called it. unwind_exit of the callback returns - * to the pad of its caller when there is one, and a helper or a kfunc that - * calls a callback has one where BPF_HAS_CB_PADS and insn_has_cb_pad(): it - * calls the callback with bpf_call_cb(), drops what it holds when the callback - * unwinds and leaves to the pad of its own caller with the r0 of the callback. - * So a call with such a callback that can unwind is a call of something that - * can unwind, and unwind_goto has to follow it. - * - * A callback has no unwind_exit and unwinds all the same when it is recursive - * and unwind_goto follows every call of it that mark_recursive_subprogs() saw: - * its check of the recursion leaves with unwind_exit then, see - * check_recursion_guard(). The call of a helper that got the callback from a - * table of functions is not one of those calls. It is known here. + * A helper or a kfunc is a C frame between its callback and the BPF code that called it. When the + * callback unwinds, a helper with an unwind label releases what it holds and leaves to the + * unwind_goto or cancel_goto after its own call, with the R0 of the callback. Reject a callback + * that can unwind if the helper has no label. A callback without unwind_exit can unwind too, at + * the stack check of a recursive function, see check_recursion_guard(). */ -static int check_cb_unwind(struct bpf_verifier_env *env, struct bpf_insn *insn, - int insn_idx, int subprog) +static int check_cb_unwind(struct bpf_verifier_env *env, struct bpf_insn *insn, int subprog) { if (!bpf_subprog_unwinds(&env->subprog_info[subprog])) return 0; - if (!bpf_is_unwind_goto(insn + 1)) { - if (bpf_helper_call(insn)) - verbose(env, "no unwind_goto after the call of %s()\n", - func_id_name(insn->imm)); - else - verbose(env, "no unwind_goto after the call of the kfunc at insn %d\n", - insn_idx); - return -EINVAL; - } /* The helpers have unwind labels only with asm goto outputs, see bpf_call_cb(). */ if (!BPF_HAS_CB_PADS) { verbose(env, "callback cannot unwind: the helpers have no pads\n"); @@ -11662,11 +11665,10 @@ static int push_callback_call(struct bpf_verifier_env *env, struct bpf_insn *ins else env->subprog_info[subprog].is_other_cb = true; - err = check_cb_unwind(env, insn, insn_idx, subprog); + err = check_cb_unwind(env, insn, subprog); if (err) return err; - /* the helper has no pad, or nothing takes the program from its call to a pad */ - if (!insn_has_cb_pad(insn) || !bpf_is_unwind_goto(insn + 1)) { + if (!insn_has_cb_pad(insn)) { err = subprog_cannot_unwind(env, subprog); if (err) return err; @@ -12647,10 +12649,10 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) /* * A callback returns into the helper that called it, and to the * pad of the helper when the helper has one. check_cb_unwind() - * has seen to that, and to the unwind_goto after the call of - * the helper, where the helper leaves to right away, with the - * r0 of the callback as it is: the pad gets its state, as the - * pad after a subprog call does. + * has seen to that. From there the helper leaves right away to + * the unwind_goto after its call, with the r0 of the callback + * as it is: the pad gets its state, as the pad after a subprog + * call does. */ err = check_unwound_return(env, callee->callsite, callee->subprogno); if (err) @@ -16166,18 +16168,22 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, if (is_bpf_rbtree_add_kfunc(meta.func_id)) { /* - * The pad of the kfunc drops the node when the comparator - * unwinds, the destructors of its fields included, which is a - * bpf_obj_drop() under the rule above. The comparator unwinds - * from the check of its recursion too, see check_cb_unwind(). + * When the comparator unwinds, the kfunc drops the node at its unwind label. That + * runs the destructors of its fields, so the rule of bpf_obj_drop() above applies. + * Reject a comparator that unwinds, and mark any other so that it is not cancelled. */ - if (bpf_subprog_unwinds(&env->subprog_info[meta.subprogno]) && nmi_prog) { + if (nmi_prog) { struct_meta = btf_find_struct_meta(meta.arg_btf, meta.arg_btf_id); if (struct_meta && btf_record_has_nmi_unsafe_fields(struct_meta->record)) { - verbose(env, - "%s with a comparator that unwinds cannot be used in tracing programs on types with NMI unsafe fields\n", - func_name); - return -EINVAL; + if (bpf_subprog_unwinds(&env->subprog_info[meta.subprogno])) { + verbose(env, + "%s with a comparator that unwinds cannot be used in tracing programs on types with NMI unsafe fields\n", + func_name); + return -EINVAL; + } + err = subprog_cannot_unwind(env, meta.subprogno); + if (err) + return err; } } /* @@ -16185,12 +16191,16 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, * releases an object, so no destructor of a field may run. A list head's would take * the lock in the node, and bpf_spin_locks of a cpu share one word for irq flags. */ - if (bpf_subprog_unwinds(&env->subprog_info[meta.subprogno]) && - bpf_obj_free_runs_dtor(meta.arg_btf, meta.arg_btf_id)) { - verbose(env, - "%s with a comparator that unwinds cannot be used on types that take a lock when freed\n", - func_name); - return -EINVAL; + if (bpf_obj_free_runs_dtor(meta.arg_btf, meta.arg_btf_id)) { + if (bpf_subprog_unwinds(&env->subprog_info[meta.subprogno])) { + verbose(env, + "%s with a comparator that unwinds cannot be used on types with a list head, an rb root or a referenced kptr\n", + func_name); + return -EINVAL; + } + err = subprog_cannot_unwind(env, meta.subprogno); + if (err) + return err; } err = push_callback_call(env, insn, insn_idx, meta.subprogno, set_rbtree_add_callback_state); @@ -21683,7 +21693,7 @@ static bool frame_holds_resources(const struct bpf_verifier_state *st) * * False when it cannot: * - The subprog cannot leave with unwind_exit, see bail_unwinds. - * - A helper that calls a callback doesn't expect it to unwind. + * - A helper that has no pad for its callback doesn't expect it to unwind. * Then the program goes on along a path that it has, like after may_goto of * its own. It releases what it holds there. * @@ -21702,7 +21712,7 @@ static bool frame_can_unwind(struct bpf_verifier_env *env, const struct bpf_veri /* A landing pad has no nospec barrier, so only a loader with CAP_PERFMON gets one. */ if (!bpf_may_unwind(env)) return false; - if (frame->in_callback_fn) + if (frame->in_callback_fn && !sub->bail_unwinds) return false; return frame_is_top(st) || sub->bail_unwinds || or_exit; } diff --git a/tools/testing/selftests/bpf/progs/verifier_unwind.c b/tools/testing/selftests/bpf/progs/verifier_unwind.c index 6be56387a825..b4d7dfdc675b 100644 --- a/tools/testing/selftests/bpf/progs/verifier_unwind.c +++ b/tools/testing/selftests/bpf/progs/verifier_unwind.c @@ -930,13 +930,13 @@ static unsigned long cb_unwinds(void) ); } -/* bpf_loop() has a pad for it, so what's missing is the one after the call */ +/* bpf_loop() has a pad for it, and the verifier adds the one after the call */ SEC("tc") __description("unwind: callback unwinds") __arch_x86_64 __arch_arm64 __load_if_JITed() -__failure __msg("no unwind_goto after the call of bpf_loop()") +__success __retval(2) __naked int callback_unwinds(void) { asm volatile ( -- 2.53.1-Meta