xref: /linux-6.15/kernel/bpf/map_in_map.c (revision 55f32595)
125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
256f668dfSMartin KaFai Lau /* Copyright (c) 2017 Facebook
356f668dfSMartin KaFai Lau  */
456f668dfSMartin KaFai Lau #include <linux/slab.h>
556f668dfSMartin KaFai Lau #include <linux/bpf.h>
640ec00abSAlexei Starovoitov #include <linux/btf.h>
756f668dfSMartin KaFai Lau 
856f668dfSMartin KaFai Lau #include "map_in_map.h"
956f668dfSMartin KaFai Lau 
bpf_map_meta_alloc(int inner_map_ufd)1056f668dfSMartin KaFai Lau struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
1156f668dfSMartin KaFai Lau {
1256f668dfSMartin KaFai Lau 	struct bpf_map *inner_map, *inner_map_meta;
139d5564ddSDaniel Borkmann 	u32 inner_map_meta_size;
14*55f32595SAl Viro 	CLASS(fd, f)(inner_map_ufd);
1556f668dfSMartin KaFai Lau 
1656f668dfSMartin KaFai Lau 	inner_map = __bpf_map_get(f);
1756f668dfSMartin KaFai Lau 	if (IS_ERR(inner_map))
1856f668dfSMartin KaFai Lau 		return inner_map;
1956f668dfSMartin KaFai Lau 
2056f668dfSMartin KaFai Lau 	/* Does not support >1 level map-in-map */
21*55f32595SAl Viro 	if (inner_map->inner_map_meta)
22*55f32595SAl Viro 		return ERR_PTR(-EINVAL);
2356f668dfSMartin KaFai Lau 
24*55f32595SAl Viro 	if (!inner_map->ops->map_meta_equal)
25*55f32595SAl Viro 		return ERR_PTR(-ENOTSUPP);
26f4d05259SMartin KaFai Lau 
279d5564ddSDaniel Borkmann 	inner_map_meta_size = sizeof(*inner_map_meta);
289d5564ddSDaniel Borkmann 	/* In some cases verifier needs to access beyond just base map. */
299ee98229SAndrii Nakryiko 	if (inner_map->ops == &array_map_ops || inner_map->ops == &percpu_array_map_ops)
309d5564ddSDaniel Borkmann 		inner_map_meta_size = sizeof(struct bpf_array);
319d5564ddSDaniel Borkmann 
329d5564ddSDaniel Borkmann 	inner_map_meta = kzalloc(inner_map_meta_size, GFP_USER);
33*55f32595SAl Viro 	if (!inner_map_meta)
34*55f32595SAl Viro 		return ERR_PTR(-ENOMEM);
3556f668dfSMartin KaFai Lau 
3656f668dfSMartin KaFai Lau 	inner_map_meta->map_type = inner_map->map_type;
3756f668dfSMartin KaFai Lau 	inner_map_meta->key_size = inner_map->key_size;
3856f668dfSMartin KaFai Lau 	inner_map_meta->value_size = inner_map->value_size;
3956f668dfSMartin KaFai Lau 	inner_map_meta->map_flags = inner_map->map_flags;
4056f668dfSMartin KaFai Lau 	inner_map_meta->max_entries = inner_map->max_entries;
41f73e601aSKumar Kartikeya Dwivedi 
42aa3496acSKumar Kartikeya Dwivedi 	inner_map_meta->record = btf_record_dup(inner_map->record);
43aa3496acSKumar Kartikeya Dwivedi 	if (IS_ERR(inner_map_meta->record)) {
44aa3496acSKumar Kartikeya Dwivedi 		/* btf_record_dup returns NULL or valid pointer in case of
45aa3496acSKumar Kartikeya Dwivedi 		 * invalid/empty/valid, but ERR_PTR in case of errors. During
46aa3496acSKumar Kartikeya Dwivedi 		 * equality NULL or IS_ERR is equivalent.
47aa3496acSKumar Kartikeya Dwivedi 		 */
48*55f32595SAl Viro 		struct bpf_map *ret = ERR_CAST(inner_map_meta->record);
49*55f32595SAl Viro 		kfree(inner_map_meta);
50*55f32595SAl Viro 		return ret;
51f73e601aSKumar Kartikeya Dwivedi 	}
52c22dfdd2SKumar Kartikeya Dwivedi 	/* Note: We must use the same BTF, as we also used btf_record_dup above
53c22dfdd2SKumar Kartikeya Dwivedi 	 * which relies on BTF being same for both maps, as some members like
54c22dfdd2SKumar Kartikeya Dwivedi 	 * record->fields.list_head have pointers like value_rec pointing into
55c22dfdd2SKumar Kartikeya Dwivedi 	 * inner_map->btf.
56c22dfdd2SKumar Kartikeya Dwivedi 	 */
5740ec00abSAlexei Starovoitov 	if (inner_map->btf) {
5840ec00abSAlexei Starovoitov 		btf_get(inner_map->btf);
5940ec00abSAlexei Starovoitov 		inner_map_meta->btf = inner_map->btf;
6040ec00abSAlexei Starovoitov 	}
6156f668dfSMartin KaFai Lau 
629d5564ddSDaniel Borkmann 	/* Misc members not needed in bpf_map_meta_equal() check. */
639d5564ddSDaniel Borkmann 	inner_map_meta->ops = inner_map->ops;
649ee98229SAndrii Nakryiko 	if (inner_map->ops == &array_map_ops || inner_map->ops == &percpu_array_map_ops) {
65cba41bb7SRhys Rustad-Elliott 		struct bpf_array *inner_array_meta =
66cba41bb7SRhys Rustad-Elliott 			container_of(inner_map_meta, struct bpf_array, map);
67cba41bb7SRhys Rustad-Elliott 		struct bpf_array *inner_array = container_of(inner_map, struct bpf_array, map);
68cba41bb7SRhys Rustad-Elliott 
69cba41bb7SRhys Rustad-Elliott 		inner_array_meta->index_mask = inner_array->index_mask;
70cba41bb7SRhys Rustad-Elliott 		inner_array_meta->elem_size = inner_array->elem_size;
712c78ee89SAlexei Starovoitov 		inner_map_meta->bypass_spec_v1 = inner_map->bypass_spec_v1;
729d5564ddSDaniel Borkmann 	}
7356f668dfSMartin KaFai Lau 	return inner_map_meta;
7456f668dfSMartin KaFai Lau }
7556f668dfSMartin KaFai Lau 
bpf_map_meta_free(struct bpf_map * map_meta)7656f668dfSMartin KaFai Lau void bpf_map_meta_free(struct bpf_map *map_meta)
7756f668dfSMartin KaFai Lau {
78aa3496acSKumar Kartikeya Dwivedi 	bpf_map_free_record(map_meta);
7940ec00abSAlexei Starovoitov 	btf_put(map_meta->btf);
8056f668dfSMartin KaFai Lau 	kfree(map_meta);
8156f668dfSMartin KaFai Lau }
8256f668dfSMartin KaFai Lau 
bpf_map_meta_equal(const struct bpf_map * meta0,const struct bpf_map * meta1)8356f668dfSMartin KaFai Lau bool bpf_map_meta_equal(const struct bpf_map *meta0,
8456f668dfSMartin KaFai Lau 			const struct bpf_map *meta1)
8556f668dfSMartin KaFai Lau {
8656f668dfSMartin KaFai Lau 	/* No need to compare ops because it is covered by map_type */
8756f668dfSMartin KaFai Lau 	return meta0->map_type == meta1->map_type &&
8856f668dfSMartin KaFai Lau 		meta0->key_size == meta1->key_size &&
8956f668dfSMartin KaFai Lau 		meta0->value_size == meta1->value_size &&
9061df10c7SKumar Kartikeya Dwivedi 		meta0->map_flags == meta1->map_flags &&
91aa3496acSKumar Kartikeya Dwivedi 		btf_record_equal(meta0->record, meta1->record);
9256f668dfSMartin KaFai Lau }
9356f668dfSMartin KaFai Lau 
bpf_map_fd_get_ptr(struct bpf_map * map,struct file * map_file,int ufd)9456f668dfSMartin KaFai Lau void *bpf_map_fd_get_ptr(struct bpf_map *map,
9556f668dfSMartin KaFai Lau 			 struct file *map_file /* not used */,
9656f668dfSMartin KaFai Lau 			 int ufd)
9756f668dfSMartin KaFai Lau {
98f4d05259SMartin KaFai Lau 	struct bpf_map *inner_map, *inner_map_meta;
99*55f32595SAl Viro 	CLASS(fd, f)(ufd);
10056f668dfSMartin KaFai Lau 
10156f668dfSMartin KaFai Lau 	inner_map = __bpf_map_get(f);
10256f668dfSMartin KaFai Lau 	if (IS_ERR(inner_map))
10356f668dfSMartin KaFai Lau 		return inner_map;
10456f668dfSMartin KaFai Lau 
105f4d05259SMartin KaFai Lau 	inner_map_meta = map->inner_map_meta;
106f4d05259SMartin KaFai Lau 	if (inner_map_meta->ops->map_meta_equal(inner_map_meta, inner_map))
1071e0bd5a0SAndrii Nakryiko 		bpf_map_inc(inner_map);
10856f668dfSMartin KaFai Lau 	else
10956f668dfSMartin KaFai Lau 		inner_map = ERR_PTR(-EINVAL);
11056f668dfSMartin KaFai Lau 
11156f668dfSMartin KaFai Lau 	return inner_map;
11256f668dfSMartin KaFai Lau }
11356f668dfSMartin KaFai Lau 
bpf_map_fd_put_ptr(struct bpf_map * map,void * ptr,bool need_defer)11420c20bd1SHou Tao void bpf_map_fd_put_ptr(struct bpf_map *map, void *ptr, bool need_defer)
11556f668dfSMartin KaFai Lau {
11687667336SHou Tao 	struct bpf_map *inner_map = ptr;
11787667336SHou Tao 
118af66bfd3SHou Tao 	/* Defer the freeing of inner map according to the sleepable attribute
119af66bfd3SHou Tao 	 * of bpf program which owns the outer map, so unnecessary waiting for
120af66bfd3SHou Tao 	 * RCU tasks trace grace period can be avoided.
12156f668dfSMartin KaFai Lau 	 */
122af66bfd3SHou Tao 	if (need_defer) {
123af66bfd3SHou Tao 		if (atomic64_read(&map->sleepable_refcnt))
12487667336SHou Tao 			WRITE_ONCE(inner_map->free_after_mult_rcu_gp, true);
125af66bfd3SHou Tao 		else
126af66bfd3SHou Tao 			WRITE_ONCE(inner_map->free_after_rcu_gp, true);
127af66bfd3SHou Tao 	}
12887667336SHou Tao 	bpf_map_put(inner_map);
12956f668dfSMartin KaFai Lau }
13014dc6f04SMartin KaFai Lau 
bpf_map_fd_sys_lookup_elem(void * ptr)13114dc6f04SMartin KaFai Lau u32 bpf_map_fd_sys_lookup_elem(void *ptr)
13214dc6f04SMartin KaFai Lau {
13314dc6f04SMartin KaFai Lau 	return ((struct bpf_map *)ptr)->id;
13414dc6f04SMartin KaFai Lau }
135