mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-10-31 16:38:12 +00:00
d3bec0138b
Zero-fill element values for all other cpus than current, just as
when not using prealloc. This is the only way the bpf program can
ensure known initial values for all cpus ('onallcpus' cannot be
set when coming from the bpf program).
The scenario is: bpf program inserts some elements in a per-cpu
map, then deletes some (or userspace does). When later adding
new elements using bpf_map_update_elem(), the bpf program can
only set the value of the new elements for the current cpu.
When prealloc is enabled, previously deleted elements are re-used.
Without the fix, values for other cpus remain whatever they were
when the re-used entry was previously freed.
A selftest is added to validate correct operation in above
scenario as well as in case of LRU per-cpu map element re-use.
Fixes: 6c90598174
("bpf: pre-allocate hash map elements")
Signed-off-by: David Verbeiren <david.verbeiren@tessares.net>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Matthieu Baerts <matthieu.baerts@tessares.net>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20201104112332.15191-1-david.verbeiren@tessares.net
33 lines
752 B
C
33 lines
752 B
C
// SPDX-License-Identifier: GPL-2.0
|
|
/* Copyright (c) 2020 Tessares SA <http://www.tessares.net> */
|
|
|
|
#include "vmlinux.h"
|
|
#include <bpf/bpf_helpers.h>
|
|
|
|
__u64 inKey = 0;
|
|
__u64 inValue = 0;
|
|
__u32 inPid = 0;
|
|
|
|
struct {
|
|
__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
|
|
__uint(max_entries, 2);
|
|
__type(key, __u64);
|
|
__type(value, __u64);
|
|
} hashmap1 SEC(".maps");
|
|
|
|
|
|
SEC("tp/syscalls/sys_enter_getpgid")
|
|
int sysenter_getpgid(const void *ctx)
|
|
{
|
|
/* Just do it for once, when called from our own test prog. This
|
|
* ensures the map value is only updated for a single CPU.
|
|
*/
|
|
int cur_pid = bpf_get_current_pid_tgid() >> 32;
|
|
|
|
if (cur_pid == inPid)
|
|
bpf_map_update_elem(&hashmap1, &inKey, &inValue, BPF_NOEXIST);
|
|
|
|
return 0;
|
|
}
|
|
|
|
char _license[] SEC("license") = "GPL";
|