Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linux/bpf: regenerate help function with alignment to make compiler happy #21924

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tw4452852
Copy link
Contributor

No description provided.

@rohlem
Copy link
Contributor

rohlem commented Nov 6, 2024

Does it still make sense then that the default alignment for function pointers on BPF targets is > 1?
Do user-provided functions have a higher alignment restriction than these special helper functions?

@tw4452852
Copy link
Contributor Author

Does it still make sense then that the default alignment for function pointers on BPF targets is > 1?

I don't think so.

Do user-provided functions have a higher alignment restriction than these special helper functions?

Yes, I think for these ones that could be align(sizeof(usize))

@alexrp
Copy link
Member

alexrp commented Nov 7, 2024

Does it still make sense then that the default alignment for function pointers on BPF targets is > 1?

We match GCC and LLVM here. If we're going to change this, especially when we use LLVM as our code generator for this target, I would like to see some evidence that changing it won't cause subtle linker bugs.

@tw4452852
Copy link
Contributor Author

tw4452852 commented Nov 7, 2024

I did an experiment with clang as follows:

> cat t.c

static long (* const bpf_trace_printk)(const char *fmt, int fmt_size, ...) = (void *) 6;

int foo(void) {
        bpf_trace_printk("bar", 3);
        return 0;
}

> clang -S --target=bpfel-freestanding -emit-llvm t.c
> cat t.ll

; ModuleID = 't.c'
source_filename = "t.c"
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "bpfel-freestanding"

@.str = private unnamed_addr constant [4 x i8] c"bar\00", align 1

; Function Attrs: noinline nounwind optnone
define dso_local i32 @foo() #0 {
  %1 = call i64 (ptr, i32, ...) inttoptr (i64 6 to ptr)(ptr noundef @.str, i32 noundef 3)
  ret i32 0
}

attributes #0 = { noinline nounwind optnone "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }

!llvm.module.flags = !{!0, !1}
!llvm.ident = !{!2}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"frame-pointer", i32 2}
!2 = !{!"clang version 17.0.6"}

@alexrp
Copy link
Member

alexrp commented Nov 7, 2024

Can you clarify what you're trying to show?

@tw4452852
Copy link
Contributor Author

tw4452852 commented Nov 7, 2024

%1 = call i64 (ptr, i32, ...) inttoptr (i64 6 to ptr)(ptr noundef @.str, i32 noundef 3)

LLVM use inttoptr to cast, I think it doesn't assume any alignment requirement. Feel free to correct me.

@alexrp
Copy link
Member

alexrp commented Nov 7, 2024

Well, sure; C and LLVM IR don't represent pointer alignment in the type system like Zig does. So that in itself isn't surprising.

But changing the default function alignment often has broader ABI ramifications. In some cases, the linker may depend on a certain minimum function alignment in order to produce correct binaries. (I don't know if that's the case for BPF.) Here's the relevant code in GCC and LLVM:

@tw4452852 tw4452852 changed the title linux/bpf: add alignment for helper functions to make compiler happy linux/bpf: regenerate help function definitions with translate-c Nov 8, 2024
@tw4452852 tw4452852 changed the title linux/bpf: regenerate help function definitions with translate-c linux/bpf: regenerate help function with alignment to make compiler happy Nov 8, 2024
@tw4452852
Copy link
Contributor Author

Updated with @InKryption 's suggestion.

@alexrp There two things here to make BPF unusual:

  • BPF helpers are just a dummy call, according to the manual:

Internally, eBPF programs call directly into the compiled helper
functions without requiring any foreign-function interface. As a
result, calling helpers introduces no overhead, thus offering
excellent performance.

  • BPF programs are usually generated w/o linking, they are only being compiled(Currently no compiler supports linking BPF programs). Here's the example from linux kernel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants