diff mbox series

[RFC,bpf-next,v4,4/7] btf: Add a new kfunc set which allows to mark a function to be sleepable

Message ID 20220421140740.459558-5-benjamin.tissoires@redhat.com
State New
Headers show
Series Introduce eBPF support for HID devices (new attempt) | expand

Commit Message

Benjamin Tissoires April 21, 2022, 2:07 p.m. UTC
This allows to declare a kfunc as sleepable and prevents its use in
a non sleepable program.

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>

---

new in v4
---
 include/linux/btf.h |  2 ++
 kernel/bpf/btf.c    | 16 +++++++++++++---
 2 files changed, 15 insertions(+), 3 deletions(-)

Comments

Alexei Starovoitov April 26, 2022, 4:12 a.m. UTC | #1
On Thu, Apr 21, 2022 at 04:07:37PM +0200, Benjamin Tissoires wrote:
> This allows to declare a kfunc as sleepable and prevents its use in
> a non sleepable program.
> 
> Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>

makes sense.
> @@ -17,6 +17,7 @@ enum btf_kfunc_type {
>  	BTF_KFUNC_TYPE_ACQUIRE,
>  	BTF_KFUNC_TYPE_RELEASE,
>  	BTF_KFUNC_TYPE_RET_NULL,
> +	BTF_KFUNC_TYPE_SLEEPABLE,
>  	BTF_KFUNC_TYPE_MAX,
>  };
>  
> @@ -35,6 +36,7 @@ struct btf_kfunc_id_set {
>  			struct btf_id_set *acquire_set;
>  			struct btf_id_set *release_set;
>  			struct btf_id_set *ret_null_set;
> +			struct btf_id_set *sleepable_set;
diff mbox series

Patch

diff --git a/include/linux/btf.h b/include/linux/btf.h
index 76a3ff48ae2a..b8b83dcf86bf 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -17,6 +17,7 @@  enum btf_kfunc_type {
 	BTF_KFUNC_TYPE_ACQUIRE,
 	BTF_KFUNC_TYPE_RELEASE,
 	BTF_KFUNC_TYPE_RET_NULL,
+	BTF_KFUNC_TYPE_SLEEPABLE,
 	BTF_KFUNC_TYPE_MAX,
 };
 
@@ -35,6 +36,7 @@  struct btf_kfunc_id_set {
 			struct btf_id_set *acquire_set;
 			struct btf_id_set *release_set;
 			struct btf_id_set *ret_null_set;
+			struct btf_id_set *sleepable_set;
 		};
 		struct btf_id_set *sets[BTF_KFUNC_TYPE_MAX];
 	};
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 22e6e3cdc7ee..bf6a82461c58 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -5889,7 +5889,7 @@  static int btf_check_func_arg_match(struct bpf_verifier_env *env,
 	const struct btf_type *t, *ref_t;
 	const struct btf_param *args;
 	int ref_regno = 0, ret;
-	bool rel = false;
+	bool rel = false, sleepable = false;
 
 	t = btf_type_by_id(btf, func_id);
 	if (!t || !btf_type_is_func(t)) {
@@ -5915,10 +5915,13 @@  static int btf_check_func_arg_match(struct bpf_verifier_env *env,
 		return -EINVAL;
 	}
 
-	/* Only kfunc can be release func */
-	if (is_kfunc)
+	/* Only kfunc can be release or sleepable func */
+	if (is_kfunc) {
 		rel = btf_kfunc_id_set_contains(btf, resolve_prog_type(env->prog),
 						BTF_KFUNC_TYPE_RELEASE, func_id);
+		sleepable = btf_kfunc_id_set_contains(btf, resolve_prog_type(env->prog),
+						      BTF_KFUNC_TYPE_SLEEPABLE, func_id);
+	}
 	/* check that BTF function arguments match actual types that the
 	 * verifier sees.
 	 */
@@ -6092,6 +6095,13 @@  static int btf_check_func_arg_match(struct bpf_verifier_env *env,
 			func_name);
 		return -EINVAL;
 	}
+
+	if (sleepable && !env->prog->aux->sleepable) {
+		bpf_log(log, "kernel function %s is sleepable but the program is not\n",
+			func_name);
+		return -EINVAL;
+	}
+
 	/* returns argument register number > 0 in case of reference release kfunc */
 	return rel ? ref_regno : 0;
 }