Skip to content

Commit d3b607f

Browse files
Hou Taomehmetb0
authored andcommitted
bpf: Call the missed btf_record_free() when map creation fails
BugLink: https://bugs.launchpad.net/bugs/2097301 [ Upstream commit 87e9675 ] When security_bpf_map_create() in map_create() fails, map_create() will call btf_put() and ->map_free() callback to free the map. It doesn't free the btf_record of map value, so add the missed btf_record_free() when map creation fails. However btf_record_free() needs to be called after ->map_free() just like bpf_map_free_deferred() did, because ->map_free() may use the btf_record to free the special fields in preallocated map value. So factor out bpf_map_free() helper to free the map, btf_record, and btf orderly and use the helper in both map_create() and bpf_map_free_deferred(). Signed-off-by: Hou Tao <houtao1@huawei.com> Acked-by: Jiri Olsa <jolsa@kernel.org> Link: https://lore.kernel.org/r/20240912012845.3458483-2-houtao@huaweicloud.com Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Manuel Diewald <manuel.diewald@canonical.com> Signed-off-by: Koichiro Den <koichiro.den@canonical.com>
1 parent f58ee48 commit d3b607f

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

kernel/bpf/syscall.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -687,15 +687,11 @@ void bpf_obj_free_fields(const struct btf_record *rec, void *obj)
687687
}
688688
}
689689

690-
/* called from workqueue */
691-
static void bpf_map_free_deferred(struct work_struct *work)
690+
static void bpf_map_free(struct bpf_map *map)
692691
{
693-
struct bpf_map *map = container_of(work, struct bpf_map, work);
694692
struct btf_record *rec = map->record;
695693
struct btf *btf = map->btf;
696694

697-
security_bpf_map_free(map);
698-
bpf_map_release_memcg(map);
699695
/* implementation dependent freeing */
700696
map->ops->map_free(map);
701697
/* Delay freeing of btf_record for maps, as map_free
@@ -714,6 +710,16 @@ static void bpf_map_free_deferred(struct work_struct *work)
714710
btf_put(btf);
715711
}
716712

713+
/* called from workqueue */
714+
static void bpf_map_free_deferred(struct work_struct *work)
715+
{
716+
struct bpf_map *map = container_of(work, struct bpf_map, work);
717+
718+
security_bpf_map_free(map);
719+
bpf_map_release_memcg(map);
720+
bpf_map_free(map);
721+
}
722+
717723
static void bpf_map_put_uref(struct bpf_map *map)
718724
{
719725
if (atomic64_dec_and_test(&map->usercnt)) {
@@ -1312,8 +1318,7 @@ static int map_create(union bpf_attr *attr)
13121318
free_map_sec:
13131319
security_bpf_map_free(map);
13141320
free_map:
1315-
btf_put(map->btf);
1316-
map->ops->map_free(map);
1321+
bpf_map_free(map);
13171322
return err;
13181323
}
13191324

0 commit comments

Comments
 (0)