Skip to contentSkip to conversation

split up filter.rst v2

PATCH In review Christoph Hellwig 3 participants 12 messages Nov 19, 2021 – Nov 30, 2021

Subsystems (14)
lore
Share & tools
lore
#0 lore raw

Hi all,

for historical reasons filter.rst not only documents the classic Berkely Packet Filter, but also contains some of the most fundamental eBPF documentation. This series moves the actual eBPF documentation into newly created files under Documentation/bpf/ instead. Note that the instruction set document is still a bit of a mess due to all the references to classic BPF, but if this split goes through I plan to start on working to clean that up as well.

Changes since v1:
- rebased to the latest bpf-next
- just refernence BPF instead of eBPF in most code
- split the patches further up
- better link the BPF documentation from filter.rst

Diffstat:

 Documentation/bpf/index.rst           |    9 
 Documentation/bpf/instruction-set.rst |  467 +++++++++++++++
 Documentation/bpf/maps.rst            |   43 +
 Documentation/bpf/verifier.rst        |  529 +++++++++++++++++
 Documentation/networking/filter.rst   | 1036 ----------------------------------
 arch/arm/net/bpf_jit_32.c             |    2 
 arch/arm64/net/bpf_jit_comp.c         |    2 
 arch/sparc/net/bpf_jit_comp_64.c      |    2 
 arch/x86/net/bpf_jit_comp.c           |    4 
 kernel/bpf/core.c                     |    3 
 net/core/filter.c                     |   11 
 11 files changed, 1063 insertions(+), 1045 deletions(-)
#1 lore raw

PATCH 1/5 x86, bpf: cleanup the top of file header in bpf_jit_comp.c

Don't bother mentioning the file name as it is implied, and remove the reference to internal BPF.

---
 arch/x86/net/bpf_jit_comp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/net/bpf_jit_comp.c +2 -2
index 6318479077867..1d7b0c69b644d 100644
@@ -1,9 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
- * bpf_jit_comp.c: BPF JIT compiler
+ * BPF JIT compiler
  *
  * Copyright (C) 2011-2013 Eric Dumazet ([email protected])
- * Internal BPF Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
+ * Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  */
 #include <linux/netdevice.h>
 #include <linux/filter.h>
Signature · 1 line
2.30.2
#3 lore raw

PATCH 2/5 bpf: remove a redundant comment on bpf_prog_free

In reply to Christoph Hellwig

The comment telling that the prog_free helper is freeing the program is not exactly useful, so just remove it.

---
 kernel/bpf/core.c | 1 -
 1 file changed, 1 deletion(-)
kernel/bpf/core.c +0 -1
index b52dc845ecea3..189d85d64bf1c 100644
@@ -2301,7 +2301,6 @@ static void bpf_prog_free_deferred(struct work_struct *work)
 	}
 }
 
