Skip to contentSkip to conversation

Undefined behavior in eBPF RFC

Bpf Alexei Starovoitov 1 participant 1 message Jan 23 2 messages not in the archive BPF

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 26 lines · Tristan Itschner

Hi there again,

first of all, I would like to apologize for the unprofessional tune in my initial message. Sorry for realizing that only in retrospect.

My concern was as follows: In pipelined RTL CPU implementations, it makes a significant difference, whether unaligned or only aligned loads and stores are allowed. Implementing unaligned loads requires quite a bit more logic, especially when optimizing certain fast cases. Knowing from my background that cBPF requires unaligned loads, I assumed that eBPF does the same in principle. My concern was that someone only implements aligned loads and stores in an eBPF hardware implementation, because it is not explicitly stated. In fact, the word alignment does not appear at all in the RFC. This would break jitting cBPF programs to eBPF programs, because then the behavior of unaligned loads is undefined. But you would not notice it in practice if you were only using C programs, because the compiler only generates aligned loads and store (unless you tell it to do otherwise). This is only an issue in the context of processing packets with variable header length, such as is done with cBPF. But cBPF is enough for that of course.

However, I have realized now by re-reading the spec, that eBPF allows only aligned loads at all (I misread the C-like syntax), which of course makes a lot more sense for the actual use-cases of eBPF.

Quoted 6 lines

I hope you do understand now where I was initial coming from. I'm a big fan of both BPF implementations, so please don't get me wrong. But my concern is unfounded besides being understood as a suggestion to clarify that behavior in the RFC.

Best regards, Tristan Itschner

Hey,
Sorry for the delay.

eBPF actually allows unaligned loads.
See
https://docs.ebpf.io/linux/syscall/BPF_PROG_LOAD/#bpf_f_any_alignment

BPF_F_ANY_ALIGNMENT and BPF_F_STRICT_ALIGNMENT flags.

It's not documented explicitly in the spec, but any new HW or SW implementing support for eBPF must support unaligned load/store without heavy penalty like a trap. Otherwise it will break performance expectations of eBPF programs.