bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Martin KaFai Lau <martin.lau@kernel.org>,
	David Vernet <void@manifault.com>
Subject: [PATCH RFC bpf-next v1 6/9] bpf: Add KF_THROW annotation for kfuncs
Date: Wed,  5 Apr 2023 02:42:36 +0200	[thread overview]
Message-ID: <20230405004239.1375399-7-memxor@gmail.com> (raw)
In-Reply-To: <20230405004239.1375399-1-memxor@gmail.com>

Add KF_THROW annotation to kfuncs to indicate that they may throw. This
is mostly for testing for now, but in the future it could be used by
kfuncs to throw on invalid arguments or invalid conditions based on
their input arguments, causing the program to abort, and simplify the
overall user experience of kfuncs for the happy case, without having to
deal with corner cases that never occur at runtime.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 include/linux/btf.h   |  1 +
 kernel/bpf/verifier.c | 12 ++++++++++--
 net/bpf/test_run.c    | 12 ++++++++++++
 3 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/include/linux/btf.h b/include/linux/btf.h
index d53b10cc55f2..8dfa4113822b 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -75,6 +75,7 @@
 #define KF_ITER_NEW     (1 << 8) /* kfunc implements BPF iter constructor */
 #define KF_ITER_NEXT    (1 << 9) /* kfunc implements BPF iter next method */
 #define KF_ITER_DESTROY (1 << 10) /* kfunc implements BPF iter destructor */
+#define KF_THROW	(1 << 11) /* kfunc may throw a BPF exception */
 
 /*
  * Tag marking a kernel function as a kfunc. This is meant to minimize the
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index acfcaadca3b6..b9f4b1849647 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -9454,6 +9454,11 @@ static bool is_kfunc_arg_kptr_get(struct bpf_kfunc_call_arg_meta *meta, int arg)
 	return arg == 0 && (meta->kfunc_flags & KF_KPTR_GET);
 }
 
+static bool is_kfunc_throwing(struct bpf_kfunc_call_arg_meta *meta)
+{
+	return meta->kfunc_flags & KF_THROW;
+}
+
 static bool __kfunc_param_match_suffix(const struct btf *btf,
 				       const struct btf_param *arg,
 				       const char *suffix)
@@ -10813,11 +10818,14 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 		}
 	}
 
-	if (meta.btf == btf_vmlinux && meta.func_id == special_kfunc_list[KF_bpf_throw]) {
+	if (is_kfunc_throwing(&meta) ||
+	    (meta.btf == btf_vmlinux && meta.func_id == special_kfunc_list[KF_bpf_throw])) {
 		err = mark_chain_throw(env, insn_idx);
 		if (err < 0)
 			return err;
-		return 1;
+		/* Halt exploration only for bpf_throw */
+		if (!is_kfunc_throwing(&meta))
+			return 1;
 	}
 
 	for (i = 0; i < CALLER_SAVED_REGS; i++)
diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
index f1652f5fbd2e..31f76ee4218b 100644
--- a/net/bpf/test_run.c
+++ b/net/bpf/test_run.c
@@ -766,6 +766,16 @@ __bpf_kfunc static u32 bpf_kfunc_call_test_static_unused_arg(u32 arg, u32 unused
 	return arg;
 }
 
+__bpf_kfunc notrace void bpf_kfunc_call_test_always_throws(void)
+{
+	bpf_throw();
+}
+
+__bpf_kfunc notrace void bpf_kfunc_call_test_never_throws(void)
+{
+	return;
+}
+
 __diag_pop();
 
 BTF_SET8_START(bpf_test_modify_return_ids)
@@ -806,6 +816,8 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test_ref, KF_TRUSTED_ARGS | KF_RCU)
 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_always_throws, KF_THROW)
+BTF_ID_FLAGS(func, bpf_kfunc_call_test_never_throws, KF_THROW)
 BTF_SET8_END(test_sk_check_kfunc_ids)
 
 static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size,
-- 
2.40.0


  parent reply	other threads:[~2023-04-05  0:43 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-05  0:42 [PATCH RFC bpf-next v1 0/9] Exceptions - 1/2 Kumar Kartikeya Dwivedi
2023-04-05  0:42 ` [PATCH RFC bpf-next v1 1/9] bpf: Fix kfunc callback handling Kumar Kartikeya Dwivedi
2023-04-05  0:42 ` [PATCH RFC bpf-next v1 2/9] bpf: Refactor and generalize optimize_bpf_loop Kumar Kartikeya Dwivedi
2023-04-05  0:42 ` [PATCH RFC bpf-next v1 3/9] bpf: Implement bpf_throw kfunc Kumar Kartikeya Dwivedi
2023-04-06  2:16   ` Alexei Starovoitov
2023-04-06 23:54     ` Kumar Kartikeya Dwivedi
2023-04-07  2:11       ` Alexei Starovoitov
2023-04-07  2:46         ` Kumar Kartikeya Dwivedi
2023-04-12 19:36           ` Alexei Starovoitov
2023-04-13 17:05             ` Kumar Kartikeya Dwivedi
2023-04-05  0:42 ` [PATCH RFC bpf-next v1 4/9] bpf: Handle throwing BPF callbacks in helpers and kfuncs Kumar Kartikeya Dwivedi
2023-04-06  2:21   ` Alexei Starovoitov
2023-04-07  0:07     ` Kumar Kartikeya Dwivedi
2023-04-07  2:15       ` Alexei Starovoitov
2023-04-07  2:57         ` Kumar Kartikeya Dwivedi
2023-04-12 19:43           ` Alexei Starovoitov
2023-04-13 17:13             ` Kumar Kartikeya Dwivedi
2023-04-13 23:41               ` Alexei Starovoitov
2023-04-16  4:45                 ` Kumar Kartikeya Dwivedi
2023-04-17 23:20                   ` Alexei Starovoitov
2023-04-05  0:42 ` [PATCH RFC bpf-next v1 5/9] bpf: Add pass to fixup global function throw information Kumar Kartikeya Dwivedi
2023-04-05  0:42 ` Kumar Kartikeya Dwivedi [this message]
2023-04-05  0:42 ` [PATCH RFC bpf-next v1 7/9] bpf: Introduce bpf_set_exception_callback kfunc Kumar Kartikeya Dwivedi
2023-04-05  0:42 ` [PATCH RFC bpf-next v1 8/9] bpf: Introduce BPF assertion macros Kumar Kartikeya Dwivedi
2023-04-05  0:42 ` [PATCH RFC bpf-next v1 9/9] selftests/bpf: Add tests for BPF exceptions Kumar Kartikeya Dwivedi
2023-04-06  2:38   ` Alexei Starovoitov
2023-04-07  0:42     ` Kumar Kartikeya Dwivedi
2023-04-07  2:30       ` Alexei Starovoitov
2023-04-07  3:12         ` Kumar Kartikeya Dwivedi
2023-04-12 19:46           ` Alexei Starovoitov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230405004239.1375399-7-memxor@gmail.com \
    --to=memxor@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=martin.lau@kernel.org \
    --cc=void@manifault.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).