[PATCH bpf-next v6 3/3] arm64/cfi,bpf: Use DEFINE_CFI_TYPE in arm64

Maxwell Bland posted 3 patches 1 year, 8 months ago
There is a newer version of this series
[PATCH bpf-next v6 3/3] arm64/cfi,bpf: Use DEFINE_CFI_TYPE in arm64
Posted by Maxwell Bland 1 year, 8 months ago
Corrects Puranjay Mohan's commit to adopt Mark Rutland's
suggestion of using a C CFI type macro in kCFI+BPF.

Signed-off-by: Maxwell Bland <mbland@motorola.com>
---
 arch/arm64/kernel/alternative.c | 46 ++++-----------------------------
 1 file changed, 5 insertions(+), 41 deletions(-)

diff --git a/arch/arm64/kernel/alternative.c b/arch/arm64/kernel/alternative.c
index 1715da7df137..d7a58eca7665 100644
--- a/arch/arm64/kernel/alternative.c
+++ b/arch/arm64/kernel/alternative.c
@@ -8,6 +8,7 @@
 
 #define pr_fmt(fmt) "alternatives: " fmt
 
+#include <linux/cfi_types.h>
 #include <linux/init.h>
 #include <linux/cpu.h>
 #include <linux/elf.h>
@@ -302,53 +303,16 @@ EXPORT_SYMBOL(alt_cb_patch_nops);
 
 #ifdef CONFIG_CFI_CLANG
 struct bpf_insn;
-
 /* Must match bpf_func_t / DEFINE_BPF_PROG_RUN() */
 extern unsigned int __bpf_prog_runX(const void *ctx,
 				    const struct bpf_insn *insn);
-
-/*
- * Force a reference to the external symbol so the compiler generates
- * __kcfi_typid.
- */
-__ADDRESSABLE(__bpf_prog_runX);
-
-/* u32 __ro_after_init cfi_bpf_hash = __kcfi_typeid___bpf_prog_runX; */
-asm (
-"	.pushsection	.data..ro_after_init,\"aw\",@progbits	\n"
-"	.type	cfi_bpf_hash,@object				\n"
-"	.globl	cfi_bpf_hash					\n"
-"	.p2align	2, 0x0					\n"
-"cfi_bpf_hash:							\n"
-"	.word	__kcfi_typeid___bpf_prog_runX			\n"
-"	.size	cfi_bpf_hash, 4					\n"
-"	.popsection						\n"
-);
-
+DEFINE_CFI_TYPE(cfi_bpf_hash, __bpf_prog_runX);
 /* Must match bpf_callback_t */
 extern u64 __bpf_callback_fn(u64, u64, u64, u64, u64);
-
-__ADDRESSABLE(__bpf_callback_fn);
-
-/* u32 __ro_after_init cfi_bpf_subprog_hash = __kcfi_typeid___bpf_callback_fn; */
-asm (
-"	.pushsection	.data..ro_after_init,\"aw\",@progbits	\n"
-"	.type	cfi_bpf_subprog_hash,@object			\n"
-"	.globl	cfi_bpf_subprog_hash				\n"
-"	.p2align	2, 0x0					\n"
-"cfi_bpf_subprog_hash:						\n"
-"	.word	__kcfi_typeid___bpf_callback_fn			\n"
-"	.size	cfi_bpf_subprog_hash, 4				\n"
-"	.popsection						\n"
-);
-
+DEFINE_CFI_TYPE(cfi_bpf_subprog_hash, __bpf_callback_fn);
 u32 cfi_get_func_hash(void *func)
 {
-	u32 hash;
-
-	if (get_kernel_nofault(hash, func - cfi_get_offset()))
-		return 0;
-
-	return hash;
+	u32 *hashp = func - cfi_get_offset();
+	return READ_ONCE(*hashp);
 }
 #endif
-- 
2.43.0
Re: [PATCH bpf-next v6 3/3] arm64/cfi,bpf: Use DEFINE_CFI_TYPE in arm64
Posted by Alexei Starovoitov 1 year, 8 months ago
On Wed, Jun 12, 2024 at 8:32 AM Maxwell Bland <mbland@motorola.com> wrote:
>
> Corrects Puranjay Mohan's commit to adopt Mark Rutland's
> suggestion of using a C CFI type macro in kCFI+BPF.
>
> Signed-off-by: Maxwell Bland <mbland@motorola.com>
> ---
>  arch/arm64/kernel/alternative.c | 46 ++++-----------------------------
>  1 file changed, 5 insertions(+), 41 deletions(-)
>
> diff --git a/arch/arm64/kernel/alternative.c b/arch/arm64/kernel/alternative.c
> index 1715da7df137..d7a58eca7665 100644
> --- a/arch/arm64/kernel/alternative.c
> +++ b/arch/arm64/kernel/alternative.c
> @@ -8,6 +8,7 @@
>
>  #define pr_fmt(fmt) "alternatives: " fmt
>
> +#include <linux/cfi_types.h>
>  #include <linux/init.h>
>  #include <linux/cpu.h>
>  #include <linux/elf.h>
> @@ -302,53 +303,16 @@ EXPORT_SYMBOL(alt_cb_patch_nops);
>
>  #ifdef CONFIG_CFI_CLANG
>  struct bpf_insn;
> -
>  /* Must match bpf_func_t / DEFINE_BPF_PROG_RUN() */
>  extern unsigned int __bpf_prog_runX(const void *ctx,
>                                     const struct bpf_insn *insn);
> -
> -/*
> - * Force a reference to the external symbol so the compiler generates
> - * __kcfi_typid.
> - */
> -__ADDRESSABLE(__bpf_prog_runX);
> -
> -/* u32 __ro_after_init cfi_bpf_hash = __kcfi_typeid___bpf_prog_runX; */
> -asm (
> -"      .pushsection    .data..ro_after_init,\"aw\",@progbits   \n"
> -"      .type   cfi_bpf_hash,@object                            \n"
> -"      .globl  cfi_bpf_hash                                    \n"
> -"      .p2align        2, 0x0                                  \n"
> -"cfi_bpf_hash:                                                 \n"
> -"      .word   __kcfi_typeid___bpf_prog_runX                   \n"
> -"      .size   cfi_bpf_hash, 4                                 \n"
> -"      .popsection                                             \n"
> -);
> -
> +DEFINE_CFI_TYPE(cfi_bpf_hash, __bpf_prog_runX);
>  /* Must match bpf_callback_t */
>  extern u64 __bpf_callback_fn(u64, u64, u64, u64, u64);
> -
> -__ADDRESSABLE(__bpf_callback_fn);
> -
> -/* u32 __ro_after_init cfi_bpf_subprog_hash = __kcfi_typeid___bpf_callback_fn; */
> -asm (
> -"      .pushsection    .data..ro_after_init,\"aw\",@progbits   \n"
> -"      .type   cfi_bpf_subprog_hash,@object                    \n"
> -"      .globl  cfi_bpf_subprog_hash                            \n"
> -"      .p2align        2, 0x0                                  \n"
> -"cfi_bpf_subprog_hash:                                         \n"
> -"      .word   __kcfi_typeid___bpf_callback_fn                 \n"
> -"      .size   cfi_bpf_subprog_hash, 4                         \n"
> -"      .popsection                                             \n"
> -);
> -
> +DEFINE_CFI_TYPE(cfi_bpf_subprog_hash, __bpf_callback_fn);
>  u32 cfi_get_func_hash(void *func)
>  {
> -       u32 hash;
> -
> -       if (get_kernel_nofault(hash, func - cfi_get_offset()))
> -               return 0;
> -
> -       return hash;
> +       u32 *hashp = func - cfi_get_offset();
> +       return READ_ONCE(*hashp);

Please avoid the code churn.
Just squash it into the previous patch.

pw-bot: cr
Re: [PATCH bpf-next v6 3/3] arm64/cfi,bpf: Use DEFINE_CFI_TYPE in arm64
Posted by Maxwell Bland 1 year, 7 months ago
On Wed, Jun 12, 2024 at 10:07:09AM GMT, Alexei Starovoitov wrote:
> On Wed, Jun 12, 2024 at 8:32 AM Maxwell Bland <mbland@motorola.com> wrote:
> 
> Please avoid the code churn.
> Just squash it into the previous patch.

Sweet, thank you and done:
https://lore.kernel.org/all/ptrugmna4xb5o5lo4xislf4rlz7avdmd4pfho5fjwtjj7v422u@iqrwfrbwuxrq/

Waited 'til Monday to lower the frequency.