aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/bpf
diff options
context:
space:
mode:
authorMartin KaFai Lau <kafai@fb.com>2016-06-30 13:28:43 -0400
committerDavid S. Miller <davem@davemloft.net>2016-07-01 16:30:38 -0400
commit4ed8ec521ed57c4e207ad464ca0388776de74d4b (patch)
treef656cf08ee272920b26cad8a91ae8d315242b5cd /kernel/bpf
parent1f3fe7ebf6136c341012db9f554d4caa566fcbaa (diff)
cgroup: bpf: Add BPF_MAP_TYPE_CGROUP_ARRAY
Add a BPF_MAP_TYPE_CGROUP_ARRAY and its bpf_map_ops's implementations. To update an element, the caller is expected to obtain a cgroup2 backed fd by open(cgroup2_dir) and then update the array with that fd. Signed-off-by: Martin KaFai Lau <kafai@fb.com> Cc: Alexei Starovoitov <ast@fb.com> Cc: Daniel Borkmann <daniel@iogearbox.net> Cc: Tejun Heo <tj@kernel.org> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'kernel/bpf')
-rw-r--r--kernel/bpf/arraymap.c43
-rw-r--r--kernel/bpf/syscall.c3
-rw-r--r--kernel/bpf/verifier.c2
3 files changed, 47 insertions, 1 deletions
diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c
index 4ec57a649b1f..db1a743e3db2 100644
--- a/kernel/bpf/arraymap.c
+++ b/kernel/bpf/arraymap.c
@@ -537,3 +537,46 @@ static int __init register_perf_event_array_map(void)
537 return 0; 537 return 0;
538} 538}
539late_initcall(register_perf_event_array_map); 539late_initcall(register_perf_event_array_map);
540
541#ifdef CONFIG_SOCK_CGROUP_DATA
542static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
543 struct file *map_file /* not used */,
544 int fd)
545{
546 return cgroup_get_from_fd(fd);
547}
548
549static void cgroup_fd_array_put_ptr(void *ptr)
550{
551 /* cgroup_put free cgrp after a rcu grace period */
552 cgroup_put(ptr);
553}
554
555static void cgroup_fd_array_free(struct bpf_map *map)
556{
557 bpf_fd_array_map_clear(map);
558 fd_array_map_free(map);
559}
560
561static const struct bpf_map_ops cgroup_array_ops = {
562 .map_alloc = fd_array_map_alloc,
563 .map_free = cgroup_fd_array_free,
564 .map_get_next_key = array_map_get_next_key,
565 .map_lookup_elem = fd_array_map_lookup_elem,
566 .map_delete_elem = fd_array_map_delete_elem,
567 .map_fd_get_ptr = cgroup_fd_array_get_ptr,
568 .map_fd_put_ptr = cgroup_fd_array_put_ptr,
569};
570
571static struct bpf_map_type_list cgroup_array_type __read_mostly = {
572 .ops = &cgroup_array_ops,
573 .type = BPF_MAP_TYPE_CGROUP_ARRAY,
574};
575
576static int __init register_cgroup_array_map(void)
577{
578 bpf_register_map_type(&cgroup_array_type);
579 return 0;
580}
581late_initcall(register_cgroup_array_map);
582#endif
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 22863d9872b1..96d938a22050 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -393,7 +393,8 @@ static int map_update_elem(union bpf_attr *attr)
393 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { 393 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
394 err = bpf_percpu_array_update(map, key, value, attr->flags); 394 err = bpf_percpu_array_update(map, key, value, attr->flags);
395 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || 395 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
396 map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { 396 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
397 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY) {
397 rcu_read_lock(); 398 rcu_read_lock();
398 err = bpf_fd_array_map_update_elem(map, f.file, key, value, 399 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
399 attr->flags); 400 attr->flags);
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index eec9f90ba030..69ba2251a22b 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -1035,6 +1035,8 @@ static int check_map_func_compatibility(struct bpf_map *map, int func_id)
1035 if (func_id != BPF_FUNC_get_stackid) 1035 if (func_id != BPF_FUNC_get_stackid)
1036 goto error; 1036 goto error;
1037 break; 1037 break;
1038 case BPF_MAP_TYPE_CGROUP_ARRAY:
1039 goto error;
1038 default: 1040 default:
1039 break; 1041 break;
1040 } 1042 }