Skip to content

Commit c210129

Browse files
borkmanndavem330
authored andcommitted
bpf: align and clean bpf_{map,prog}_get helpers
Add a bpf_map_get() function that we're going to use later on and align/clean the remaining helpers a bit so that we have them a bit more consistent: - __bpf_map_get() and __bpf_prog_get() that both work on the fd struct, check whether the descriptor is eBPF and return the pointer to the map/prog stored in the private data. Also, we can return f.file->private_data directly, the function signature is enough of a documentation already. - bpf_map_get() and bpf_prog_get() that both work on u32 user fd, call their respective __bpf_map_get()/__bpf_prog_get() variants, and take a reference. Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent aa79781 commit c210129

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ struct bpf_prog *bpf_prog_get(u32 ufd);
167167
void bpf_prog_put(struct bpf_prog *prog);
168168
void bpf_prog_put_rcu(struct bpf_prog *prog);
169169

170-
struct bpf_map *bpf_map_get(struct fd f);
170+
struct bpf_map *__bpf_map_get(struct fd f);
171171
void bpf_map_put(struct bpf_map *map);
172172

173173
extern int sysctl_unprivileged_bpf_disabled;

kernel/bpf/syscall.c

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,29 @@ static int map_create(union bpf_attr *attr)
162162
/* if error is returned, fd is released.
163163
* On success caller should complete fd access with matching fdput()
164164
*/
165-
struct bpf_map *bpf_map_get(struct fd f)
165+
struct bpf_map *__bpf_map_get(struct fd f)
166166
{
167-
struct bpf_map *map;
168-
169167
if (!f.file)
170168
return ERR_PTR(-EBADF);
171-
172169
if (f.file->f_op != &bpf_map_fops) {
173170
fdput(f);
174171
return ERR_PTR(-EINVAL);
175172
}
176173

177-
map = f.file->private_data;
174+
return f.file->private_data;
175+
}
176+
177+
static struct bpf_map *bpf_map_get(u32 ufd)
178+
{
179+
struct fd f = fdget(ufd);
180+
struct bpf_map *map;
181+
182+
map = __bpf_map_get(f);
183+
if (IS_ERR(map))
184+
return map;
185+
186+
atomic_inc(&map->refcnt);
187+
fdput(f);
178188

179189
return map;
180190
}
@@ -202,7 +212,7 @@ static int map_lookup_elem(union bpf_attr *attr)
202212
return -EINVAL;
203213

204214
f = fdget(ufd);
205-
map = bpf_map_get(f);
215+
map = __bpf_map_get(f);
206216
if (IS_ERR(map))
207217
return PTR_ERR(map);
208218

@@ -261,7 +271,7 @@ static int map_update_elem(union bpf_attr *attr)
261271
return -EINVAL;
262272

263273
f = fdget(ufd);
264-
map = bpf_map_get(f);
274+
map = __bpf_map_get(f);
265275
if (IS_ERR(map))
266276
return PTR_ERR(map);
267277

@@ -314,7 +324,7 @@ static int map_delete_elem(union bpf_attr *attr)
314324
return -EINVAL;
315325

316326
f = fdget(ufd);
317-
map = bpf_map_get(f);
327+
map = __bpf_map_get(f);
318328
if (IS_ERR(map))
319329
return PTR_ERR(map);
320330

@@ -355,7 +365,7 @@ static int map_get_next_key(union bpf_attr *attr)
355365
return -EINVAL;
356366

357367
f = fdget(ufd);
358-
map = bpf_map_get(f);
368+
map = __bpf_map_get(f);
359369
if (IS_ERR(map))
360370
return PTR_ERR(map);
361371

@@ -549,21 +559,16 @@ static int bpf_prog_new_fd(struct bpf_prog *prog)
549559
O_RDWR | O_CLOEXEC);
550560
}
551561

552-
static struct bpf_prog *get_prog(struct fd f)
562+
static struct bpf_prog *__bpf_prog_get(struct fd f)
553563
{
554-
struct bpf_prog *prog;
555-
556564
if (!f.file)
557565
return ERR_PTR(-EBADF);
558-
559566
if (f.file->f_op != &bpf_prog_fops) {
560567
fdput(f);
561568
return ERR_PTR(-EINVAL);
562569
}
563570

564-
prog = f.file->private_data;
565-
566-
return prog;
571+
return f.file->private_data;
567572
}
568573

569574
/* called by sockets/tracing/seccomp before attaching program to an event
@@ -574,13 +579,13 @@ struct bpf_prog *bpf_prog_get(u32 ufd)
574579
struct fd f = fdget(ufd);
575580
struct bpf_prog *prog;
576581

577-
prog = get_prog(f);
578-
582+
prog = __bpf_prog_get(f);
579583
if (IS_ERR(prog))
580584
return prog;
581585

582586
atomic_inc(&prog->aux->refcnt);
583587
fdput(f);
588+
584589
return prog;
585590
}
586591
EXPORT_SYMBOL_GPL(bpf_prog_get);

kernel/bpf/verifier.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,8 +1989,7 @@ static int replace_map_fd_with_map_ptr(struct verifier_env *env)
19891989
}
19901990

19911991
f = fdget(insn->imm);
1992-
1993-
map = bpf_map_get(f);
1992+
map = __bpf_map_get(f);
19941993
if (IS_ERR(map)) {
19951994
verbose("fd %d is not pointing to valid bpf_map\n",
19961995
insn->imm);

0 commit comments

Comments
 (0)