bpf: htab: Reduce memory use of hash maps - #13245
bpf: htab: Reduce memory use of hash maps#13245kernel-patches-daemon-bpf[bot] wants to merge 2 commits into
Conversation
|
Upstream branch: 3a59f11 |
239ce7e to
f9ee2fe
Compare
|
Upstream branch: 3a59f11 |
5ac3acc to
2c8b9de
Compare
AI reviewed your patch. Please fix the bug or email reply why it's not a bug. In-Reply-To-Subject: |
AI reviewed your patch. Please fix the bug or email reply why it's not a bug. In-Reply-To-Subject: |
|
Forwarding comment 5274302490 via email |
|
Forwarding comment 5274306118 via email |
f9ee2fe to
7bfbd95
Compare
|
Upstream branch: 108d440 |
2c8b9de to
6ad5228
Compare
7bfbd95 to
f3e34e7
Compare
|
Upstream branch: a88dbe1 |
6ad5228 to
3c7f0ca
Compare
f3e34e7 to
5182577
Compare
|
Upstream branch: 7c3e54c |
3c7f0ca to
8062599
Compare
5182577 to
673b1f0
Compare
The htab_elem struct is used as the per-element type for all BPF hash
map types and includes bpf_lru_node in a union with a ptr_to_pptr
pointer. For standard (non-LRU, non-PCPU) hash maps, the 24 byte union
allocated for every element is entirely unused. For non-preallocated
PCPU maps, ptr_to_pptr only requires 8 bytes, leaving 16 bytes of unused
overhead in the union. For preallocated PCPU maps ptr_to_pptr is unused
since elements are freed to the PCPU freelist.
Eliminate this per-element memory overhead by splitting htab_elem into
dedicated structures for each map type:
- struct htab_elem: Minimal structure for standard hash maps and
preallocated PCPU maps (saves 24 bytes per element).
- struct htab_elem_pcpu: Structure for non-preallocated PCPU maps
containing ptr_to_pptr (saves 16 bytes per element).
- struct htab_elem_lru: Retains struct bpf_lru_node for LRU maps.
Because element sizes now vary by map type, add key_offset to struct
bpf_htab to track the dynamic key offset. Update helper accessors and
lookups to compute key and value offsets using htab->key_offset.
Pointers to struct htab_elem in the existing code (e.g. htab_elem_hash)
serve as generic base element pointers. This is possible because
htab_elem, htab_elem_pcpu, and htab_elem_lru share a common initial
sequence, making pointer casts safe.
Signed-off-by: T.J. Mercier <tjmercier@google.com>
For standard and PCPU (non-LRU) hash maps with small key sizes (less than or equal to the word size), comparing keys requires only a single instruction. Storing a cached 32-bit hash value to shortcut full key comparisons provides no performance advantage for small keys, and consumes memory for every element. This memory can be saved by eliminating hash along with its associated 4 byte padding before the key, reducing the elem_size (and key_offset) by 8 bytes for standard and PCPU maps. Introduce htab_has_hash() to check whether a map requires a cached hash field. Update htab_elem_set_hash(), lookup_elem_raw(), and lookup_nulls_elem_raw() to conditionally bypass hash checking and storage when htab_has_hash() is false. Together with the previous patch, this reduces the minimum standard and preallocated hash map element size from 64 bytes down to 32 bytes, and non-preallocated per-CPU element size from 64 bytes down to 40 bytes. Signed-off-by: T.J. Mercier <tjmercier@google.com>
|
Upstream branch: 6f03361 |
8062599 to
6908e48
Compare
Pull request for series with
subject: bpf: htab: Reduce memory use of hash maps
version: 4
url: https://patchwork.kernel.org/project/netdevbpf/list/?series=1145038