Skip to content

Instantly share code, notes, and snippets.

@kkdwivedi
Last active June 21, 2022 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkdwivedi/7839cc9e4f002acc3e15350b1b86c88c to your computer and use it in GitHub Desktop.
Save kkdwivedi/7839cc9e4f002acc3e15350b1b86c88c to your computer and use it in GitHub Desktop.
commit 32ace24aeff9c815b2792cbbd9c69512d17882d6
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Date: Fri May 20 15:14:09 2022 +0530
stage
diff --git a/include/linux/btf.h b/include/linux/btf.h
index 2611cea2c2b6..c4b355bdf552 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -12,6 +12,73 @@
#define BTF_TYPE_EMIT(type) ((void)(type *)0)
#define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val)
+#define BTF_ARG_NONE 0
+#define BTF_ARG_SIZE 1
+#define BTF_ARG_PTR_TO_MEM 2
+#define BTF_ARG_PTR_TO_CTX 3
+#define BTF_ARG_PTR_TO_BTF_ID 4
+#define BTF_ARG_RET_PTR_SIZE 5
+
+#define BTF_ARG_RET_PTR_NULL 29
+#define BTF_ARG_PTR_REF 30
+#define BTF_ARG_PTR_NULL 31
+#define BTF_ARG_MAX BTF_ARG_PTR_NULL + 1
+
+#define __KARG_N(type, name, t1, t2, t3, t4, t5) type, name, t1, t2, t3, t4, t5
+#define __KARG_N_EXP(type, name, t1, t2, t3, t4, t5, ...) \
+ __KARG_N(type, name, BTF_ARG_##t1, BTF_ARG_##t2, BTF_ARG_##t3, BTF_ARG_##t4, BTF_ARG_##t5)
+#define KARG(type, name, ...) __KARG_N_EXP(type, name, ##__VA_ARGS__, BTF_ARG_NONE, \
+ BTF_ARG_NONE, BTF_ARG_NONE, BTF_ARG_NONE, BTF_ARG_NONE)
+
+#define __KARG_NAME_TAG(arg, t1, t2, t3, t4, t5) arg##__##t1##_##t2##_##t3##_##t4##_##t5
+#define __KARG_NAME_NOTAG(arg, t1, t2, t3, t4, t5) arg
+#define __KARG_DECL(select, type, arg, t1, t2, t3, t4, t5) \
+ type __KARG_NAME_##select(arg, t1, t2, t3, t4, t5)
+
+#define __KARG_DECL_0(...)
+#define __KARG_DECL_1(select, type, arg, t1, t2, t3, t4, t5, ...) \
+ __KARG_DECL(select, type, arg, t1, t2, t3, t4, t5)
+#define __KARG_DECL_2(select, type, arg, t1, t2, t3, t4, t5, ...) \
+ __KARG_DECL(select, type, arg, t1, t2, t3, t4, t5), __KARG_DECL_1(select, ##__VA_ARGS__)
+#define __KARG_DECL_3(select, type, arg, t1, t2, t3, t4, t5, ...) \
+ __KARG_DECL(select, type, arg, t1, t2, t3, t4, t5), __KARG_DECL_2(select, ##__VA_ARGS__)
+#define __KARG_DECL_4(select, type, arg, t1, t2, t3, t4, t5, ...) \
+ __KARG_DECL(select, type, arg, t1, t2, t3, t4, t5), __KARG_DECL_3(select, ##__VA_ARGS__)
+#define __KARG_DECL_5(select, type, arg, t1, t2, t3, t4, t5, ...) \
+ __KARG_DECL(select, type, arg, t1, t2, t3, t4, t5), __KARG_DECL_4(select, ##__VA_ARGS__)
+
+#define __KARG_DECL_N_TAG(n, ...) __KARG_DECL_##n(TAG, ##__VA_ARGS__)
+#define __KARG_DECL_N_NOTAG(n, ...) __KARG_DECL_##n(NOTAG, ##__VA_ARGS__)
+
+#define __KARG_PARAMS_0(...)
+#define __KARG_PARAMS_1(type, arg, t1, t2, t3, t4, t5, ...) \
+ __KARG_NAME_TAG(arg, t1, t2, t3, t4, t5)
+#define __KARG_PARAMS_2(type, arg, t1, t2, t3, t4, t5, ...) \
+ __KARG_NAME_TAG(arg, t1, t2, t3, t4, t5), __KARG_PARAMS_1(__VA_ARGS__)
+#define __KARG_PARAMS_3(type, arg, t1, t2, t3, t4, t5, ...) \
+ __KARG_NAME_TAG(arg, t1, t2, t3, t4, t5), __KARG_PARAMS_2(__VA_ARGS__)
+#define __KARG_PARAMS_4(type, arg, t1, t2, t3, t4, t5, ...) \
+ __KARG_NAME_TAG(arg, t1, t2, t3, t4, t5), __KARG_PARAMS_3(__VA_ARGS__)
+#define __KARG_PARAMS_5(type, arg, t1, t2, t3, t4, t5, ...) \
+ __KARG_NAME_TAG(arg, t1, t2, t3, t4, t5), __KARG_PARAMS_4(__VA_ARGS__)
+
+#define __KARG_PARAMS(n, ...) __KARG_PARAMS_##n(__VA_ARGS__)
+
+#define __KFUNC_CALL(n, ret_type, kfunc_name, ...) \
+static __always_inline ret_type ____##kfunc_name(__KARG_DECL_N_NOTAG(n, ##__VA_ARGS__));\
+ret_type kfunc_name(__KARG_DECL_N_TAG(n, ##__VA_ARGS__)) \
+{ \
+ return ____##kfunc_name(__KARG_PARAMS(n, ##__VA_ARGS__)); \
+} \
+static __always_inline ret_type ____##kfunc_name(__KARG_DECL_N_NOTAG(n, __VA_ARGS__))
+
+#define KFUNC_CALL_0(...) __KFUNC_CALL(0, __VA_ARGS__)
+#define KFUNC_CALL_1(...) __KFUNC_CALL(1, __VA_ARGS__)
+#define KFUNC_CALL_2(...) __KFUNC_CALL(2, __VA_ARGS__)
+#define KFUNC_CALL_3(...) __KFUNC_CALL(3, __VA_ARGS__)
+#define KFUNC_CALL_4(...) __KFUNC_CALL(4, __VA_ARGS__)
+#define KFUNC_CALL_5(...) __KFUNC_CALL(5, __VA_ARGS__)
+
enum btf_kfunc_type {
BTF_KFUNC_TYPE_CHECK,
BTF_KFUNC_TYPE_ACQUIRE,
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 6dbcaab231e8..351782714f38 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -287,6 +287,20 @@ struct btf_verifier_env {
enum resolve_mode resolve_mode;
};
+static const int btf_arg_flag[BTF_ARG_MAX] = {
+ [BTF_ARG_NONE] = 0,
+ /* Types */
+ [BTF_ARG_SIZE] = 1 << 0,
+ [BTF_ARG_PTR_TO_MEM] = 1 << 1,
+ [BTF_ARG_PTR_TO_CTX] = 1 << 2,
+ [BTF_ARG_PTR_TO_BTF_ID] = 1 << 3,
+ [BTF_ARG_RET_PTR_SIZE] = 1 << 4,
+ /* Modifiers */
+ [BTF_ARG_RET_PTR_NULL] = 1 << 29,
+ [BTF_ARG_PTR_REF] = 1 << 30,
+ [BTF_ARG_PTR_NULL] = 1 << 31,
+};
+
static const char * const btf_kind_str[NR_BTF_KINDS] = {
[BTF_KIND_UNKN] = "UNKNOWN",
[BTF_KIND_INT] = "INT",
diff --git a/net/netfilter/nf_conntrack_bpf.c b/net/netfilter/nf_conntrack_bpf.c
index bc4d5cd63a94..dbd1dee16024 100644
--- a/net/netfilter/nf_conntrack_bpf.c
+++ b/net/netfilter/nf_conntrack_bpf.c
@@ -130,9 +130,12 @@ __diag_ignore_all("-Wmissing-prototypes",
* @opts__sz - Length of the bpf_ct_opts structure
* Must be NF_BPF_CT_OPTS_SZ (12)
*/
-struct nf_conn *
-bpf_xdp_ct_lookup(struct xdp_md *xdp_ctx, struct bpf_sock_tuple *bpf_tuple,
- u32 tuple__sz, struct bpf_ct_opts *opts, u32 opts__sz)
+KFUNC_CALL_5(struct nf_conn *, bpf_xdp_ct_lookup,
+ KARG(struct xdp_md *, xdp_ctx, PTR_TO_CTX),
+ KARG(struct bpf_sock_tuple *, bpf_tuple, PTR_TO_MEM),
+ KARG(u32, tuple__sz, SIZE),
+ KARG(struct bpf_ct_opts *, opts, PTR_TO_MEM),
+ KARG(u32, opts__sz, SIZE))
{
struct xdp_buff *ctx = (struct xdp_buff *)xdp_ctx;
struct net *caller_net;
@@ -173,9 +176,12 @@ bpf_xdp_ct_lookup(struct xdp_md *xdp_ctx, struct bpf_sock_tuple *bpf_tuple,
* @opts__sz - Length of the bpf_ct_opts structure
* Must be NF_BPF_CT_OPTS_SZ (12)
*/
-struct nf_conn *
-bpf_skb_ct_lookup(struct __sk_buff *skb_ctx, struct bpf_sock_tuple *bpf_tuple,
- u32 tuple__sz, struct bpf_ct_opts *opts, u32 opts__sz)
+KFUNC_CALL_5(struct nf_conn *, bpf_skb_ct_lookup,
+ KARG(struct __sk_buff *, skb_ctx, PTR_TO_CTX),
+ KARG(struct bpf_sock_tuple *, bpf_tuple, PTR_TO_MEM),
+ KARG(u32, tuple__sz, SIZE),
+ KARG(struct bpf_ct_opts *, opts, PTR_TO_MEM),
+ KARG(u32, opts__sz, SIZE))
{
struct sk_buff *skb = (struct sk_buff *)skb_ctx;
struct net *caller_net;
@@ -210,7 +216,8 @@ bpf_skb_ct_lookup(struct __sk_buff *skb_ctx, struct bpf_sock_tuple *bpf_tuple,
* @nf_conn - Pointer to referenced nf_conn object, obtained using
* bpf_xdp_ct_lookup or bpf_skb_ct_lookup.
*/
-void bpf_ct_release(struct nf_conn *nfct)
+KFUNC_CALL_1(void, bpf_ct_release,
+ KARG(struct nf_conn *, nfct, PTR_TO_BTF_ID, PTR_NULL))
{
if (!nfct)
return;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment