Skip to content

Commit fa8380a

Browse files
committed
Merge tag 'bpf-next-6.12-struct-fd' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
Pull bpf 'struct fd' updates from Alexei Starovoitov: "This includes struct_fd BPF changes from Al and Andrii" * tag 'bpf-next-6.12-struct-fd' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next: bpf: convert bpf_token_create() to CLASS(fd, ...) security,bpf: constify struct path in bpf_token_create() LSM hook bpf: more trivial fdget() conversions bpf: trivial conversions for fdget() bpf: switch maps to CLASS(fd, ...) bpf: factor out fetching bpf_map from FD and adding it to used_maps list bpf: switch fdget_raw() uses to CLASS(fd_raw, ...) bpf: convert __bpf_prog_get() to CLASS(fd, ...)
2 parents 68e5c7d + 37d3dd6 commit fa8380a

File tree

12 files changed

+179
-303
lines changed

12 files changed

+179
-303
lines changed

include/linux/bpf.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2246,7 +2246,16 @@ void __bpf_obj_drop_impl(void *p, const struct btf_record *rec, bool percpu);
22462246

22472247
struct bpf_map *bpf_map_get(u32 ufd);
22482248
struct bpf_map *bpf_map_get_with_uref(u32 ufd);
2249-
struct bpf_map *__bpf_map_get(struct fd f);
2249+
2250+
static inline struct bpf_map *__bpf_map_get(struct fd f)
2251+
{
2252+
if (fd_empty(f))
2253+
return ERR_PTR(-EBADF);
2254+
if (unlikely(fd_file(f)->f_op != &bpf_map_fops))
2255+
return ERR_PTR(-EINVAL);
2256+
return fd_file(f)->private_data;
2257+
}
2258+
22502259
void bpf_map_inc(struct bpf_map *map);
22512260
void bpf_map_inc_with_uref(struct bpf_map *map);
22522261
struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref);

include/linux/lsm_hook_defs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ LSM_HOOK(int, 0, bpf_prog_load, struct bpf_prog *prog, union bpf_attr *attr,
431431
struct bpf_token *token)
432432
LSM_HOOK(void, LSM_RET_VOID, bpf_prog_free, struct bpf_prog *prog)
433433
LSM_HOOK(int, 0, bpf_token_create, struct bpf_token *token, union bpf_attr *attr,
434-
struct path *path)
434+
const struct path *path)
435435
LSM_HOOK(void, LSM_RET_VOID, bpf_token_free, struct bpf_token *token)
436436
LSM_HOOK(int, 0, bpf_token_cmd, const struct bpf_token *token, enum bpf_cmd cmd)
437437
LSM_HOOK(int, 0, bpf_token_capable, const struct bpf_token *token, int cap)

include/linux/security.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,7 +2182,7 @@ extern int security_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
21822182
struct bpf_token *token);
21832183
extern void security_bpf_prog_free(struct bpf_prog *prog);
21842184
extern int security_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
2185-
struct path *path);
2185+
const struct path *path);
21862186
extern void security_bpf_token_free(struct bpf_token *token);
21872187
extern int security_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cmd);
21882188
extern int security_bpf_token_capable(const struct bpf_token *token, int cap);
@@ -2222,7 +2222,7 @@ static inline void security_bpf_prog_free(struct bpf_prog *prog)
22222222
{ }
22232223

22242224
static inline int security_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
2225-
struct path *path)
2225+
const struct path *path)
22262226
{
22272227
return 0;
22282228
}

kernel/bpf/bpf_inode_storage.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,33 +78,29 @@ void bpf_inode_storage_free(struct inode *inode)
7878
static void *bpf_fd_inode_storage_lookup_elem(struct bpf_map *map, void *key)
7979
{
8080
struct bpf_local_storage_data *sdata;
81-
struct fd f = fdget_raw(*(int *)key);
81+
CLASS(fd_raw, f)(*(int *)key);
8282

83-
if (!fd_file(f))
83+
if (fd_empty(f))
8484
return ERR_PTR(-EBADF);
8585

8686
sdata = inode_storage_lookup(file_inode(fd_file(f)), map, true);
87-
fdput(f);
8887
return sdata ? sdata->data : NULL;
8988
}
9089

9190
static long bpf_fd_inode_storage_update_elem(struct bpf_map *map, void *key,
9291
void *value, u64 map_flags)
9392
{
9493
struct bpf_local_storage_data *sdata;
95-
struct fd f = fdget_raw(*(int *)key);
94+
CLASS(fd_raw, f)(*(int *)key);
9695

97-
if (!fd_file(f))
96+
if (fd_empty(f))
9897
return -EBADF;
99-
if (!inode_storage_ptr(file_inode(fd_file(f)))) {
100-
fdput(f);
98+
if (!inode_storage_ptr(file_inode(fd_file(f))))
10199
return -EBADF;
102-
}
103100

104101
sdata = bpf_local_storage_update(file_inode(fd_file(f)),
105102
(struct bpf_local_storage_map *)map,
106103
value, map_flags, GFP_ATOMIC);
107-
fdput(f);
108104
return PTR_ERR_OR_ZERO(sdata);
109105
}
110106

@@ -123,15 +119,11 @@ static int inode_storage_delete(struct inode *inode, struct bpf_map *map)
123119

124120
static long bpf_fd_inode_storage_delete_elem(struct bpf_map *map, void *key)
125121
{
126-
struct fd f = fdget_raw(*(int *)key);
127-
int err;
122+
CLASS(fd_raw, f)(*(int *)key);
128123

129-
if (!fd_file(f))
124+
if (fd_empty(f))
130125
return -EBADF;
131-
132-
err = inode_storage_delete(file_inode(fd_file(f)), map);
133-
fdput(f);
134-
return err;
126+
return inode_storage_delete(file_inode(fd_file(f)), map);
135127
}
136128

137129
/* *gfp_flags* is a hidden argument provided by the verifier */

kernel/bpf/btf.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7711,21 +7711,16 @@ int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
77117711
struct btf *btf_get_by_fd(int fd)
77127712
{
77137713
struct btf *btf;
7714-
struct fd f;
7714+
CLASS(fd, f)(fd);
77157715

7716-
f = fdget(fd);
7717-
7718-
if (!fd_file(f))
7716+
if (fd_empty(f))
77197717
return ERR_PTR(-EBADF);
77207718

7721-
if (fd_file(f)->f_op != &btf_fops) {
7722-
fdput(f);
7719+
if (fd_file(f)->f_op != &btf_fops)
77237720
return ERR_PTR(-EINVAL);
7724-
}
77257721

77267722
btf = fd_file(f)->private_data;
77277723
refcount_inc(&btf->refcnt);
7728-
fdput(f);
77297724

77307725
return btf;
77317726
}

kernel/bpf/map_in_map.c

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,27 @@ struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
1111
{
1212
struct bpf_map *inner_map, *inner_map_meta;
1313
u32 inner_map_meta_size;
14-
struct fd f;
15-
int ret;
14+
CLASS(fd, f)(inner_map_ufd);
1615

17-
f = fdget(inner_map_ufd);
1816
inner_map = __bpf_map_get(f);
1917
if (IS_ERR(inner_map))
2018
return inner_map;
2119

2220
/* Does not support >1 level map-in-map */
23-
if (inner_map->inner_map_meta) {
24-
ret = -EINVAL;
25-
goto put;
26-
}
21+
if (inner_map->inner_map_meta)
22+
return ERR_PTR(-EINVAL);
2723

28-
if (!inner_map->ops->map_meta_equal) {
29-
ret = -ENOTSUPP;
30-
goto put;
31-
}
24+
if (!inner_map->ops->map_meta_equal)
25+
return ERR_PTR(-ENOTSUPP);
3226

3327
inner_map_meta_size = sizeof(*inner_map_meta);
3428
/* In some cases verifier needs to access beyond just base map. */
3529
if (inner_map->ops == &array_map_ops || inner_map->ops == &percpu_array_map_ops)
3630
inner_map_meta_size = sizeof(struct bpf_array);
3731

3832
inner_map_meta = kzalloc(inner_map_meta_size, GFP_USER);
39-
if (!inner_map_meta) {
40-
ret = -ENOMEM;
41-
goto put;
42-
}
33+
if (!inner_map_meta)
34+
return ERR_PTR(-ENOMEM);
4335

4436
inner_map_meta->map_type = inner_map->map_type;
4537
inner_map_meta->key_size = inner_map->key_size;
@@ -53,8 +45,9 @@ struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
5345
* invalid/empty/valid, but ERR_PTR in case of errors. During
5446
* equality NULL or IS_ERR is equivalent.
5547
*/
56-
ret = PTR_ERR(inner_map_meta->record);
57-
goto free;
48+
struct bpf_map *ret = ERR_CAST(inner_map_meta->record);
49+
kfree(inner_map_meta);
50+
return ret;
5851
}
5952
/* Note: We must use the same BTF, as we also used btf_record_dup above
6053
* which relies on BTF being same for both maps, as some members like
@@ -77,14 +70,7 @@ struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
7770
inner_array_meta->elem_size = inner_array->elem_size;
7871
inner_map_meta->bypass_spec_v1 = inner_map->bypass_spec_v1;
7972
}
80-
81-
fdput(f);
8273
return inner_map_meta;
83-
free:
84-
kfree(inner_map_meta);
85-
put:
86-
fdput(f);
87-
return ERR_PTR(ret);
8874
}
8975

9076
void bpf_map_meta_free(struct bpf_map *map_meta)
@@ -110,9 +96,8 @@ void *bpf_map_fd_get_ptr(struct bpf_map *map,
11096
int ufd)
11197
{
11298
struct bpf_map *inner_map, *inner_map_meta;
113-
struct fd f;
99+
CLASS(fd, f)(ufd);
114100

115-
f = fdget(ufd);
116101
inner_map = __bpf_map_get(f);
117102
if (IS_ERR(inner_map))
118103
return inner_map;
@@ -123,7 +108,6 @@ void *bpf_map_fd_get_ptr(struct bpf_map *map,
123108
else
124109
inner_map = ERR_PTR(-EINVAL);
125110

126-
fdput(f);
127111
return inner_map;
128112
}
129113

0 commit comments

Comments
 (0)