summaryrefslogtreecommitdiffstats
path: root/kernel/bpf/helpers.c
diff options
context:
space:
mode:
authorAlexei Starovoitov <ast@kernel.org>2019-01-31 18:40:09 -0500
committerDaniel Borkmann <daniel@iogearbox.net>2019-02-01 14:55:39 -0500
commit96049f3afd50fe8db69fa0068cdca822e747b1e4 (patch)
treeb082ac077ea0bb78a073a25e540be72034ce0451 /kernel/bpf/helpers.c
parentab963beb9f5db303b4fd7e34e422b96270e5b972 (diff)
bpf: introduce BPF_F_LOCK flag
Introduce BPF_F_LOCK flag for map_lookup and map_update syscall commands and for map_update() helper function. In all these cases take a lock of existing element (which was provided in BTF description) before copying (in or out) the rest of map value. Implementation details that are part of uapi: Array: The array map takes the element lock for lookup/update. Hash: hash map also takes the lock for lookup/update and tries to avoid the bucket lock. If old element exists it takes the element lock and updates the element in place. If element doesn't exist it allocates new one and inserts into hash table while holding the bucket lock. In rare case the hashmap has to take both the bucket lock and the element lock to update old value in place. Cgroup local storage: It is similar to array. update in place and lookup are done with lock taken. Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'kernel/bpf/helpers.c')
-rw-r--r--kernel/bpf/helpers.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index fbe544761628..a411fc17d265 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -301,6 +301,22 @@ const struct bpf_func_proto bpf_spin_unlock_proto = {
301 .arg1_type = ARG_PTR_TO_SPIN_LOCK, 301 .arg1_type = ARG_PTR_TO_SPIN_LOCK,
302}; 302};
303 303
304void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
305 bool lock_src)
306{
307 struct bpf_spin_lock *lock;
308
309 if (lock_src)
310 lock = src + map->spin_lock_off;
311 else
312 lock = dst + map->spin_lock_off;
313 preempt_disable();
314 ____bpf_spin_lock(lock);
315 copy_map_value(map, dst, src);
316 ____bpf_spin_unlock(lock);
317 preempt_enable();
318}
319
304#ifdef CONFIG_CGROUPS 320#ifdef CONFIG_CGROUPS
305BPF_CALL_0(bpf_get_current_cgroup_id) 321BPF_CALL_0(bpf_get_current_cgroup_id)
306{ 322{