RFC uptr KV store
Get series with b4
b4 am [email protected]Versions newest first · linked by author and title
- v2 RFC Latest4 patches 1 replyIn review
- v1 RFC4 patches 3 repliesSuperseded
Changes in v2 from the cover letter
- Miscellaneous bug fixes
- Fix workarounds in bpf APIs
- Reduce 8 x 4K data pages to just one to save lookup time
- Only use dynptr for reading/writing value size other than 1, 4, 8
- Introduce kv_pairs, a list of key-value pairs, for initializing and updating kv_store
- Allocate just enough memory for metadata/data if kv_pairs is provided. Otherwise, create a 16-key, 256-byte value storage kv_store
- Change the second example to show how to manage kv_store from the user space
Patches in v2 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/4 | bpf: Allow creating dynptr from uptrinclude/ kernel/ | +5 | 0 replies | |
| 2/4 | selftests/bpf: Implement basic uptr KV storetools/ tools/ tools/ +1 more | +596 | 0 replies | |
| 3/4 | selftests/bpf: Test basic uptr KV store APIs from user space and bpftools/ tools/ tools/ | +121 | 0 replies | |
| 4/4 | selftests/bpf: Test updating KV store layout when rolling out new progtools/ tools/ tools/ +1 more | +248 | 0 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.
Cover letter [RFC PATCH v2 0/4] uptr KV store
* Overview *
The uptr KV store implements a key-value store based on existing bpf features with one small change to the bpf verifier code. A motivation of this work is to make rolling out a new bpf program with changes to map value layouts easier. Currently, there is not a simple and easy way to do it. One may try to create a new map that has the new map value layout, copy old values into the new one, and then starts the new program. However, this process is not trivial to automate. uptr KV store provides an alternative to this. By replacing a structure in a map with the use of KV store with multiple key-value pairs, changing map layout becomes just adding/deleting key-value pairs. In addition, by maintaining a manifest of key-value pairs, the roll out process can be more easily automated.
* Design & implementation *
- User space and bpf API
The uptr KV store provides basic user space and bpf APIs (get/set/ delete). In addition, there are APIs for managing space that are only provided to the user space. It is assume that all keys are known before deploying a bpf program. To use it, the user space program first needs to initialize the KV store by calling kv_store_init() with a list of key-value pairs, or initializes all key-value pairs using kv_store_set(). Then, the bpf program and user space program can start using the KV store. When rolling out a new bpf program, user space program can update the KV store with the updated list of key-value pairs by calling kv_store_update(). It will delete keys no longer exist, set new keys and update values. The example can be found in patch 4.
- Single global map lookup in KV store bpf API
A use case of the KV store is to store per-task attributes (e.g., an experiment flag, the priority of the task, etc.). While the KV store is not intended to be used as part of the data path, making it performant is a design goal so that it can be use in bpf programs sitting in some relatively hot paths. To achieve this, a key is to keep map lookups as little as possible. The current implementation only requires one task local storage lookup during one program invocation. Then, get/set/delete only involves memory accesses in the uptr regions in the task local storage.
The uptr KV store mainly consists of two uptr regions, metadata and data. The metadata is an array of metadata indexed by key, where each metadata contains the page index and page offset and the size of the key-value pair. The data region are pages allocated on-demand for storing values, and one page is allocated initially.
If using string key is desired, the metadata can be moved to a bpf hashmap indexed with string keys. However, this will add one hashmap lookup to every basic KV store operation.
- 1K max int keys; 1B - 4KB value
The KV store is indexed by integer keys and the maximum number of keys supported is 1K. This is limited by the maximum number of metadata as shown below that can be stored in a 4KB uptr metadata array. The largest size of a value is also bound to 4KB for the same reason.
struct kv_store_meta {
__u32 page_off:12;
__u32 size:12;
__u32 init:1;
}- Growable storage backed by uptr
To be able to accommodate future storage space needed for new key-value pairs or temporarily storing old and new key-value pairs at the same time during transition, the KV store is growable. This is possible as the KV store leverages uptr, which can be allocated in user space on demand.
- Variable-size data copy with dynptr
get/set involves copying variable-size data between uptr region and stack. Since llvm does not support emitting bytecode for memcpy with variable size, byte-by-byte copy in a for loop needs to be used. This can be improved by using dynptr. Please refer to the patch 1 for details.
Changelog:
v1 -> v2
- Miscellaneous bug fixes
- Fix workarounds in bpf APIs
- Reduce 8 x 4K data pages to just one to save lookup time
- Only use dynptr for reading/writing value size other than 1, 4, 8
- Introduce kv_pairs, a list of key-value pairs, for initializing and updating kv_store
- Allocate just enough memory for metadata/data if kv_pairs is provided. Otherwise, create a 16-key, 256-byte value storage kv_store
- Change the second example to show how to manage kv_store from the user space
Amery Hung (4):
bpf: Allow creating dynptr from uptr
selftests/bpf: Implement basic uptr KV store selftests/bpf: Test basic uptr KV store APIs from user space and bpf selftests/bpf: Test updating KV store layout when rolling out new prog
include/uapi/linux/bpf.h | 4 +- kernel/bpf/verifier.c | 3 +- .../bpf/prog_tests/test_uptr_kv_store.c | 259 ++++++++++++++ .../selftests/bpf/prog_tests/uptr_kv_store.c | 329 ++++++++++++++++++ .../selftests/bpf/prog_tests/uptr_kv_store.h | 103 ++++++ .../selftests/bpf/progs/test_uptr_kv_store.c | 44 +++ .../bpf/progs/test_uptr_kv_store_v1.c | 44 +++ .../selftests/bpf/progs/uptr_kv_store.h | 127 +++++++ .../selftests/bpf/test_uptr_kv_store_common.h | 22 ++ .../selftests/bpf/uptr_kv_store_common.h | 37 ++ 10 files changed, 970 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_uptr_kv_store.c create mode 100644 tools/testing/selftests/bpf/prog_tests/uptr_kv_store.c create mode 100644 tools/testing/selftests/bpf/prog_tests/uptr_kv_store.h create mode 100644 tools/testing/selftests/bpf/progs/test_uptr_kv_store.c create mode 100644 tools/testing/selftests/bpf/progs/test_uptr_kv_store_v1.c create mode 100644 tools/testing/selftests/bpf/progs/uptr_kv_store.h create mode 100644 tools/testing/selftests/bpf/test_uptr_kv_store_common.h create mode 100644 tools/testing/selftests/bpf/uptr_kv_store_common.h
Signature · 1 line
2.47.1