-/* Free internal BPF program */
 void bpf_prog_free(struct bpf_prog *fp)
 {
 	struct bpf_prog_aux *aux = fp->aux;
Signature · 1 line
2.30.2
#5 lore raw

PATCH 3/5 bpf, docs: prune all references to "internal BPF"

In reply to Christoph Hellwig

The eBPF name has completely taken over from eBPF in general usage for the actual eBPF representation, or BPF for any general in-kernel use. Prune all remaining references to "internal BPF".

---
 Documentation/networking/filter.rst | 22 +++++++++++-----------
 arch/arm/net/bpf_jit_32.c           |  2 +-
 arch/arm64/net/bpf_jit_comp.c       |  2 +-
 arch/sparc/net/bpf_jit_comp_64.c    |  2 +-
 kernel/bpf/core.c                   |  2 +-
 net/core/filter.c                   | 11 +++++------
 6 files changed, 20 insertions(+), 21 deletions(-)
Documentation/networking/filter.rst +11 -11
index ce2b8e8bb9ab6..83ffcaa5b91aa 100644
@@ -617,7 +617,7 @@ format with similar underlying principles from BPF described in previous
 paragraphs is being used. However, the instruction set format is modelled
 closer to the underlying architecture to mimic native instruction sets, so
 that a better performance can be achieved (more details later). This new
-ISA is called 'eBPF' or 'internal BPF' interchangeably. (Note: eBPF which
+ISA is called 'eBPF'. (Note: eBPF which
 originates from [e]xtended BPF is not the same as BPF extensions! While
 eBPF is an ISA, BPF extensions date back to classic BPF's 'overloading'
 of BPF_LD | BPF_{B,H,W} | BPF_ABS instruction.)
@@ -690,7 +690,7 @@ Some core changes of the new internal format:
   That behavior maps directly to x86_64 and arm64 subregister definition, but
   makes other JITs more difficult.
 
-  32-bit architectures run 64-bit internal BPF programs via interpreter.
+  32-bit architectures run 64-bit eBPF programs via interpreter.
   Their JITs may convert BPF programs that only use 32-bit subregisters into
   native instruction set and let the rest being interpreted.
 
@@ -711,7 +711,7 @@ Some core changes of the new internal format:
 - Introduces bpf_call insn and register passing convention for zero overhead
   calls from/to other kernel functions:
 
-  Before an in-kernel function call, the internal BPF program needs to
+  Before an in-kernel function call, the eBPF program needs to
   place function arguments into R1 to R5 registers to satisfy calling
   convention, then the interpreter will take them from registers and pass
   to in-kernel function. If R1 - R5 registers are mapped to CPU registers
@@ -780,7 +780,7 @@ Some core changes of the new internal format:
   ... since x86_64 ABI mandates rdi, rsi, rdx, rcx, r8, r9 for argument passing
   and rbx, r12 - r15 are callee saved.
 
-  Then the following internal BPF pseudo-program::
+  Then the following eBPF pseudo-program::
 
     bpf_mov R6, R1 /* save ctx */
     bpf_mov R2, 2
@@ -846,7 +846,7 @@ Some core changes of the new internal format:
     bpf_exit
 
   After the call the registers R1-R5 contain junk values and cannot be read.
-  An in-kernel eBPF verifier is used to validate internal BPF programs.
+  An in-kernel eBPF verifier is used to validate eBPF programs.
 
 Also in the new design, eBPF is limited to 4096 insns, which means that any
 program will terminate quickly and will only call a fixed number of kernel
@@ -861,23 +861,23 @@ A program, that is translated internally consists of the following elements::
 
   op:16, jt:8, jf:8, k:32    ==>    op:8, dst_reg:4, src_reg:4, off:16, imm:32
 
-So far 87 internal BPF instructions were implemented. 8-bit 'op' opcode field
+So far 87 eBPF instructions were implemented. 8-bit 'op' opcode field
 has room for new instructions. Some of them may use 16/24/32 byte encoding. New
 instructions must be multiple of 8 bytes to preserve backward compatibility.
 
-Internal BPF is a general purpose RISC instruction set. Not every register and
+eBPF is a general purpose RISC instruction set. Not every register and
 every instruction are used during translation from original BPF to new format.
 For example, socket filters are not using ``exclusive add`` instruction, but
 tracing filters may do to maintain counters of events, for example. Register R9
 is not used by socket filters either, but more complex filters may be running
 out of registers and would have to resort to spill/fill to stack.
 
-Internal BPF can be used as a generic assembler for last step performance
+eBPF can be used as a generic assembler for last step performance
 optimizations, socket filters and seccomp are using it as assembler. Tracing
 filters may use it as assembler to generate code from kernel. In kernel usage
-may not be bounded by security considerations, since generated internal BPF code
+may not be bounded by security considerations, since generated eBPF code
 may be optimizing internal code path and not being exposed to the user space.
-Safety of internal BPF can come from a verifier (TBD). In such use cases as
+Safety of eBPF can come from a verifier (TBD). In such use cases as
 described, it may be used as safe instruction set.
 
 Just like the original BPF, the new format runs within a controlled environment,
@@ -1675,7 +1675,7 @@ Testing
 -------
 
 Next to the BPF toolchain, the kernel also ships a test module that contains
-various test cases for classic and internal BPF that can be executed against
+various test cases for classic and eBPF that can be executed against
 the BPF interpreter and JIT compiler. It can be found in lib/test_bpf.c and
 enabled via Kconfig::
 
arch/arm/net/bpf_jit_32.c +1 -1
index e59b41e9ab0c1..10ceebb7530b6 100644
@@ -163,7 +163,7 @@ static const s8 bpf2a32[][2] = {
 	[BPF_REG_9] = {STACK_OFFSET(BPF_R9_HI), STACK_OFFSET(BPF_R9_LO)},
 	/* Read only Frame Pointer to access Stack */
 	[BPF_REG_FP] = {STACK_OFFSET(BPF_FP_HI), STACK_OFFSET(BPF_FP_LO)},
-	/* Temporary Register for internal BPF JIT, can be used
+	/* Temporary Register for BPF JIT, can be used
 	 * for constant blindings and others.
 	 */
 	[TMP_REG_1] = {ARM_R7, ARM_R6},
arch/arm64/net/bpf_jit_comp.c +1 -1
index 07c12c42b7517..07aad85848fa3 100644
@@ -44,7 +44,7 @@ static const int bpf2a64[] = {
 	[BPF_REG_9] = A64_R(22),
 	/* read-only frame pointer to access stack */
 	[BPF_REG_FP] = A64_R(25),
-	/* temporary registers for internal BPF JIT */
+	/* temporary registers for BPF JIT */
 	[TMP_REG_1] = A64_R(10),
 	[TMP_REG_2] = A64_R(11),
 	[TMP_REG_3] = A64_R(12),
arch/sparc/net/bpf_jit_comp_64.c +1 -1
index 0bfe1c72a0c9e..b1e38784eb238 100644
@@ -227,7 +227,7 @@ static const int bpf2sparc[] = {
 
 	[BPF_REG_AX] = G7,
 
-	/* temporary register for internal BPF JIT */
+	/* temporary register for BPF JIT */
 	[TMP_REG_1] = G1,
 	[TMP_REG_2] = G2,
 	[TMP_REG_3] = G3,
kernel/bpf/core.c +1 -1
index 189d85d64bf1c..de3e5bc6781fe 100644
@@ -1892,7 +1892,7 @@ static void bpf_prog_select_func(struct bpf_prog *fp)
 
 /**
  *	bpf_prog_select_runtime - select exec runtime for BPF program
- *	@fp: bpf_prog populated with internal BPF program
+ *	@fp: bpf_prog populated with BPF program
  *	@err: pointer to error variable
  *
  * Try to JIT eBPF program, if JIT is not available, use interpreter.
net/core/filter.c +5 -6
index 26e0276aa00de..fe27c91e37580 100644
@@ -1242,10 +1242,9 @@ static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
 	int err, new_len, old_len = fp->len;
 	bool seen_ld_abs = false;
 
-	/* We are free to overwrite insns et al right here as it
-	 * won't be used at this point in time anymore internally
-	 * after the migration to the internal BPF instruction
-	 * representation.
+	/* We are free to overwrite insns et al right here as it won't be used at
+	 * this point in time anymore internally after the migration to the eBPF
+	 * instruction representation.
 	 */
 	BUILD_BUG_ON(sizeof(struct sock_filter) !=
 		     sizeof(struct bpf_insn));
@@ -1336,8 +1335,8 @@ static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
 	 */
 	bpf_jit_compile(fp);
 
-	/* JIT compiler couldn't process this filter, so do the
-	 * internal BPF translation for the optimized interpreter.
+	/* JIT compiler couldn't process this filter, so do the eBPF translation
+	 * for the optimized interpreter.
 	 */
 	if (!fp->jited)
 		fp = bpf_migrate_filter(fp);
Signature · 1 line
2.30.2
reviewer Acked-by on v1 3/5
#6 lore raw

Re: [PATCH 3/5] bpf, docs: prune all references to "internal BPF"

Quoted 6 lines · Christoph Hellwig

The eBPF name has completely taken over from eBPF in general usage for the actual eBPF representation, or BPF for any general in-kernel use. Prune all remaining references to "internal BPF".

#7 lore raw

PATCH 4/5 bpf, docs: move handling of maps to Documentation/bpf/maps.rst

In reply to Christoph Hellwig

Move the general maps documentation into the maps.rst file from the overall networking filter documentation and add a link instead.

---
 Documentation/bpf/maps.rst          | 43 ++++++++++++++++++++++++++
 Documentation/networking/filter.rst | 47 ++---------------------------
 2 files changed, 46 insertions(+), 44 deletions(-)
Documentation/bpf/maps.rst +43 -0
index 2084b0e7cde88..f41619e312ace 100644
@@ -1,4 +1,47 @@
+
+=========
+eBPF maps
 =========
+
+'maps' is a generic storage of different types for sharing data between kernel
+and userspace.
+
+The maps are accessed from user space via BPF syscall, which has commands:
+
+- create a map with given type and attributes
+  ``map_fd = bpf(BPF_MAP_CREATE, union bpf_attr *attr, u32 size)``
+  using attr->map_type, attr->key_size, attr->value_size, attr->max_entries
+  returns process-local file descriptor or negative error
+
+- lookup key in a given map
+  ``err = bpf(BPF_MAP_LOOKUP_ELEM, union bpf_attr *attr, u32 size)``
+  using attr->map_fd, attr->key, attr->value
+  returns zero and stores found elem into value or negative error
+
+- create or update key/value pair in a given map
+  ``err = bpf(BPF_MAP_UPDATE_ELEM, union bpf_attr *attr, u32 size)``
+  using attr->map_fd, attr->key, attr->value
+  returns zero or negative error
+
+- find and delete element by key in a given map
+  ``err = bpf(BPF_MAP_DELETE_ELEM, union bpf_attr *attr, u32 size)``
+  using attr->map_fd, attr->key
+
+- to delete map: close(fd)
+  Exiting process will delete maps automatically
+
+userspace programs use this syscall to create/access maps that eBPF programs
+are concurrently updating.
+
+maps can have different types: hash, array, bloom filter, radix-tree, etc.
+
+The map is defined by:
+
+  - type
+  - max number of elements
+  - key size in bytes
+  - value size in bytes
+
 Map Types
 =========
 
Documentation/networking/filter.rst +3 -44

59 lines not on this page · raw message

Signature · 1 line
2.30.2
#9 lore raw

PATCH 5/5 bpf, docs: split general purpose eBPF documentation out of filter.rst

In reply to Christoph Hellwig

filter.rst starts out documenting the classic BPF and then spills into introducing and documentating eBPF. Move the eBPF documentation into rwo new files under Documentation/bpf/ for the instruction set and the verifier and link to the BPF documentation from filter.rst.

---
 Documentation/bpf/index.rst           |   9 +-
 Documentation/bpf/instruction-set.rst | 467 ++++++++++++
 Documentation/bpf/verifier.rst        | 529 ++++++++++++++
 Documentation/networking/filter.rst   | 993 +-------------------------
 4 files changed, 1008 insertions(+), 990 deletions(-)
 create mode 100644 Documentation/bpf/instruction-set.rst
 create mode 100644 Documentation/bpf/verifier.rst
Documentation/bpf/index.rst +4 -5
index 413f50101eca6..91ba5a62026ba 100644
@@ -5,16 +5,15 @@ BPF Documentation
 This directory contains documentation for the BPF (Berkeley Packet
 Filter) facility, with a focus on the extended BPF version (eBPF).
 
-This kernel side documentation is still work in progress. The main
-textual documentation is (for historical reasons) described in
-:ref:`networking-filter`, which describe both classical and extended
-BPF instruction-set.
+This kernel side documentation is still work in progress.
 The Cilium project also maintains a `BPF and XDP Reference Guide`_
 that goes into great technical depth about the BPF Architecture.
 
 .. toctree::
    :maxdepth: 1
 
+   instruction-set
+   verifier
    libbpf/index
    btf
    faq
@@ -34,4 +33,4 @@ that goes into great technical depth about the BPF Architecture.
    * :ref:`genindex`
 
 .. Links:
-.. _BPF and XDP Reference Guide: https://docs.cilium.io/en/latest/bpf/
\ No newline at end of file
+.. _BPF and XDP Reference Guide: https://docs.cilium.io/en/latest/bpf/
Documentation/bpf/instruction-set.rst new file +467 -0

467 lines not on this page · raw message

Documentation/bpf/verifier.rst new file +529 -0

529 lines not on this page · raw message

Documentation/networking/filter.rst +8 -985

1,015 lines not on this page · raw message

Signature · 1 line
2.30.2
reviewer Acked-by on v1 5/5
#10 lore raw

Re: [PATCH 5/5] bpf, docs: split general purpose eBPF documentation out of filter.rst

Quoted 7 lines · Christoph Hellwig

filter.rst starts out documenting the classic BPF and then spills into introducing and documentating eBPF. Move the eBPF documentation into rwo new files under Documentation/bpf/ for the instruction set and the verifier and link to the BPF documentation from filter.rst.

[...]

#11 lore raw
In reply to Christoph Hellwig
Quoted 11 lines · Christoph Hellwig

Hi all,

for historical reasons filter.rst not only documents the classic Berkely Packet Filter, but also contains some of the most fundamental eBPF documentation. This series moves the actual eBPF documentation into newly created files under Documentation/bpf/ instead. Note that the instruction set document is still a bit of a mess due to all the references to classic BPF, but if this split goes through I plan to start on working to clean that up as well.

Quoted 5 lines

Changes since v1:
- rebased to the latest bpf-next
- just refernence BPF instead of eBPF in most code
- split the patches further up
- better link the BPF documentation from filter.rst

Applied. Thanks