Skip to contentSkip to conversation

docs/bpf: Add table to describe LRU properties

PATCH Applied Joe Stringer 3 participants 5 messages Apr 22, 2023 – Apr 27, 2023

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

PATCH v5 1/2 docs/bpf: Add table to describe LRU properties

Depending on the map type and flags for LRU, different properties are global or percpu. Add a table to describe these.

---
v5: Use bold rather than verbatim for column header
v4: Initial posting

---
 Documentation/bpf/map_hash.rst | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
Documentation/bpf/map_hash.rst +10 -1
index 8669426264c6..1314dfc5e7e1 100644
@@ -29,7 +29,16 @@ will automatically evict the least recently used entries when the hash
 table reaches capacity. An LRU hash maintains an internal LRU list that
 is used to select elements for eviction. This internal LRU list is
 shared across CPUs but it is possible to request a per CPU LRU list with
-the ``BPF_F_NO_COMMON_LRU`` flag when calling ``bpf_map_create``.
+the ``BPF_F_NO_COMMON_LRU`` flag when calling ``bpf_map_create``.  The
+following table outlines the properties of LRU maps depending on the a
+map type and the flags used to create the map.
+
+======================== ========================= ================================
+Flag                     ``BPF_MAP_TYPE_LRU_HASH`` ``BPF_MAP_TYPE_LRU_PERCPU_HASH``
+======================== ========================= ================================
+**BPF_F_NO_COMMON_LRU**  Per-CPU LRU, global map   Per-CPU LRU, per-cpu map
+**!BPF_F_NO_COMMON_LRU** Global LRU, global map    Global LRU, per-cpu map
+======================== ========================= ================================
 
 Usage
 =====
Signature · 1 line
2.34.1
#1 lore raw

PATCH v5 2/2 docs/bpf: Add LRU internals description and graph

Extend the bpf hashmap docs to include a brief description of the internals of the LRU map type (setting appropriate API expectations), including the original commit message from Martin and a variant on the graph that I had presented during my Linux Plumbers Conference 2022 talk on "Pressure feedback for LRU map types"[0].

The node names in the dot file correspond roughly to the functions where the logic for those decisions or steps is defined, to help curious developers to cross-reference and update this logic if the details of the LRU implementation ever differ from this description.

[0]: https://lpc.events/event/16/contributions/1368/

---
v5: Fix warning for use of 'label' attribute
v4: Move UAPI descriptions outside of the internals section
Fix function reference discrepancies in dot source Fix incorrect flag references (missing F_)
Simplify logic at bottom of graph for map updates Add missing return codes to graph for failure cases
v3: Use standard table syntax
Replace inline commit message with reference to commit Fix incorrect Y/N label for common LRU check
Rename some dotfile variables to reduce confusion between cases Minor wording touchups
v2: Fix issue that caused initial email submission to fail

---
 Documentation/bpf/map_hash.rst            |  42 ++++++
 Documentation/bpf/map_lru_hash_update.dot | 172 ++++++++++++++++++++++
 2 files changed, 214 insertions(+)
 create mode 100644 Documentation/bpf/map_lru_hash_update.dot
Documentation/bpf/map_hash.rst +42 -0
index 1314dfc5e7e1..d2343952f2cb 100644
@@ -1,5 +1,6 @@
 .. SPDX-License-Identifier: GPL-2.0-only
 .. Copyright (C) 2022 Red Hat, Inc.
+.. Copyright (C) 2022-2023 Isovalent, Inc.
 
 ===============================================
 BPF_MAP_TYPE_HASH, with PERCPU and LRU Variants
@@ -215,3 +216,44 @@ Userspace walking the map elements from the map declared above:
                     cur_key = &next_key;
             }
     }
+
+Internals
+=========
+
+This section of the document is targeted at Linux developers and describes
+aspects of the map implementations that are not considered stable ABI. The
+following details are subject to change in future versions of the kernel.
+
+``BPF_MAP_TYPE_LRU_HASH`` and variants
+--------------------------------------
+
+Updating elements in LRU maps may trigger eviction behaviour when the capacity
+of the map is reached. There are various steps that the update algorithm
+attempts in order to enforce the LRU property which have increasing impacts on
+other CPUs involved in the following operation attempts:
+
+- Attempt to use CPU-local state to batch operations
+- Attempt to fetch free nodes from global lists
+- Attempt to pull any node from a global list and remove it from the hashmap
+- Attempt to pull any node from any CPU's list and remove it from the hashmap
+
+This algorithm is described visually in the following diagram. See the
+description in commit 3a08c2fd7634 ("bpf: LRU List") for a full explanation of
+the corresponding operations:
+
+.. kernel-figure::  map_lru_hash_update.dot
+   :alt:    Diagram outlining the LRU eviction steps taken during map update.
+
+   LRU hash eviction during map update for ``BPF_MAP_TYPE_LRU_HASH`` and
+   variants. See the dot file source for kernel function name code references.
+
+Map updates start from the oval in the top right "begin ``bpf_map_update()``"
+and progress through the graph towards the bottom where the result may be
+either a successful update or a failure with various error codes. The key in
+the top right provides indicators for which locks may be involved in specific
+operations. This is intended as a visual hint for reasoning about how map
+contention may impact update operations, though the map type and flags may
+impact the actual contention on those locks, based on the logic described in
+the table above. For instance, if the map is created with type
+``BPF_MAP_TYPE_LRU_PERCPU_HASH`` and flags ``BPF_F_NO_COMMON_LRU`` then all map
+properties would be per-cpu.
Documentation/bpf/map_lru_hash_update.dot new file +172 -0

172 lines not on this page · raw message

Signature · 1 line
2.34.1
Acked-by on v5 2/2
#2 lore raw

RE: [PATCH bpf-next v5 2/2] docs/bpf: Add LRU internals description and graph

Quoted 11 lines · Joe Stringer

Extend the bpf hashmap docs to include a brief description of the internals of the LRU map type (setting appropriate API expectations), including the original commit message from Martin and a variant on the graph that I had presented during my Linux Plumbers Conference 2022 talk on "Pressure feedback for LRU map types"[0].

The node names in the dot file correspond roughly to the functions where the logic for those decisions or steps is defined, to help curious developers to cross-reference and update this logic if the details of the LRU implementation ever differ from this description.

Quoted 5 lines

[0]: https://lpc.events/event/16/contributions/1368/

---

Acked-by on v5 1/2
#3 lore raw
In reply to Joe Stringer
Quoted 36 lines · Joe Stringer

Depending on the map type and flags for LRU, different properties are global or percpu. Add a table to describe these.

---
v5: Use bold rather than verbatim for column header
v4: Initial posting

---
 Documentation/bpf/map_hash.rst | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
Documentation/bpf/map_hash.rst +10 -1
index 8669426264c6..1314dfc5e7e1 100644
@@ -29,7 +29,16 @@ will automatically evict the least recently used entries when the hash
 table reaches capacity. An LRU hash maintains an internal LRU list that
 is used to select elements for eviction. This internal LRU list is
 shared across CPUs but it is possible to request a per CPU LRU list with
-the ``BPF_F_NO_COMMON_LRU`` flag when calling ``bpf_map_create``.
+the ``BPF_F_NO_COMMON_LRU`` flag when calling ``bpf_map_create``.  The
+following table outlines the properties of LRU maps depending on the a
+map type and the flags used to create the map.
+
+======================== ========================= ================================
+Flag                     ``BPF_MAP_TYPE_LRU_HASH`` ``BPF_MAP_TYPE_LRU_PERCPU_HASH``
+======================== ========================= ================================
+**BPF_F_NO_COMMON_LRU**  Per-CPU LRU, global map   Per-CPU LRU, per-cpu map
+**!BPF_F_NO_COMMON_LRU** Global LRU, global map    Global LRU, per-cpu map
+======================== ========================= ================================
 
 Usage
 =====
Signature · 1 line
2.34.1
#4 lore raw
In reply to Joe Stringer
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 9 lines · On Sat, 22 Apr 2023 10:20:53 -0700 you wrote:

Depending on the map type and flags for LRU, different properties are global or percpu. Add a table to describe these.

---
v5: Use bold rather than verbatim for column header
v4: Initial posting

[...]

Here is the summary with links:
- [bpf-next,v5,1/2] docs/bpf: Add table to describe LRU properties

    https://git.kernel.org/bpf/bpf-next/c/af0335d2926e
  - [bpf-next,v5,2/2] docs/bpf: Add LRU internals description and graph
    https://git.kernel.org/bpf/bpf-next/c/1a986518b8a5

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html