Skip to content

Commit ffa915f

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf_metadata'
Stanislav Fomichev says: ==================== Currently, if a user wants to store arbitrary metadata for an eBPF program, for example, the program build commit hash or version, they could store it in a map, and conveniently libbpf uses .data section to populate an internal map. However, if the program does not actually reference the map, then the map would be de-refcounted and freed. This patch set introduces a new syscall BPF_PROG_BIND_MAP to add a map to a program's used_maps, even if the program instructions does not reference the map. libbpf is extended to always BPF_PROG_BIND_MAP .rodata section so the metadata is kept in place. bpftool is also extended to print metadata in the 'bpftool prog' list. The variable is considered metadata if it starts with the magic 'bpf_metadata_' prefix; everything after the prefix is the metadata name. An example use of this would be BPF C file declaring: volatile const char bpf_metadata_commit_hash[] SEC(".rodata") = "abcdef123456"; and bpftool would emit: $ bpftool prog [...] metadata: commit_hash = "abcdef123456" v6 changes: * libbpf: drop FEAT_GLOBAL_DATA from probe_prog_bind_map (Andrii Nakryiko) * bpftool: combine find_metadata_map_id & find_metadata; drops extra bpf_map_get_fd_by_id and bpf_map_get_fd_by_id (Andrii Nakryiko) * bpftool: use strncmp instead of strstr (Andrii Nakryiko) * bpftool: memset(map_info) and extra empty line (Andrii Nakryiko) v5 changes: * selftest: verify that prog holds rodata (Andrii Nakryiko) * selftest: use volatile for metadata (Andrii Nakryiko) * bpftool: use sizeof in BPF_METADATA_PREFIX_LEN (Andrii Nakryiko) * bpftool: new find_metadata that does map lookup (Andrii Nakryiko) * libbpf: don't generalize probe_create_global_data (Andrii Nakryiko) * libbpf: use OPTS_VALID in bpf_prog_bind_map (Andrii Nakryiko) * libbpf: keep LIBBPF_0.2.0 sorted (Andrii Nakryiko) v4 changes: * Don't return EEXIST from syscall if already bound (Andrii Nakryiko) * Removed --metadata argument (Andrii Nakryiko) * Removed custom .metadata section (Alexei Starovoitov) * Addressed Andrii's suggestions about btf helpers and vsi (Andrii Nakryiko) * Moved bpf_prog_find_metadata into bpftool (Alexei Starovoitov) v3 changes: * API changes for bpf_prog_find_metadata (Toke Høiland-Jørgensen) v2 changes: * Made struct bpf_prog_bind_opts in libbpf so flags is optional. * Deduped probe_kern_global_data and probe_prog_bind_map into a common helper. * Added comment regarding why EEXIST is ignored in libbpf bind map. * Froze all LIBBPF_MAP_METADATA internal maps. * Moved bpf_prog_bind_map into new LIBBPF_0.1.1 in libbpf.map. * Added p_err() calls on error cases in bpftool show_prog_metadata. * Reverse christmas tree coding style in bpftool show_prog_metadata. * Made bpftool gen skeleton recognize .metadata as an internal map and generate datasec definition in skeleton. * Added C test using skeleton to see asset that the metadata is what we expect and rebinding causes EEXIST. v1 changes: * Fixed a few missing unlocks, and missing close while iterating map fds. * Move mutex initialization to right after prog aux allocation, and mutex destroy to right after prog aux free. * s/ADD_MAP/BIND_MAP/ * Use mutex only instead of RCU to protect the used_map array & count. Cc: YiFei Zhu <[email protected]> ==================== Acked-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents d317b0a + d42d1cc commit ffa915f

File tree

19 files changed

+678
-18
lines changed

19 files changed

+678
-18
lines changed

drivers/net/ethernet/netronome/nfp/bpf/offload.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,34 +111,40 @@ static int
111111
nfp_map_ptrs_record(struct nfp_app_bpf *bpf, struct nfp_prog *nfp_prog,
112112
struct bpf_prog *prog)
113113
{
114-
int i, cnt, err;
114+
int i, cnt, err = 0;
115+
116+
mutex_lock(&prog->aux->used_maps_mutex);
115117

116118
/* Quickly count the maps we will have to remember */
117119
cnt = 0;
118120
for (i = 0; i < prog->aux->used_map_cnt; i++)
119121
if (bpf_map_offload_neutral(prog->aux->used_maps[i]))
120122
cnt++;
121123
if (!cnt)
122-
return 0;
124+
goto out;
123125

124126
nfp_prog->map_records = kmalloc_array(cnt,
125127
sizeof(nfp_prog->map_records[0]),
126128
GFP_KERNEL);
127-
if (!nfp_prog->map_records)
128-
return -ENOMEM;
129+
if (!nfp_prog->map_records) {
130+
err = -ENOMEM;
131+
goto out;
132+
}
129133

130134
for (i = 0; i < prog->aux->used_map_cnt; i++)
131135
if (bpf_map_offload_neutral(prog->aux->used_maps[i])) {
132136
err = nfp_map_ptr_record(bpf, nfp_prog,
133137
prog->aux->used_maps[i]);
134138
if (err) {
135139
nfp_map_ptrs_forget(bpf, nfp_prog);
136-
return err;
140+
goto out;
137141
}
138142
}
139143
WARN_ON(cnt != nfp_prog->map_records_cnt);
140144

141-
return 0;
145+
out:
146+
mutex_unlock(&prog->aux->used_maps_mutex);
147+
return err;
142148
}
143149

144150
static int

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ struct bpf_prog_aux {
751751
struct bpf_ksym ksym;
752752
const struct bpf_prog_ops *ops;
753753
struct bpf_map **used_maps;
754+
struct mutex used_maps_mutex; /* mutex for used_maps and used_map_cnt */
754755
struct bpf_prog *prog;
755756
struct user_struct *user;
756757
u64 load_time; /* ns since boottime */

include/uapi/linux/bpf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ enum bpf_cmd {
124124
BPF_ENABLE_STATS,
125125
BPF_ITER_CREATE,
126126
BPF_LINK_DETACH,
127+
BPF_PROG_BIND_MAP,
127128
};
128129

129130
enum bpf_map_type {
@@ -658,6 +659,12 @@ union bpf_attr {
658659
__u32 flags;
659660
} iter_create;
660661

662+
struct { /* struct used by BPF_PROG_BIND_MAP command */
663+
__u32 prog_fd;
664+
__u32 map_fd;
665+
__u32 flags; /* extra flags */
666+
} prog_bind_map;
667+
661668
} __attribute__((aligned(8)));
662669

663670
/* The description below is an attempt at providing documentation to eBPF

kernel/bpf/core.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flag
9898
fp->jit_requested = ebpf_jit_enabled();
9999

100100
INIT_LIST_HEAD_RCU(&fp->aux->ksym.lnode);
101+
mutex_init(&fp->aux->used_maps_mutex);
101102

102103
return fp;
103104
}
@@ -253,6 +254,7 @@ struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
253254
void __bpf_prog_free(struct bpf_prog *fp)
254255
{
255256
if (fp->aux) {
257+
mutex_destroy(&fp->aux->used_maps_mutex);
256258
free_percpu(fp->aux->stats);
257259
kfree(fp->aux->poke_tab);
258260
kfree(fp->aux);
@@ -1747,8 +1749,9 @@ bool bpf_prog_array_compatible(struct bpf_array *array,
17471749
static int bpf_check_tail_call(const struct bpf_prog *fp)
17481750
{
17491751
struct bpf_prog_aux *aux = fp->aux;
1750-
int i;
1752+
int i, ret = 0;
17511753

1754+
mutex_lock(&aux->used_maps_mutex);
17521755
for (i = 0; i < aux->used_map_cnt; i++) {
17531756
struct bpf_map *map = aux->used_maps[i];
17541757
struct bpf_array *array;
@@ -1757,11 +1760,15 @@ static int bpf_check_tail_call(const struct bpf_prog *fp)
17571760
continue;
17581761

17591762
array = container_of(map, struct bpf_array, map);
1760-
if (!bpf_prog_array_compatible(array, fp))
1761-
return -EINVAL;
1763+
if (!bpf_prog_array_compatible(array, fp)) {
1764+
ret = -EINVAL;
1765+
goto out;
1766+
}
17621767
}
17631768

1764-
return 0;
1769+
out:
1770+
mutex_unlock(&aux->used_maps_mutex);
1771+
return ret;
17651772
}
17661773

17671774
static void bpf_prog_select_func(struct bpf_prog *fp)

kernel/bpf/syscall.c

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3162,21 +3162,25 @@ static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
31623162
const struct bpf_map *map;
31633163
int i;
31643164

3165+
mutex_lock(&prog->aux->used_maps_mutex);
31653166
for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
31663167
map = prog->aux->used_maps[i];
31673168
if (map == (void *)addr) {
31683169
*type = BPF_PSEUDO_MAP_FD;
3169-
return map;
3170+
goto out;
31703171
}
31713172
if (!map->ops->map_direct_value_meta)
31723173
continue;
31733174
if (!map->ops->map_direct_value_meta(map, addr, off)) {
31743175
*type = BPF_PSEUDO_MAP_VALUE;
3175-
return map;
3176+
goto out;
31763177
}
31773178
}
3179+
map = NULL;
31783180

3179-
return NULL;
3181+
out:
3182+
mutex_unlock(&prog->aux->used_maps_mutex);
3183+
return map;
31803184
}
31813185

31823186
static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
@@ -3294,6 +3298,7 @@ static int bpf_prog_get_info_by_fd(struct file *file,
32943298
memcpy(info.tag, prog->tag, sizeof(prog->tag));
32953299
memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
32963300

3301+
mutex_lock(&prog->aux->used_maps_mutex);
32973302
ulen = info.nr_map_ids;
32983303
info.nr_map_ids = prog->aux->used_map_cnt;
32993304
ulen = min_t(u32, info.nr_map_ids, ulen);
@@ -3303,9 +3308,12 @@ static int bpf_prog_get_info_by_fd(struct file *file,
33033308

33043309
for (i = 0; i < ulen; i++)
33053310
if (put_user(prog->aux->used_maps[i]->id,
3306-
&user_map_ids[i]))
3311+
&user_map_ids[i])) {
3312+
mutex_unlock(&prog->aux->used_maps_mutex);
33073313
return -EFAULT;
3314+
}
33083315
}
3316+
mutex_unlock(&prog->aux->used_maps_mutex);
33093317

33103318
err = set_info_rec_size(&info);
33113319
if (err)
@@ -4153,6 +4161,66 @@ static int bpf_iter_create(union bpf_attr *attr)
41534161
return err;
41544162
}
41554163

4164+
#define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4165+
4166+
static int bpf_prog_bind_map(union bpf_attr *attr)
4167+
{
4168+
struct bpf_prog *prog;
4169+
struct bpf_map *map;
4170+
struct bpf_map **used_maps_old, **used_maps_new;
4171+
int i, ret = 0;
4172+
4173+
if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4174+
return -EINVAL;
4175+
4176+
if (attr->prog_bind_map.flags)
4177+
return -EINVAL;
4178+
4179+
prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4180+
if (IS_ERR(prog))
4181+
return PTR_ERR(prog);
4182+
4183+
map = bpf_map_get(attr->prog_bind_map.map_fd);
4184+
if (IS_ERR(map)) {
4185+
ret = PTR_ERR(map);
4186+
goto out_prog_put;
4187+
}
4188+
4189+
mutex_lock(&prog->aux->used_maps_mutex);
4190+
4191+
used_maps_old = prog->aux->used_maps;
4192+
4193+
for (i = 0; i < prog->aux->used_map_cnt; i++)
4194+
if (used_maps_old[i] == map)
4195+
goto out_unlock;
4196+
4197+
used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4198+
sizeof(used_maps_new[0]),
4199+
GFP_KERNEL);
4200+
if (!used_maps_new) {
4201+
ret = -ENOMEM;
4202+
goto out_unlock;
4203+
}
4204+
4205+
memcpy(used_maps_new, used_maps_old,
4206+
sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4207+
used_maps_new[prog->aux->used_map_cnt] = map;
4208+
4209+
prog->aux->used_map_cnt++;
4210+
prog->aux->used_maps = used_maps_new;
4211+
4212+
kfree(used_maps_old);
4213+
4214+
out_unlock:
4215+
mutex_unlock(&prog->aux->used_maps_mutex);
4216+
4217+
if (ret)
4218+
bpf_map_put(map);
4219+
out_prog_put:
4220+
bpf_prog_put(prog);
4221+
return ret;
4222+
}
4223+
41564224
SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
41574225
{
41584226
union bpf_attr attr;
@@ -4286,6 +4354,9 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
42864354
case BPF_LINK_DETACH:
42874355
err = link_detach(&attr);
42884356
break;
4357+
case BPF_PROG_BIND_MAP:
4358+
err = bpf_prog_bind_map(&attr);
4359+
break;
42894360
default:
42904361
err = -EINVAL;
42914362
break;

net/core/dev.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5441,15 +5441,20 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
54415441
if (new) {
54425442
u32 i;
54435443

5444+
mutex_lock(&new->aux->used_maps_mutex);
5445+
54445446
/* generic XDP does not work with DEVMAPs that can
54455447
* have a bpf_prog installed on an entry
54465448
*/
54475449
for (i = 0; i < new->aux->used_map_cnt; i++) {
5448-
if (dev_map_can_have_prog(new->aux->used_maps[i]))
5449-
return -EINVAL;
5450-
if (cpu_map_prog_allowed(new->aux->used_maps[i]))
5450+
if (dev_map_can_have_prog(new->aux->used_maps[i]) ||
5451+
cpu_map_prog_allowed(new->aux->used_maps[i])) {
5452+
mutex_unlock(&new->aux->used_maps_mutex);
54515453
return -EINVAL;
5454+
}
54525455
}
5456+
5457+
mutex_unlock(&new->aux->used_maps_mutex);
54535458
}
54545459

54555460
switch (xdp->command) {

tools/bpf/bpftool/json_writer.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ void jsonw_pretty(json_writer_t *self, bool on)
119119
self->pretty = on;
120120
}
121121

122+
void jsonw_reset(json_writer_t *self)
123+
{
124+
assert(self->depth == 0);
125+
self->sep = '\0';
126+
}
127+
122128
/* Basic blocks */
123129
static void jsonw_begin(json_writer_t *self, int c)
124130
{

tools/bpf/bpftool/json_writer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ void jsonw_destroy(json_writer_t **self_p);
2727
/* Cause output to have pretty whitespace */
2828
void jsonw_pretty(json_writer_t *self, bool on);
2929

30+
/* Reset separator to create new JSON */
31+
void jsonw_reset(json_writer_t *self);
32+
3033
/* Add property name */
3134
void jsonw_name(json_writer_t *self, const char *name);
3235

0 commit comments

Comments
 (0)