Skip to content

Commit 64cae2f

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Daniel Borkmann says: ==================== pull-request: bpf 2020-08-08 The following pull-request contains BPF updates for your *net* tree. We've added 11 non-merge commits during the last 2 day(s) which contain a total of 24 files changed, 216 insertions(+), 135 deletions(-). The main changes are: 1) Fix UAPI for BPF map iterator before it gets frozen to allow for more extensions/customization in future, from Yonghong Song. 2) Fix selftests build to undo verbose build output, from Andrii Nakryiko. 3) Fix inlining compilation error on bpf_do_trace_printk() due to variable argument lists, from Stanislav Fomichev. 4) Fix an uninitialized pointer warning at btf__parse_raw() in libbpf, from Daniel T. Lee. 5) Fix several compilation warnings in selftests with regards to ignoring return value, from Jianlin Lv. 6) Fix interruptions by switching off timeout for BPF tests, from Jiri Benc. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents 7ee2492 + b8c1a30 commit 64cae2f

File tree

24 files changed

+216
-135
lines changed

24 files changed

+216
-135
lines changed

Documentation/bpf/bpf_design_QA.rst

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -246,17 +246,6 @@ program is loaded the kernel will print warning message, so
246246
this helper is only useful for experiments and prototypes.
247247
Tracing BPF programs are root only.
248248

249-
Q: bpf_trace_printk() helper warning
250-
------------------------------------
251-
Q: When bpf_trace_printk() helper is used the kernel prints nasty
252-
warning message. Why is that?
253-
254-
A: This is done to nudge program authors into better interfaces when
255-
programs need to pass data to user space. Like bpf_perf_event_output()
256-
can be used to efficiently stream data via perf ring buffer.
257-
BPF maps can be used for asynchronous data sharing between kernel
258-
and user space. bpf_trace_printk() should only be used for debugging.
259-
260249
Q: New functionality via kernel modules?
261250
----------------------------------------
262251
Q: Can BPF functionality such as new program or map types, new

include/linux/bpf.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,15 +1214,17 @@ struct bpf_iter_aux_info {
12141214
struct bpf_map *map;
12151215
};
12161216

1217-
typedef int (*bpf_iter_check_target_t)(struct bpf_prog *prog,
1218-
struct bpf_iter_aux_info *aux);
1217+
typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
1218+
union bpf_iter_link_info *linfo,
1219+
struct bpf_iter_aux_info *aux);
1220+
typedef void (*bpf_iter_detach_target_t)(struct bpf_iter_aux_info *aux);
12191221

12201222
#define BPF_ITER_CTX_ARG_MAX 2
12211223
struct bpf_iter_reg {
12221224
const char *target;
1223-
bpf_iter_check_target_t check_target;
1225+
bpf_iter_attach_target_t attach_target;
1226+
bpf_iter_detach_target_t detach_target;
12241227
u32 ctx_arg_info_size;
1225-
enum bpf_iter_link_info req_linfo;
12261228
struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
12271229
const struct bpf_iter_seq_info *seq_info;
12281230
};

include/uapi/linux/bpf.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ struct bpf_cgroup_storage_key {
8181
__u32 attach_type; /* program attach type */
8282
};
8383

84+
union bpf_iter_link_info {
85+
struct {
86+
__u32 map_fd;
87+
} map;
88+
};
89+
8490
/* BPF syscall commands, see bpf(2) man-page for details. */
8591
enum bpf_cmd {
8692
BPF_MAP_CREATE,
@@ -249,13 +255,6 @@ enum bpf_link_type {
249255
MAX_BPF_LINK_TYPE,
250256
};
251257

252-
enum bpf_iter_link_info {
253-
BPF_ITER_LINK_UNSPEC = 0,
254-
BPF_ITER_LINK_MAP_FD = 1,
255-
256-
MAX_BPF_ITER_LINK_INFO,
257-
};
258-
259258
/* cgroup-bpf attach flags used in BPF_PROG_ATTACH command
260259
*
261260
* NONE(default): No further bpf programs allowed in the subtree.
@@ -623,6 +622,8 @@ union bpf_attr {
623622
};
624623
__u32 attach_type; /* attach type */
625624
__u32 flags; /* extra flags */
625+
__aligned_u64 iter_info; /* extra bpf_iter_link_info */
626+
__u32 iter_info_len; /* iter_info length */
626627
} link_create;
627628

