Skip to content

Instantly share code, notes, and snippets.

@eddyz87
Created January 15, 2024 15:25
Show Gist options
  • Save eddyz87/276f1ecc51930017dcddbb56e37f57ad to your computer and use it in GitHub Desktop.
Save eddyz87/276f1ecc51930017dcddbb56e37f57ad to your computer and use it in GitHub Desktop.
selftests-inline-asm-polymorphic-r.patch
diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h
index f44875f8b367..21c22d849216 100644
--- a/tools/testing/selftests/bpf/bpf_experimental.h
+++ b/tools/testing/selftests/bpf/bpf_experimental.h
@@ -222,6 +222,19 @@ extern void bpf_throw(u64 cookie) __ksym;
signed long long: 1 \
)
+#define __bpf_upcast64(x) _Generic((x), \
+ unsigned char: ((u64)x), \
+ unsigned short: ((u64)x), \
+ unsigned int: ((u64)x), \
+ unsigned long: ((u64)x), \
+ unsigned long long: ((u64)x), \
+ signed char: ((s64)x), \
+ signed short: ((s64)x), \
+ signed int: ((s64)x), \
+ signed long: ((s64)x), \
+ signed long long: ((s64)x) \
+)
+
#define __bpf_assert_check(LHS, op, RHS) \
_Static_assert(sizeof(&(LHS)), "1st argument must be an lvalue expression"); \
_Static_assert(sizeof(LHS) == 8, "Only 8-byte integers are supported\n"); \
@@ -232,7 +245,9 @@ extern void bpf_throw(u64 cookie) __ksym;
({ \
(void)bpf_throw; \
asm volatile ("if %[lhs] " op " %[rhs] goto +2; r1 = %[value]; call bpf_throw" \
- : : [lhs] "r"(LHS), [rhs] cons(RHS), [value] "ri"(VAL) : ); \
+ : : [lhs] "r"(LHS), \
+ [rhs] cons(__bpf_upcast64(RHS)), \
+ [value] "ri"(__bpf_upcast64(VAL)) : ); \
})
#define __bpf_assert_op_sign(LHS, op, cons, RHS, VAL, supp_sign) \
@@ -265,7 +280,7 @@ extern void bpf_throw(u64 cookie) __ksym;
__label__ l_true; \
bool ret = DEFAULT; \
asm volatile goto("if %[lhs] " SIGN #OP " %[rhs] goto %l[l_true]" \
- :: [lhs] "r"((short)LHS), [rhs] PRED (RHS) :: l_true); \
+ :: [lhs] "r"(LHS), [rhs] PRED (RHS) :: l_true); \
ret = !DEFAULT; \
l_true: \
ret; \
@@ -285,14 +300,14 @@ l_true: \
(void)(__lhs OP __rhs); \
if (__cmp_cannot_be_signed(OP) || !__is_signed_type(typeof(__lhs))) { \
if (sizeof(__rhs) == 8) \
- ret = __bpf_cmp(__lhs, OP, "", "r", __rhs, NOFLIP); \
+ ret = __bpf_cmp((s64)__lhs, OP, "", "r", (s64)__rhs, NOFLIP); \
else \
- ret = __bpf_cmp(__lhs, OP, "", "i", __rhs, NOFLIP); \
+ ret = __bpf_cmp((s64)__lhs, OP, "", "i", (s64)__rhs, NOFLIP); \
} else { \
if (sizeof(__rhs) == 8) \
- ret = __bpf_cmp(__lhs, OP, "s", "r", __rhs, NOFLIP); \
+ ret = __bpf_cmp((u64)__lhs, OP, "s", "r", (u64)__rhs, NOFLIP); \
else \
- ret = __bpf_cmp(__lhs, OP, "s", "i", __rhs, NOFLIP); \
+ ret = __bpf_cmp((u64)__lhs, OP, "s", "i", (u64)__rhs, NOFLIP); \
} \
ret; \
})
@@ -325,7 +340,7 @@ l_true: \
#ifndef bpf_nop_mov
#define bpf_nop_mov(var) \
- asm volatile("%[reg]=%[reg]"::[reg]"r"((short)var))
+ asm volatile("%[reg]=%[reg]"::[reg]"r"((unsigned long)var))
#endif
/* Description
diff --git a/tools/testing/selftests/bpf/progs/iters.c b/tools/testing/selftests/bpf/progs/iters.c
index fe971992e635..62092ac6b34c 100644
--- a/tools/testing/selftests/bpf/progs/iters.c
+++ b/tools/testing/selftests/bpf/progs/iters.c
@@ -79,7 +79,7 @@ int iter_err_unsafe_asm_loop(const void *ctx)
:
: [it]"r"(&it),
[small_arr]"p"(small_arr),
- [zero]"p"(zero),
+ [zero]"r"((__u64)(__u32)zero),
__imm(bpf_iter_num_new),
__imm(bpf_iter_num_next),
__imm(bpf_iter_num_destroy)
diff --git a/tools/testing/selftests/bpf/progs/pyperf.h b/tools/testing/selftests/bpf/progs/pyperf.h
index 026d573ce179..2ce1221fe9c6 100644
--- a/tools/testing/selftests/bpf/progs/pyperf.h
+++ b/tools/testing/selftests/bpf/progs/pyperf.h
@@ -172,7 +172,7 @@ struct process_frame_ctx {
bool done;
};
-static int process_frame_callback(__u32 i, struct process_frame_ctx *ctx)
+static int process_frame_callback(__u64 i, struct process_frame_ctx *ctx)
{
int zero = 0;
void *frame_ptr = ctx->frame_ptr;
diff --git a/tools/testing/selftests/bpf/progs/test_cls_redirect.c b/tools/testing/selftests/bpf/progs/test_cls_redirect.c
index 66b304982245..d280e7e533ae 100644
--- a/tools/testing/selftests/bpf/progs/test_cls_redirect.c
+++ b/tools/testing/selftests/bpf/progs/test_cls_redirect.c
@@ -148,7 +148,7 @@ static __always_inline size_t buf_off(const buf_t *buf)
* Use inline asm to break this optimization.
*/
size_t off = (size_t)buf->head;
- asm("%0 -= %1" : "+r"(off) : "r"(buf->skb->data));
+ asm("%0 -= %1" : "+r"(off) : "r"((__u64)buf->skb->data));
return off;
}
diff --git a/tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c b/tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c
index cf7ed8cbb1fe..0de3d315cd39 100644
--- a/tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c
+++ b/tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c
@@ -49,7 +49,7 @@ int bpf_testcb(struct bpf_sock_ops *skops)
int save_syn = 1;
int rv = -1;
int v = 0;
- int op;
+ long op;
/* Test reading fields in bpf_sock_ops using single register */
asm volatile (
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment