Skip to content

Commit 58fd62e

Browse files
Martin KellyMartin KaFai Lau
authored andcommitted
bpf: Clarify batch lookup/lookup_and_delete semantics
The batch lookup and lookup_and_delete APIs have two parameters, in_batch and out_batch, to facilitate iterative lookup/lookup_and_deletion operations for supported maps. Except NULL for in_batch at the start of these two batch operations, both parameters need to point to memory equal or larger than the respective map key size, except for various hashmaps (hash, percpu_hash, lru_hash, lru_percpu_hash) where the in_batch/out_batch memory size should be at least 4 bytes. Document these semantics to clarify the API. Signed-off-by: Martin Kelly <martin.kelly@crowdstrike.com> Acked-by: Yonghong Song <yonghong.song@linux.dev> Link: https://lore.kernel.org/r/20240221211838.1241578-1-martin.kelly@crowdstrike.com Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
1 parent 89ee838 commit 58fd62e

3 files changed

Lines changed: 22 additions & 7 deletions

File tree

include/uapi/linux/bpf.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,11 @@ union bpf_iter_link_info {
617617
* to NULL to begin the batched operation. After each subsequent
618618
* **BPF_MAP_LOOKUP_BATCH**, the caller should pass the resultant
619619
* *out_batch* as the *in_batch* for the next operation to
620-
* continue iteration from the current point.
620+
* continue iteration from the current point. Both *in_batch* and
621+
* *out_batch* must point to memory large enough to hold a key,
622+
* except for maps of type **BPF_MAP_TYPE_{HASH, PERCPU_HASH,
623+
* LRU_HASH, LRU_PERCPU_HASH}**, for which batch parameters
624+
* must be at least 4 bytes wide regardless of key size.
621625
*
622626
* The *keys* and *values* are output parameters which must point
623627
* to memory large enough to hold *count* items based on the key

tools/include/uapi/linux/bpf.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,11 @@ union bpf_iter_link_info {
617617
* to NULL to begin the batched operation. After each subsequent
618618
* **BPF_MAP_LOOKUP_BATCH**, the caller should pass the resultant
619619
* *out_batch* as the *in_batch* for the next operation to
620-
* continue iteration from the current point.
620+
* continue iteration from the current point. Both *in_batch* and
621+
* *out_batch* must point to memory large enough to hold a key,
622+
* except for maps of type **BPF_MAP_TYPE_{HASH, PERCPU_HASH,
623+
* LRU_HASH, LRU_PERCPU_HASH}**, for which batch parameters
624+
* must be at least 4 bytes wide regardless of key size.
621625
*
622626
* The *keys* and *values* are output parameters which must point
623627
* to memory large enough to hold *count* items based on the key

tools/lib/bpf/bpf.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,14 @@ LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys,
190190
/**
191191
* @brief **bpf_map_lookup_batch()** allows for batch lookup of BPF map elements.
192192
*
193-
* The parameter *in_batch* is the address of the first element in the batch to read.
194-
* *out_batch* is an output parameter that should be passed as *in_batch* to subsequent
195-
* calls to **bpf_map_lookup_batch()**. NULL can be passed for *in_batch* to indicate
196-
* that the batched lookup starts from the beginning of the map.
193+
* The parameter *in_batch* is the address of the first element in the batch to
194+
* read. *out_batch* is an output parameter that should be passed as *in_batch*
195+
* to subsequent calls to **bpf_map_lookup_batch()**. NULL can be passed for
196+
* *in_batch* to indicate that the batched lookup starts from the beginning of
197+
* the map. Both *in_batch* and *out_batch* must point to memory large enough to
198+
* hold a single key, except for maps of type **BPF_MAP_TYPE_{HASH, PERCPU_HASH,
199+
* LRU_HASH, LRU_PERCPU_HASH}**, for which the memory size must be at
200+
* least 4 bytes wide regardless of key size.
197201
*
198202
* The *keys* and *values* are output parameters which must point to memory large enough to
199203
* hold *count* items based on the key and value size of the map *map_fd*. The *keys*
@@ -226,7 +230,10 @@ LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch,
226230
*
227231
* @param fd BPF map file descriptor
228232
* @param in_batch address of the first element in batch to read, can pass NULL to
229-
* get address of the first element in *out_batch*
233+
* get address of the first element in *out_batch*. If not NULL, must be large
234+
* enough to hold a key. For **BPF_MAP_TYPE_{HASH, PERCPU_HASH, LRU_HASH,
235+
* LRU_PERCPU_HASH}**, the memory size must be at least 4 bytes wide regardless
236+
* of key size.
230237
* @param out_batch output parameter that should be passed to next call as *in_batch*
231238
* @param keys pointer to an array of *count* keys
232239
* @param values pointer to an array large enough for *count* values

0 commit comments

Comments
 (0)