628629
struct { /* struct used by BPF_LINK_UPDATE command */

kernel/bpf/bpf_iter.c

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,8 @@ static void bpf_iter_link_release(struct bpf_link *link)
338338
struct bpf_iter_link *iter_link =
339339
container_of(link, struct bpf_iter_link, link);
340340

341-
if (iter_link->aux.map)
342-
bpf_map_put_with_uref(iter_link->aux.map);
341+
if (iter_link->tinfo->reg_info->detach_target)
342+
iter_link->tinfo->reg_info->detach_target(&iter_link->aux);
343343
}
344344

345345
static void bpf_iter_link_dealloc(struct bpf_link *link)
@@ -390,15 +390,35 @@ bool bpf_link_is_iter(struct bpf_link *link)
390390

391391
int bpf_iter_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
392392
{
393+
union bpf_iter_link_info __user *ulinfo;
393394
struct bpf_link_primer link_primer;
394395
struct bpf_iter_target_info *tinfo;
395-
struct bpf_iter_aux_info aux = {};
396+
union bpf_iter_link_info linfo;
396397
struct bpf_iter_link *link;
397-
u32 prog_btf_id, target_fd;
398+
u32 prog_btf_id, linfo_len;
398399
bool existed = false;
399-
struct bpf_map *map;
400400
int err;
401401

402+
if (attr->link_create.target_fd || attr->link_create.flags)
403+
return -EINVAL;
404+
405+
memset(&linfo, 0, sizeof(union bpf_iter_link_info));
406+
407+
ulinfo = u64_to_user_ptr(attr->link_create.iter_info);
408+
linfo_len = attr->link_create.iter_info_len;
409+
if (!ulinfo ^ !linfo_len)
410+
return -EINVAL;
411+
412+
if (ulinfo) {
413+
err = bpf_check_uarg_tail_zero(ulinfo, sizeof(linfo),
414+
linfo_len);
415+
if (err)
416+
return err;
417+
linfo_len = min_t(u32, linfo_len, sizeof(linfo));
418+
if (copy_from_user(&linfo, ulinfo, linfo_len))
419+
return -EFAULT;
420+
}
421+
402422
prog_btf_id = prog->aux->attach_btf_id;
403423
mutex_lock(&targets_mutex);
404424
list_for_each_entry(tinfo, &targets, list) {
@@ -411,13 +431,6 @@ int bpf_iter_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
411431
if (!existed)
412432
return -ENOENT;
413433

414-
/* Make sure user supplied flags are target expected. */
415-
target_fd = attr->link_create.target_fd;
416-
if (attr->link_create.flags != tinfo->reg_info->req_linfo)
417-
return -EINVAL;
418-
if (!attr->link_create.flags && target_fd)
419-
return -EINVAL;
420-
421434
link = kzalloc(sizeof(*link), GFP_USER | __GFP_NOWARN);
422435
if (!link)
423436
return -ENOMEM;
@@ -431,28 +444,15 @@ int bpf_iter_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
431444
return err;
432445
}
433446

434-
if (tinfo->reg_info->req_linfo == BPF_ITER_LINK_MAP_FD) {
435-
map = bpf_map_get_with_uref(target_fd);
436-
if (IS_ERR(map)) {
437-
err = PTR_ERR(map);
438-
goto cleanup_link;
439-
}
440-
441-
aux.map = map;
442-
err = tinfo->reg_info->check_target(prog, &aux);
447+
if (tinfo->reg_info->attach_target) {
448+
err = tinfo->reg_info->attach_target(prog, &linfo, &link->aux);
443449
if (err) {
444-
bpf_map_put_with_uref(map);
445-
goto cleanup_link;
450+
bpf_link_cleanup(&link_primer);
451+
return err;
446452
}
447-
448-
link->aux.map = map;
449453
}
450454

451455
return bpf_link_settle(&link_primer);
452-
453-
cleanup_link:
454-
bpf_link_cleanup(&link_primer);
455-
return err;
456456
}
457457

458458
static void init_seq_meta(struct bpf_iter_priv_data *priv_data,

kernel/bpf/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1966,7 +1966,7 @@ void bpf_prog_array_delete_safe(struct bpf_prog_array *array,
19661966
* @index: the index of the program to replace
19671967
*
19681968
* Skips over dummy programs, by not counting them, when calculating
1969-
* the the position of the program to replace.
1969+
* the position of the program to replace.
19701970
*
19711971
* Return:
19721972
* * 0 - Success

kernel/bpf/map_iter.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,21 @@ static struct bpf_iter_reg bpf_map_reg_info = {
9898
.seq_info = &bpf_map_seq_info,
9999
};
100100

101-
static int bpf_iter_check_map(struct bpf_prog *prog,
102-
struct bpf_iter_aux_info *aux)
101+
static int bpf_iter_attach_map(struct bpf_prog *prog,
102+
union bpf_iter_link_info *linfo,
103+
struct bpf_iter_aux_info *aux)
103104
{
104105
u32 key_acc_size, value_acc_size, key_size, value_size;
105-
struct bpf_map *map = aux->map;
106+
struct bpf_map *map;
106107
bool is_percpu = false;
108+
int err = -EINVAL;
109+
110+
if (!linfo->map.map_fd)
111+
return -EBADF;
112+
113+
map = bpf_map_get_with_uref(linfo->map.map_fd);
114+
if (IS_ERR(map))
115+
return PTR_ERR(map);
107116

108117
if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
109118
map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
@@ -112,7 +121,7 @@ static int bpf_iter_check_map(struct bpf_prog *prog,
112121
else if (map->map_type != BPF_MAP_TYPE_HASH &&
113122
map->map_type != BPF_MAP_TYPE_LRU_HASH &&
114123
map->map_type != BPF_MAP_TYPE_ARRAY)
115-
return -EINVAL;
124+
goto put_map;
116125

117126
key_acc_size = prog->aux->max_rdonly_access;
118127
value_acc_size = prog->aux->max_rdwr_access;
@@ -122,19 +131,31 @@ static int bpf_iter_check_map(struct bpf_prog *prog,
122131
else
123132
value_size = round_up(map->value_size, 8) * num_possible_cpus();
124133

125-
if (key_acc_size > key_size || value_acc_size > value_size)
126-
return -EACCES;
134+
if (key_acc_size > key_size || value_acc_size > value_size) {
135+
err = -EACCES;
136+
goto put_map;
137+
}
127138

139+
aux->map = map;
128140
return 0;
141+
142+
put_map:
143+
bpf_map_put_with_uref(map);
144+
return err;
145+
}
146+
147+
static void bpf_iter_detach_map(struct bpf_iter_aux_info *aux)
148+
{
149+
bpf_map_put_with_uref(aux->map);
129150
}
130151

131152
DEFINE_BPF_ITER_FUNC(bpf_map_elem, struct bpf_iter_meta *meta,
132153
struct bpf_map *map, void *key, void *value)
133154

134155
static const struct bpf_iter_reg bpf_map_elem_reg_info = {
135156
.target = "bpf_map_elem",
136-
.check_target = bpf_iter_check_map,
137-
.req_linfo = BPF_ITER_LINK_MAP_FD,
157+
.attach_target = bpf_iter_attach_map,
158+
.detach_target = bpf_iter_detach_map,
138159
.ctx_arg_info_size = 2,
139160
.ctx_arg_info = {
140161
{ offsetof(struct bpf_iter__bpf_map_elem, key),

kernel/bpf/syscall.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3883,7 +3883,7 @@ static int tracing_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *
38833883
return -EINVAL;
38843884
}
38853885

3886-
#define BPF_LINK_CREATE_LAST_FIELD link_create.flags
3886+
#define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
38873887
static int link_create(union bpf_attr *attr)
38883888
{
38893889
enum bpf_prog_type ptype;

kernel/bpf/verifier.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8294,7 +8294,7 @@ static bool stacksafe(struct bpf_func_state *old,
82948294
if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
82958295
cur->stack[spi].slot_type[i % BPF_REG_SIZE])
82968296
/* Ex: old explored (safe) state has STACK_SPILL in
8297-
* this stack slot, but current has has STACK_MISC ->
8297+
* this stack slot, but current has STACK_MISC ->
82988298
* this verifier states are not equivalent,
82998299
* return false to continue verification of this path
83008300
*/

kernel/trace/bpf_trace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ static DEFINE_RAW_SPINLOCK(trace_printk_lock);
383383

384384
#define BPF_TRACE_PRINTK_SIZE 1024
385385

386-
static inline __printf(1, 0) int bpf_do_trace_printk(const char *fmt, ...)
386+
static __printf(1, 0) int bpf_do_trace_printk(const char *fmt, ...)
387387
{
388388
static char buf[BPF_TRACE_PRINTK_SIZE];
389389
unsigned long flags;

net/core/bpf_sk_storage.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,18 +1384,39 @@ static int bpf_iter_init_sk_storage_map(void *priv_data,
13841384
return 0;
13851385
}
13861386

1387-
static int bpf_iter_check_map(struct bpf_prog *prog,
1388-
struct bpf_iter_aux_info *aux)
1387+
static int bpf_iter_attach_map(struct bpf_prog *prog,
1388+
union bpf_iter_link_info *linfo,
1389+
struct bpf_iter_aux_info *aux)
13891390
{
1390-
struct bpf_map *map = aux->map;
1391+
struct bpf_map *map;
1392+
int err = -EINVAL;
1393+
1394+
if (!linfo->map.map_fd)
1395+
return -EBADF;
1396+
1397+
map = bpf_map_get_with_uref(linfo->map.map_fd);
1398+
if (IS_ERR(map))
1399+
return PTR_ERR(map);
13911400

13921401
if (map->map_type != BPF_MAP_TYPE_SK_STORAGE)
1393-
return -EINVAL;
1402+
goto put_map;
13941403

1395-
if (prog->aux->max_rdonly_access > map->value_size)
1396-
return -EACCES;
1404+
if (prog->aux->max_rdonly_access > map->value_size) {
1405+
err = -EACCES;
1406+
goto put_map;
1407+
}
13971408

1409+
aux->map = map;
13981410
return 0;
1411+
1412+
put_map:
1413+
bpf_map_put_with_uref(map);
1414+
return err;
1415+
}
1416+
1417+
static void bpf_iter_detach_map(struct bpf_iter_aux_info *aux)
1418+
{
1419+
bpf_map_put_with_uref(aux->map);
13991420
}
14001421

14011422
static const struct seq_operations bpf_sk_storage_map_seq_ops = {
@@ -1414,8 +1435,8 @@ static const struct bpf_iter_seq_info iter_seq_info = {
14141435

14151436
static struct bpf_iter_reg bpf_sk_storage_map_reg_info = {
14161437
.target = "bpf_sk_storage_map",
1417-
.check_target = bpf_iter_check_map,
1418-
.req_linfo = BPF_ITER_LINK_MAP_FD,
1438+
.attach_target = bpf_iter_attach_map,
1439+
.detach_target = bpf_iter_detach_map,
14191440
.ctx_arg_info_size = 2,
14201441
.ctx_arg_info = {
14211442
{ offsetof(struct bpf_iter__bpf_sk_storage_map, sk),

0 commit comments

Comments
 (0)