^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Copyright (c) 2017 Facebook
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "map_in_map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) struct bpf_map *inner_map, *inner_map_meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) u32 inner_map_meta_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct fd f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) f = fdget(inner_map_ufd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) inner_map = __bpf_map_get(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) if (IS_ERR(inner_map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) return inner_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /* Does not support >1 level map-in-map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) if (inner_map->inner_map_meta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (!inner_map->ops->map_meta_equal) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return ERR_PTR(-ENOTSUPP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (map_value_has_spin_lock(inner_map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return ERR_PTR(-ENOTSUPP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) inner_map_meta_size = sizeof(*inner_map_meta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* In some cases verifier needs to access beyond just base map. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (inner_map->ops == &array_map_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) inner_map_meta_size = sizeof(struct bpf_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) inner_map_meta = kzalloc(inner_map_meta_size, GFP_USER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (!inner_map_meta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) inner_map_meta->map_type = inner_map->map_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) inner_map_meta->key_size = inner_map->key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) inner_map_meta->value_size = inner_map->value_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) inner_map_meta->map_flags = inner_map->map_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) inner_map_meta->max_entries = inner_map->max_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) inner_map_meta->spin_lock_off = inner_map->spin_lock_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* Misc members not needed in bpf_map_meta_equal() check. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) inner_map_meta->ops = inner_map->ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (inner_map->ops == &array_map_ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) inner_map_meta->bypass_spec_v1 = inner_map->bypass_spec_v1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) container_of(inner_map_meta, struct bpf_array, map)->index_mask =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) container_of(inner_map, struct bpf_array, map)->index_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return inner_map_meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) void bpf_map_meta_free(struct bpf_map *map_meta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) kfree(map_meta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) bool bpf_map_meta_equal(const struct bpf_map *meta0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) const struct bpf_map *meta1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /* No need to compare ops because it is covered by map_type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return meta0->map_type == meta1->map_type &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) meta0->key_size == meta1->key_size &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) meta0->value_size == meta1->value_size &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) meta0->map_flags == meta1->map_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) void *bpf_map_fd_get_ptr(struct bpf_map *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct file *map_file /* not used */,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int ufd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct bpf_map *inner_map, *inner_map_meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct fd f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) f = fdget(ufd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) inner_map = __bpf_map_get(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (IS_ERR(inner_map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return inner_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) inner_map_meta = map->inner_map_meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (inner_map_meta->ops->map_meta_equal(inner_map_meta, inner_map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) bpf_map_inc(inner_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) inner_map = ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return inner_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) void bpf_map_fd_put_ptr(void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /* ptr->ops->map_free() has to go through one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * rcu grace period by itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) bpf_map_put(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) u32 bpf_map_fd_sys_lookup_elem(void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return ((struct bpf_map *)ptr)->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }