요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.
1. 요약·해설
원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.
2. 영어 원문 전체
번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.
원문 전체 펼치기
==============
BPF Design Q&A
==============
BPF extensibility and applicability to networking, tracing, security
in the linux kernel and several user space implementations of BPF
virtual machine led to a number of misunderstanding on what BPF actually is.
This short QA is an attempt to address that and outline a direction
of where BPF is heading long term.
.. contents::
:local:
:depth: 3
Questions and Answers
=====================
Q: Is BPF a generic instruction set similar to x64 and arm64?
-------------------------------------------------------------
A: NO.
Q: Is BPF a generic virtual machine ?
-------------------------------------
A: NO.
BPF is generic instruction set *with* C calling convention.
-----------------------------------------------------------
Q: Why C calling convention was chosen?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A: Because BPF programs are designed to run in the linux kernel
which is written in C, hence BPF defines instruction set compatible
with two most used architectures x64 and arm64 (and takes into
consideration important quirks of other architectures) and
defines calling convention that is compatible with C calling
convention of the linux kernel on those architectures.
Q: Can multiple return values be supported in the future?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A: NO. BPF allows only register R0 to be used as return value.
Q: Can more than 5 function arguments be supported in the future?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A: NO. BPF calling convention only allows registers R1-R5 to be used
as arguments. BPF is not a standalone instruction set.
(unlike x64 ISA that allows msft, cdecl and other conventions)
Q: Can BPF programs access instruction pointer or return address?
-----------------------------------------------------------------
A: NO.
Q: Can BPF programs access stack pointer ?
------------------------------------------
A: NO.
Only frame pointer (register R10) is accessible.
From compiler point of view it's necessary to have stack pointer.
For example, LLVM defines register R11 as stack pointer in its
BPF backend, but it makes sure that generated code never uses it.
Q: Does C-calling convention diminishes possible use cases?
-----------------------------------------------------------
A: YES.
BPF design forces addition of major functionality in the form
of kernel helper functions and kernel objects like BPF maps with
seamless interoperability between them. It lets kernel call into
BPF programs and programs call kernel helpers with zero overhead,
as all of them were native C code. That is particularly the case
for JITed BPF programs that are indistinguishable from
native kernel C code.
Q: Does it mean that 'innovative' extensions to BPF code are disallowed?
------------------------------------------------------------------------
A: Soft yes.
At least for now, until BPF core has support for
bpf-to-bpf calls, indirect calls, loops, global variables,
jump tables, read-only sections, and all other normal constructs
that C code can produce.
Q: Can loops be supported in a safe way?
----------------------------------------
A: It's not clear yet.
BPF developers are trying to find a way to
support bounded loops.
Q: What are the verifier limits?
--------------------------------
A: The only limit known to the user space is BPF_MAXINSNS (4096).
It's the maximum number of instructions that the unprivileged bpf
program can have. The verifier has various internal limits.
Like the maximum number of instructions that can be explored during
program analysis. Currently, that limit is set to 1 million.
Which essentially means that the largest program can consist
of 1 million NOP instructions. There is a limit to the maximum number
of subsequent branches, a limit to the number of nested bpf-to-bpf
calls, a limit to the number of the verifier states per instruction,
a limit to the number of maps used by the program.
All these limits can be hit with a sufficiently complex program.
There are also non-numerical limits that can cause the program
to be rejected. The verifier used to recognize only pointer + constant
expressions. Now it can recognize pointer + bounded_register.
bpf_lookup_map_elem(key) had a requirement that 'key' must be
a pointer to the stack. Now, 'key' can be a pointer to map value.
The verifier is steadily getting 'smarter'. The limits are
being removed. The only way to know that the program is going to
be accepted by the verifier is to try to load it.
The bpf development process guarantees that the future kernel
versions will accept all bpf programs that were accepted by
the earlier versions.
Instruction level questions
---------------------------
Q: LD_ABS and LD_IND instructions vs C code
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q: How come LD_ABS and LD_IND instruction are present in BPF whereas
C code cannot express them and has to use builtin intrinsics?
A: This is artifact of compatibility with classic BPF. Modern
networking code in BPF performs better without them.
See 'direct packet access'.
Q: BPF instructions mapping not one-to-one to native CPU
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q: It seems not all BPF instructions are one-to-one to native CPU.
For example why BPF_JNE and other compare and jumps are not cpu-like?
A: This was necessary to avoid introducing flags into ISA which are
impossible to make generic and efficient across CPU architectures.
Q: Why BPF_DIV instruction doesn't map to x64 div?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A: Because if we picked one-to-one relationship to x64 it would have made
it more complicated to support on arm64 and other archs. Also it
needs div-by-zero runtime check.
Q: Why BPF has implicit prologue and epilogue?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A: Because architectures like sparc have register windows and in general
there are enough subtle differences between architectures, so naive
store return address into stack won't work. Another reason is BPF has
to be safe from division by zero (and legacy exception path
of LD_ABS insn). Those instructions need to invoke epilogue and
return implicitly.
Q: Why BPF_JLT and BPF_JLE instructions were not introduced in the beginning?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A: Because classic BPF didn't have them and BPF authors felt that compiler
workaround would be acceptable. Turned out that programs lose performance
due to lack of these compare instructions and they were added.
These two instructions is a perfect example what kind of new BPF
instructions are acceptable and can be added in the future.
These two already had equivalent instructions in native CPUs.
New instructions that don't have one-to-one mapping to HW instructions
will not be accepted.
Q: BPF 32-bit subregister requirements
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q: BPF 32-bit subregisters have a requirement to zero upper 32-bits of BPF
registers which makes BPF inefficient virtual machine for 32-bit
CPU architectures and 32-bit HW accelerators. Can true 32-bit registers
be added to BPF in the future?
A: NO.
But some optimizations on zero-ing the upper 32 bits for BPF registers are
available, and can be leveraged to improve the performance of JITed BPF
programs for 32-bit architectures.
Starting with version 7, LLVM is able to generate instructions that operate
on 32-bit subregisters, provided the option -mattr=+alu32 is passed for
compiling a program. Furthermore, the verifier can now mark the
instructions for which zero-ing the upper bits of the destination register
is required, and insert an explicit zero-extension (zext) instruction
(a mov32 variant). This means that for architectures without zext hardware
support, the JIT back-ends do not need to clear the upper bits for
subregisters written by alu32 instructions or narrow loads. Instead, the
back-ends simply need to support code generation for that mov32 variant,
and to overwrite bpf_jit_needs_zext() to make it return "true" (in order to
enable zext insertion in the verifier).
Note that it is possible for a JIT back-end to have partial hardware
support for zext. In that case, if verifier zext insertion is enabled,
it could lead to the insertion of unnecessary zext instructions. Such
instructions could be removed by creating a simple peephole inside the JIT
back-end: if one instruction has hardware support for zext and if the next
instruction is an explicit zext, then the latter can be skipped when doing
the code generation.
Q: Does BPF have a stable ABI?
------------------------------
A: YES. BPF instructions, arguments to BPF programs, set of helper
functions and their arguments, recognized return codes are all part
of ABI. However there is one specific exception to tracing programs
which are using helpers like bpf_probe_read() to walk kernel internal
data structures and compile with kernel internal headers. Both of these
kernel internals are subject to change and can break with newer kernels
such that the program needs to be adapted accordingly.
New BPF functionality is generally added through the use of kfuncs instead of
new helpers. Kfuncs are not considered part of the stable API, and have their own
lifecycle expectations as described in :ref:`BPF_kfunc_lifecycle_expectations`.
Q: Are tracepoints part of the stable ABI?
------------------------------------------
A: NO. Tracepoints are tied to internal implementation details hence they are
subject to change and can break with newer kernels. BPF programs need to change
accordingly when this happens.
Q: Are places where kprobes can attach part of the stable ABI?
--------------------------------------------------------------
A: NO. The places to which kprobes can attach are internal implementation
details, which means that they are subject to change and can break with
newer kernels. BPF programs need to change accordingly when this happens.
Q: How much stack space a BPF program uses?
-------------------------------------------
A: Currently all program types are limited to 512 bytes of stack
space, but the verifier computes the actual amount of stack used
and both interpreter and most JITed code consume necessary amount.
Q: Can BPF be offloaded to HW?
------------------------------
A: YES. BPF HW offload is supported by NFP driver.
Q: Does classic BPF interpreter still exist?
--------------------------------------------
A: NO. Classic BPF programs are converted into extend BPF instructions.
Q: Can BPF call arbitrary kernel functions?
-------------------------------------------
A: NO. BPF programs can only call specific functions exposed as BPF helpers or
kfuncs. The set of available functions is defined for every program type.
Q: Can BPF overwrite arbitrary kernel memory?
---------------------------------------------
A: NO.
Tracing bpf programs can *read* arbitrary memory with bpf_probe_read()
and bpf_probe_read_str() helpers. Networking programs cannot read
arbitrary memory, since they don't have access to these helpers.
Programs can never read or write arbitrary memory directly.
Q: Can BPF overwrite arbitrary user memory?
-------------------------------------------
A: Sort-of.
Tracing BPF programs can overwrite the user memory
of the current task with bpf_probe_write_user(). Every time such
program is loaded the kernel will print warning message, so
this helper is only useful for experiments and prototypes.
Tracing BPF programs are root only.
Q: New functionality via kernel modules?
----------------------------------------
Q: Can BPF functionality such as new program or map types, new
helpers, etc be added out of kernel module code?
A: Yes, through kfuncs and kptrs
The core BPF functionality such as program types, maps and helpers cannot be
added to by modules. However, modules can expose functionality to BPF programs
by exporting kfuncs (which may return pointers to module-internal data
structures as kptrs).
Q: Directly calling kernel function is an ABI?
----------------------------------------------
Q: Some kernel functions (e.g. tcp_slow_start) can be called
by BPF programs. Do these kernel functions become an ABI?
A: NO.
The kernel function protos will change and the bpf programs will be
rejected by the verifier. Also, for example, some of the bpf-callable
kernel functions have already been used by other kernel tcp
cc (congestion-control) implementations. If any of these kernel
functions has changed, both the in-tree and out-of-tree kernel tcp cc
implementations have to be changed. The same goes for the bpf
programs and they have to be adjusted accordingly. See
:ref:`BPF_kfunc_lifecycle_expectations` for details.
Q: Attaching to arbitrary kernel functions is an ABI?
-----------------------------------------------------
Q: BPF programs can be attached to many kernel functions. Do these
kernel functions become part of the ABI?
A: NO.
The kernel function prototypes will change, and BPF programs attaching to
them will need to change. The BPF compile-once-run-everywhere (CO-RE)
should be used in order to make it easier to adapt your BPF programs to
different versions of the kernel.
Q: Marking a function with BTF_ID makes that function an ABI?
-------------------------------------------------------------
A: NO.
The BTF_ID macro does not cause a function to become part of the ABI
any more than does the EXPORT_SYMBOL_GPL macro.
Q: What is the compatibility story for special BPF types in map values?
-----------------------------------------------------------------------
Q: Users are allowed to embed bpf_spin_lock, bpf_timer fields in their BPF map
values (when using BTF support for BPF maps). This allows to use helpers for
such objects on these fields inside map values. Users are also allowed to embed
pointers to some kernel types (with __kptr_untrusted and __kptr BTF tags). Will the
kernel preserve backwards compatibility for these features?
A: It depends. For bpf_spin_lock, bpf_timer: YES, for kptr and everything else:
NO, but see below.
For struct types that have been added already, like bpf_spin_lock and bpf_timer,
the kernel will preserve backwards compatibility, as they are part of UAPI.
For kptrs, they are also part of UAPI, but only with respect to the kptr
mechanism. The types that you can use with a __kptr_untrusted and __kptr tagged
pointer in your struct are NOT part of the UAPI contract. The supported types can
and will change across kernel releases. However, operations like accessing kptr
fields and bpf_kptr_xchg() helper will continue to be supported across kernel
releases for the supported types.
For any other supported struct type, unless explicitly stated in this document
and added to bpf.h UAPI header, such types can and will arbitrarily change their
size, type, and alignment, or any other user visible API or ABI detail across
kernel releases. The users must adapt their BPF programs to the new changes and
update them to make sure their programs continue to work correctly.
NOTE: BPF subsystem specially reserves the 'bpf\_' prefix for type names, in
order to introduce more special fields in the future. Hence, user programs must
avoid defining types with 'bpf\_' prefix to not be broken in future releases.
In other words, no backwards compatibility is guaranteed if one using a type
in BTF with 'bpf\_' prefix.
Q: What is the compatibility story for special BPF types in allocated objects?
------------------------------------------------------------------------------
Q: Same as above, but for allocated objects (i.e. objects allocated using
bpf_obj_new for user defined types). Will the kernel preserve backwards
compatibility for these features?
A: NO.
Unlike map value types, the API to work with allocated objects and any support
for special fields inside them is exposed through kfuncs, and thus has the same
lifecycle expectations as the kfuncs themselves. See
:ref:`BPF_kfunc_lifecycle_expectations` for details.
3. 한국어 전문 번역
영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.
BPF의 정체성과 C calling convention
1-28Linux kernel의 networking·tracing·security에 대한 BPF의 확장성과 적용 범위, 그리고 여러 userspace BPF virtual machine 구현 때문에 BPF가 실제로 무엇인지 오해가 생겼습니다. 이 짧은 Q&A는 그 오해를 바로잡고 BPF의 장기 방향을 설명합니다.
질문: BPF는 x64나 arm64와 비슷한 generic instruction set입니까? 답변: `NO`.
질문: BPF는 generic virtual machine입니까? 답변: `NO`.
BPF는 C calling convention을 함께 정의한 generic instruction set입니다.
Return value와 function argument
29-47질문: 왜 C calling convention을 선택했습니까? 답변: BPF program은 C로 작성된 Linux kernel에서 실행하도록 설계됐기 때문입니다. BPF는 가장 널리 쓰이는 x64·arm64와 compatible한 instruction set을 정의하고 다른 architecture의 중요한 특성도 고려합니다. 또한 해당 architecture에서 Linux kernel의 C calling convention과 compatible한 calling convention을 정의합니다.
질문: 앞으로 여러 return value를 지원할 수 있습니까? 답변: `NO`. BPF는 register `R0`만 return value로 사용할 수 있습니다.
질문: 앞으로 function argument를 5개보다 많이 지원할 수 있습니까? 답변: `NO`. BPF calling convention은 register `R1-R5`만 argument로 허용합니다. BPF는 msft, cdecl 등 여러 convention을 허용하는 x64 ISA와 달리 standalone instruction set이 아닙니다.
Instruction·stack pointer와 C interoperation
48-72질문: BPF program이 instruction pointer 또는 return address에 access할 수 있습니까? 답변: `NO`.
질문: BPF program이 stack pointer에 access할 수 있습니까? 답변: `NO`. frame pointer인 register `R10`만 access할 수 있습니다.
compiler 관점에서는 stack pointer가 필요합니다. 예를 들어 LLVM은 BPF backend에서 register `R11`을 stack pointer로 정의하지만 생성 code가 이를 사용하지 않도록 보장합니다.
질문: C calling convention이 가능한 use case를 줄입니까? 답변: `YES`.
BPF design은 major functionality를 kernel helper function과 BPF map 같은 kernel object 형태로 추가하고 이들 사이의 seamless interoperability를 요구합니다. kernel은 BPF program을 호출하고 program은 kernel helper를 native C code처럼 overhead 없이 호출할 수 있습니다. 특히 JIT된 BPF program은 native kernel C code와 구별되지 않습니다.
Extension·loop·verifier limit
73-110질문: BPF code의 'innovative' extension은 허용되지 않습니까? 답변: `Soft yes`. 적어도 BPF core가 bpf-to-bpf call, indirect call, loop, global variable, jump table, read-only section과 C code가 생성할 수 있는 일반 construct를 모두 지원할 때까지는 그렇습니다.
질문: loop를 안전하게 지원할 수 있습니까? 답변: 아직 명확하지 않습니다. BPF developer는 bounded loop를 지원할 방법을 찾고 있습니다.
질문: verifier limit은 무엇입니까? 답변: userspace에 알려진 유일한 limit은 `BPF_MAXINSNS (4096)`이며 unprivileged BPF program의 최대 instruction 수입니다.
verifier에는 program analysis에서 탐색할 수 있는 instruction 최대 수 등 여러 internal limit이 있습니다. 현재 탐색 limit은 `1 million`이므로 가장 큰 program은 본질적으로 NOP instruction 100만 개로 구성할 수 있습니다.
연속 branch 최대 수, nested bpf-to-bpf call 수, instruction별 verifier state 수, program이 사용하는 map 수에도 limit이 있습니다. 충분히 복잡한 program은 이 limit에 도달할 수 있습니다.
program을 reject하는 non-numerical limit도 있습니다. verifier는 과거에 `pointer + constant` expression만 인식했지만 이제 `pointer + bounded_register`를 인식합니다. `bpf_lookup_map_elem(key)`의 `key`는 과거 stack pointer여야 했지만 이제 map value pointer일 수도 있습니다.
verifier는 계속 더 똑똑해지고 limit은 제거되고 있습니다. program이 accepted될지 아는 유일한 방법은 load를 시도하는 것입니다.
Accepted program 호환성과 LD_ABS·LD_IND
111-128BPF development process는 이전 kernel version에서 accepted된 모든 BPF program을 미래 kernel version도 accept하도록 보장합니다.
질문: C code로 표현할 수 없어 builtin intrinsic을 써야 하는 `LD_ABS`, `LD_IND` instruction이 BPF에 있는 이유는 무엇입니까? 답변: classic BPF와의 compatibility에서 남은 artifact입니다. modern BPF networking code는 이 instruction 없이 더 잘 동작합니다. `direct packet access`를 참조하십시오.
Native CPU mapping과 BPF instruction
129-161질문: 모든 BPF instruction이 native CPU와 1:1 mapping되지 않는 이유, 예를 들어 `BPF_JNE`와 compare-and-jump가 CPU와 같은 형태가 아닌 이유는 무엇입니까? 답변: CPU architecture 전체에서 generic하면서 efficient하게 만들 수 없는 flag를 ISA에 도입하지 않기 위해 필요했습니다.
질문: `BPF_DIV` instruction이 x64 `div`에 mapping되지 않는 이유는 무엇입니까? 답변: x64와 1:1 관계를 선택하면 arm64와 다른 architecture 지원이 복잡해지고, division-by-zero runtime check도 필요하기 때문입니다.
질문: BPF에 implicit prologue와 epilogue가 있는 이유는 무엇입니까? 답변: sparc 같은 architecture에는 register window가 있고 architecture 사이에 미묘한 차이가 많아 return address를 stack에 단순 저장하는 방식이 동작하지 않기 때문입니다. BPF는 division by zero와 legacy `LD_ABS` instruction exception path에서도 안전해야 하므로 해당 instruction은 epilogue를 invoke하고 implicit return해야 합니다.
질문: `BPF_JLT`와 `BPF_JLE` instruction을 처음부터 도입하지 않은 이유는 무엇입니까? 답변: classic BPF에 없었고 compiler workaround로 충분할 것으로 봤기 때문입니다. 그러나 compare instruction 부재로 성능이 떨어져 나중에 추가됐습니다.
`BPF_JLT`와 `BPF_JLE`는 미래에 허용할 수 있는 새 BPF instruction의 좋은 예입니다. native CPU에 이미 equivalent instruction이 있었기 때문입니다. HW instruction과 1:1 mapping되지 않는 새 instruction은 허용되지 않습니다.
32-bit subregister와 zext
162-194질문: BPF 32-bit subregister는 BPF register의 upper 32-bit를 0으로 만들어야 하므로 32-bit CPU architecture와 accelerator에서 비효율적입니다. 앞으로 true 32-bit register를 추가할 수 있습니까? 답변: `NO`.
하지만 upper 32-bit zeroing을 최적화해 32-bit architecture의 JIT된 BPF program 성능을 높일 수 있습니다.
LLVM 7부터 program compile 시 `-mattr=+alu32` option을 주면 32-bit subregister에서 동작하는 instruction을 생성할 수 있습니다. verifier도 destination register의 upper bit zeroing이 필요한 instruction을 표시하고 explicit zero-extension인 `zext` instruction, 즉 `mov32` variant를 삽입할 수 있습니다.
zext hardware support가 없는 architecture의 JIT backend는 alu32 instruction이나 narrow load가 쓴 subregister의 upper bit를 직접 clear할 필요가 없습니다. `mov32` variant code generation을 지원하고 `bpf_jit_needs_zext()`를 override해 `true`를 반환하도록 하면 verifier의 zext insertion이 활성화됩니다.
JIT backend가 zext를 부분적으로 hardware 지원할 수도 있습니다. 이때 verifier zext insertion은 불필요한 zext instruction을 넣을 수 있습니다. zext를 hardware로 처리하는 instruction 바로 뒤에 explicit zext가 오면 code generation에서 뒤 instruction을 생략하는 간단한 peephole을 JIT backend에 구현해 제거할 수 있습니다.
Stable ABI와 tracing attachment
195-220질문: BPF는 stable ABI를 가집니까? 답변: `YES`. BPF instruction, BPF program argument, helper function 집합과 argument, 인식되는 return code는 모두 ABI의 일부입니다.
예외는 `bpf_probe_read()` 같은 helper로 kernel internal data structure를 순회하고 kernel internal header로 compile하는 tracing program입니다. 두 kernel internal은 변경될 수 있어 새 kernel에서 program이 깨지면 맞게 수정해야 합니다.
새 BPF functionality는 보통 새 helper 대신 kfunc로 추가합니다. kfunc는 stable API의 일부가 아니며 `BPF_kfunc_lifecycle_expectations`에 설명한 자체 lifecycle expectation을 가집니다.
질문: tracepoint는 stable ABI의 일부입니까? 답변: `NO`. internal implementation detail에 묶여 있어 새 kernel에서 변경되고 깨질 수 있으며 BPF program도 수정해야 합니다.
질문: kprobe를 attach할 수 있는 위치는 stable ABI의 일부입니까? 답변: `NO`. attach 위치는 internal implementation detail이므로 변경될 수 있고 BPF program도 맞춰 수정해야 합니다.
Stack·offload·helper와 kfunc
221-239질문: BPF program은 stack space를 얼마나 사용합니까? 답변: 현재 모든 program type은 `512 bytes`로 제한되지만 verifier가 실제 사용량을 계산하므로 interpreter와 대부분의 JIT code는 필요한 양만 사용합니다.
질문: BPF를 HW로 offload할 수 있습니까? 답변: `YES`. NFP driver가 BPF HW offload를 지원합니다.
질문: classic BPF interpreter가 아직 존재합니까? 답변: `NO`. classic BPF program은 extended BPF instruction으로 변환됩니다.
질문: BPF가 임의의 kernel function을 호출할 수 있습니까? 답변: `NO`. BPF program은 BPF helper 또는 kfunc로 노출된 특정 function만 호출할 수 있으며 사용 가능한 function 집합은 program type마다 정의됩니다.
Kernel·user memory access
240-259질문: BPF가 임의의 kernel memory를 overwrite할 수 있습니까? 답변: `NO`.
tracing BPF program은 `bpf_probe_read()`와 `bpf_probe_read_str()` helper로 임의 memory를 read할 수 있습니다. networking program은 이 helper에 access할 수 없어 임의 memory를 읽을 수 없습니다. 어떤 program도 임의 memory를 직접 read하거나 write할 수 없습니다.
질문: BPF가 임의의 user memory를 overwrite할 수 있습니까? 답변: `Sort-of`.
tracing BPF program은 `bpf_probe_write_user()`로 current task의 user memory를 overwrite할 수 있습니다. 이 program을 load할 때마다 kernel이 warning을 출력하므로 실험과 prototype에만 유용합니다. tracing BPF program은 root만 사용할 수 있습니다.
Kernel module·kfunc·direct call
260-286질문: 새 program type, map type, helper 같은 BPF functionality를 kernel module code 밖에서 추가할 수 있습니까? 답변: `Yes, through kfuncs and kptrs`.
program type, map, helper 같은 core BPF functionality는 module이 추가할 수 없습니다. 하지만 module은 kfunc를 export해 BPF program에 functionality를 노출할 수 있고, kfunc는 module 내부 data structure pointer를 kptr로 반환할 수 있습니다.
질문: `tcp_slow_start`처럼 BPF program이 직접 호출할 수 있는 kernel function은 ABI가 됩니까? 답변: `NO`.
kernel function prototype이 바뀌면 verifier가 BPF program을 reject합니다. 일부 BPF-callable kernel function은 다른 kernel TCP congestion-control 구현도 사용합니다. function이 바뀌면 in-tree·out-of-tree kernel TCP cc 구현과 BPF program을 모두 맞춰 수정해야 합니다. 자세한 내용은 `BPF_kfunc_lifecycle_expectations`를 참조하십시오.
Function attachment·CO-RE·BTF_ID
287-305질문: BPF program을 attach할 수 있는 많은 kernel function이 ABI의 일부가 됩니까? 답변: `NO`. kernel function prototype은 변경될 수 있고 attach하는 BPF program도 변경해야 합니다.
BPF compile-once-run-everywhere(CO-RE)를 사용하면 서로 다른 kernel version에 BPF program을 더 쉽게 맞출 수 있습니다.
질문: function에 `BTF_ID`를 표시하면 ABI가 됩니까? 답변: `NO`. `BTF_ID` macro는 `EXPORT_SYMBOL_GPL` macro와 마찬가지로 function을 ABI 일부로 만들지 않습니다.
Map value의 special BPF type 호환성
306-338질문: BTF 기반 BPF map value에 `bpf_spin_lock`, `bpf_timer` field나 `__kptr_untrusted`, `__kptr` BTF tag가 붙은 kernel type pointer를 넣을 수 있습니다. kernel이 이 feature의 backwards compatibility를 보존합니까?
답변: `bpf_spin_lock`과 `bpf_timer`는 `YES`, kptr와 그 밖의 모든 것은 `NO`이지만 아래 설명을 따릅니다.
`bpf_spin_lock`, `bpf_timer`처럼 이미 추가된 struct type은 UAPI의 일부이므로 kernel이 backwards compatibility를 보존합니다.
kptr도 kptr mechanism 자체에 대해서는 UAPI의 일부입니다. 그러나 struct에서 `__kptr_untrusted` 또는 `__kptr` tagged pointer로 사용할 수 있는 type은 UAPI contract의 일부가 아니며 kernel release 사이에서 바뀔 수 있습니다. 지원되는 type에 대한 kptr field access와 `bpf_kptr_xchg()` helper 같은 operation은 계속 지원됩니다.
그 밖의 supported struct type은 이 문서에 명시하고 `bpf.h` UAPI header에 추가한 경우가 아니면 kernel release 사이에서 size, type, alignment와 모든 user-visible API·ABI detail이 임의로 바뀔 수 있습니다. user는 program을 새 변경에 맞춰 update해야 합니다.
BPF subsystem은 미래의 special field 도입을 위해 type name의 `bpf_` prefix를 예약합니다. user program은 미래 release에서 깨지지 않도록 `bpf_` prefix type을 정의하면 안 됩니다. BTF에서 이 prefix의 type을 사용하면 backwards compatibility를 보장하지 않습니다.
Allocated object의 special type
339-351질문: `bpf_obj_new`로 user-defined type을 할당한 object에서도 위와 같은 special field의 backwards compatibility를 kernel이 보존합니까? 답변: `NO`.
map value type과 달리 allocated object를 다루는 API와 내부 special field 지원은 kfunc로 노출되므로 kfunc 자체와 같은 lifecycle expectation을 가집니다. 자세한 내용은 `BPF_kfunc_lifecycle_expectations`를 참조하십시오.
요약과 해설
bpf_design_QA.rst:1-351BPF는 범용 VM이 아니라 Linux kernel의 C calling convention과 결합된 instruction set입니다. `R0` return, `R1-R5` argument, `R10` frame pointer 같은 고정 규칙이 kernel helper·map과의 낮은 overhead interoperation을 가능하게 합니다.
verifier는 accepted program의 미래 호환성을 보장하지만 program acceptance는 실제 load로 확인해야 합니다. BPF instruction과 helper ABI는 stable한 반면 tracepoint, kprobe 위치, kfunc, 직접 호출하거나 attach하는 kernel function은 internal detail에 따라 변할 수 있습니다.
UAPI에 명시된 `bpf_spin_lock`과 `bpf_timer`는 호환성을 유지하지만 kptr이 가리킬 수 있는 kernel type과 allocated-object kfunc API는 stable contract가 아닙니다. CO-RE와 BTF를 사용해 kernel version 변화에 적응해야 합니다.