Skip to content

Commit d0003ec

Browse files
Alexei Starovoitovdavem330
Alexei Starovoitov
authored andcommitted
bpf: allow eBPF programs to use maps
expose bpf_map_lookup_elem(), bpf_map_update_elem(), bpf_map_delete_elem() map accessors to eBPF programs Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent ffb65f2 commit d0003ec

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

include/linux/bpf.h

+5
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,9 @@ struct bpf_prog *bpf_prog_get(u32 ufd);
133133
/* verify correctness of eBPF program */
134134
int bpf_check(struct bpf_prog *fp, union bpf_attr *attr);
135135

136+
/* verifier prototypes for helper functions called from eBPF programs */
137+
extern struct bpf_func_proto bpf_map_lookup_elem_proto;
138+
extern struct bpf_func_proto bpf_map_update_elem_proto;
139+
extern struct bpf_func_proto bpf_map_delete_elem_proto;
140+
136141
#endif /* _LINUX_BPF_H */

include/uapi/linux/bpf.h

+3
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ union bpf_attr {
158158
*/
159159
enum bpf_func_id {
160160
BPF_FUNC_unspec,
161+
BPF_FUNC_map_lookup_elem, /* void *map_lookup_elem(&map, &key) */
162+
BPF_FUNC_map_update_elem, /* int map_update_elem(&map, &key, &value, flags) */
163+
BPF_FUNC_map_delete_elem, /* int map_delete_elem(&map, &key) */
161164
__BPF_FUNC_MAX_ID,
162165
};
163166

kernel/bpf/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
obj-y := core.o
2-
obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o hashtab.o arraymap.o
2+
obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o hashtab.o arraymap.o helpers.o
33
ifdef CONFIG_TEST_BPF
44
obj-$(CONFIG_BPF_SYSCALL) += test_stub.o
55
endif

kernel/bpf/helpers.c

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2+
*
3+
* This program is free software; you can redistribute it and/or
4+
* modify it under the terms of version 2 of the GNU General Public
5+
* License as published by the Free Software Foundation.
6+
*
7+
* This program is distributed in the hope that it will be useful, but
8+
* WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10+
* General Public License for more details.
11+
*/
12+
#include <linux/bpf.h>
13+
#include <linux/rcupdate.h>
14+
15+
/* If kernel subsystem is allowing eBPF programs to call this function,
16+
* inside its own verifier_ops->get_func_proto() callback it should return
17+
* bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
18+
*
19+
* Different map implementations will rely on rcu in map methods
20+
* lookup/update/delete, therefore eBPF programs must run under rcu lock
21+
* if program is allowed to access maps, so check rcu_read_lock_held in
22+
* all three functions.
23+
*/
24+
static u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
25+
{
26+
/* verifier checked that R1 contains a valid pointer to bpf_map
27+
* and R2 points to a program stack and map->key_size bytes were
28+
* initialized
29+
*/
30+
struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
31+
void *key = (void *) (unsigned long) r2;
32+
void *value;
33+
34+
WARN_ON_ONCE(!rcu_read_lock_held());
35+
36+
value = map->ops->map_lookup_elem(map, key);
37+
38+
/* lookup() returns either pointer to element value or NULL
39+
* which is the meaning of PTR_TO_MAP_VALUE_OR_NULL type
40+
*/
41+
return (unsigned long) value;
42+
}
43+
44+
struct bpf_func_proto bpf_map_lookup_elem_proto = {
45+
.func = bpf_map_lookup_elem,
46+
.gpl_only = false,
47+
.ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
48+
.arg1_type = ARG_CONST_MAP_PTR,
49+
.arg2_type = ARG_PTR_TO_MAP_KEY,
50+
};
51+
52+
static u64 bpf_map_update_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
53+
{
54+
struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
55+
void *key = (void *) (unsigned long) r2;
56+
void *value = (void *) (unsigned long) r3;
57+
58+
WARN_ON_ONCE(!rcu_read_lock_held());
59+
60+
return map->ops->map_update_elem(map, key, value, r4);
61+
}
62+
63+
struct bpf_func_proto bpf_map_update_elem_proto = {
64+
.func = bpf_map_update_elem,
65+
.gpl_only = false,
66+
.ret_type = RET_INTEGER,
67+
.arg1_type = ARG_CONST_MAP_PTR,
68+
.arg2_type = ARG_PTR_TO_MAP_KEY,
69+
.arg3_type = ARG_PTR_TO_MAP_VALUE,
70+
.arg4_type = ARG_ANYTHING,
71+
};
72+
73+
static u64 bpf_map_delete_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
74+
{
75+
struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
76+
void *key = (void *) (unsigned long) r2;
77+
78+
WARN_ON_ONCE(!rcu_read_lock_held());
79+
80+
return map->ops->map_delete_elem(map, key);
81+
}
82+
83+
struct bpf_func_proto bpf_map_delete_elem_proto = {
84+
.func = bpf_map_delete_elem,
85+
.gpl_only = false,
86+
.ret_type = RET_INTEGER,
87+
.arg1_type = ARG_CONST_MAP_PTR,
88+
.arg2_type = ARG_PTR_TO_MAP_KEY,
89+
};

0 commit comments

Comments
 (0)