Skip to content

Commit c48c3a7

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf: Drop redundant hash_buf from map_get_hash operation
bpf_map_get_info_by_fd() is the only caller of the ->map_get_hash and always invokes it with hash_buf == map->sha and hash_buf_size of SHA256_DIGEST_SIZE. array_map_get_hash() in turn lets sha256() write the digest directly into that buffer (map->sha) and then performs a trailing memcpy(), which evaluates to memcpy(map->sha, map->sha, 32): a redundant self-copy. The hash_buf_size argument was never used at all. Simplify this a bit, no functional change. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/r/20260601150248.394863-3-daniel@iogearbox.net Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 9a3c3c4 commit c48c3a7

3 files changed

Lines changed: 6 additions & 10 deletions

File tree

include/linux/bpf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ struct bpf_map_ops {
111111
long (*map_pop_elem)(struct bpf_map *map, void *value);
112112
long (*map_peek_elem)(struct bpf_map *map, void *value);
113113
void *(*map_lookup_percpu_elem)(struct bpf_map *map, void *key, u32 cpu);
114-
int (*map_get_hash)(struct bpf_map *map, u32 hash_buf_size, void *hash_buf);
114+
int (*map_get_hash)(struct bpf_map *map);
115115

116116
/* funcs called by prog_array and perf_event_array map */
117117
void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,

kernel/bpf/arraymap.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,12 @@ static void *array_map_lookup_elem(struct bpf_map *map, void *key)
175175
return array->value + (u64)array->elem_size * (index & array->index_mask);
176176
}
177177

178-
static int array_map_get_hash(struct bpf_map *map, u32 hash_buf_size,
179-
void *hash_buf)
178+
static int array_map_get_hash(struct bpf_map *map)
180179
{
181180
struct bpf_array *array = container_of(map, struct bpf_array, map);
182181

183182
sha256(array->value, (u64)array->elem_size * array->map.max_entries,
184-
hash_buf);
185-
memcpy(array->map.sha, hash_buf, sizeof(array->map.sha));
183+
array->map.sha);
186184
return 0;
187185
}
188186

kernel/bpf/syscall.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5434,18 +5434,16 @@ static int bpf_map_get_info_by_fd(struct file *file,
54345434

54355435
if (!map->ops->map_get_hash)
54365436
return -EINVAL;
5437-
5438-
if (info.hash_size != SHA256_DIGEST_SIZE)
5437+
if (info.hash_size != sizeof(map->sha))
54395438
return -EINVAL;
5440-
54415439
if (!READ_ONCE(map->frozen))
54425440
return -EPERM;
54435441

5444-
err = map->ops->map_get_hash(map, SHA256_DIGEST_SIZE, map->sha);
5442+
err = map->ops->map_get_hash(map);
54455443
if (err != 0)
54465444
return err;
54475445

5448-
if (copy_to_user(uhash, map->sha, SHA256_DIGEST_SIZE) != 0)
5446+
if (copy_to_user(uhash, map->sha, sizeof(map->sha)) != 0)
54495447
return -EFAULT;
54505448
} else if (info.hash_size) {
54515449
return -EINVAL;

0 commit comments

Comments
 (0)