Skip to content

Commit 4ed8ec5

Browse files
iamkafaidavem330
authored andcommitted
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 <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Daniel Borkmann <[email protected]> Cc: Tejun Heo <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1f3fe7e commit 4ed8ec5

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

include/uapi/linux/bpf.h

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ enum bpf_map_type {
8484
BPF_MAP_TYPE_PERCPU_HASH,
8585
BPF_MAP_TYPE_PERCPU_ARRAY,
8686
BPF_MAP_TYPE_STACK_TRACE,
87+
BPF_MAP_TYPE_CGROUP_ARRAY,
8788
};
8889

8990
enum bpf_prog_type {

kernel/bpf/arraymap.c

+43
Original file line numberDiff line numberDiff line change
@@ -537,3 +537,46 @@ static int __init register_perf_event_array_map(void)
537537
return 0;
538538
}
539539
late_initcall(register_perf_event_array_map);
540+
541+
#ifdef CONFIG_SOCK_CGROUP_DATA
542+
static 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+
549+
static 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+
555+
static 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+
561+
static 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+
571+
static struct bpf_map_type_list cgroup_array_type __read_mostly = {
572+
.ops = &cgroup_array_ops,
573+
.type = BPF_MAP_TYPE_CGROUP_ARRAY,
574+
};
575+
576+
static int __init register_cgroup_array_map(void)
577+
{
578+
bpf_register_map_type(&cgroup_array_type);
579+
return 0;
580+
}
581+
late_initcall(register_cgroup_array_map);
582+
#endif

kernel/bpf/syscall.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ static int map_update_elem(union bpf_attr *attr)
393393
} else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
394394
err = bpf_percpu_array_update(map, key, value, attr->flags);
395395
} 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) {
397398
rcu_read_lock();
398399
err = bpf_fd_array_map_update_elem(map, f.file, key, value,
399400
attr->flags);

kernel/bpf/verifier.c

+2
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,8 @@ static int check_map_func_compatibility(struct bpf_map *map, int func_id)
10351035
if (func_id != BPF_FUNC_get_stackid)
10361036
goto error;
10371037
break;
1038+
case BPF_MAP_TYPE_CGROUP_ARRAY:
1039+
goto error;
10381040
default:
10391041
break;
10401042
}

0 commit comments

Comments
 (0)