Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Copyright (c) 2017 Facebook
0003  */
0004 #include <linux/slab.h>
0005 #include <linux/bpf.h>
0006 #include <linux/btf.h>
0007 
0008 #include "map_in_map.h"
0009 
0010 struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
0011 {
0012     struct bpf_map *inner_map, *inner_map_meta;
0013     u32 inner_map_meta_size;
0014     struct fd f;
0015 
0016     f = fdget(inner_map_ufd);
0017     inner_map = __bpf_map_get(f);
0018     if (IS_ERR(inner_map))
0019         return inner_map;
0020 
0021     /* Does not support >1 level map-in-map */
0022     if (inner_map->inner_map_meta) {
0023         fdput(f);
0024         return ERR_PTR(-EINVAL);
0025     }
0026 
0027     if (!inner_map->ops->map_meta_equal) {
0028         fdput(f);
0029         return ERR_PTR(-ENOTSUPP);
0030     }
0031 
0032     if (map_value_has_spin_lock(inner_map)) {
0033         fdput(f);
0034         return ERR_PTR(-ENOTSUPP);
0035     }
0036 
0037     inner_map_meta_size = sizeof(*inner_map_meta);
0038     /* In some cases verifier needs to access beyond just base map. */
0039     if (inner_map->ops == &array_map_ops)
0040         inner_map_meta_size = sizeof(struct bpf_array);
0041 
0042     inner_map_meta = kzalloc(inner_map_meta_size, GFP_USER);
0043     if (!inner_map_meta) {
0044         fdput(f);
0045         return ERR_PTR(-ENOMEM);
0046     }
0047 
0048     inner_map_meta->map_type = inner_map->map_type;
0049     inner_map_meta->key_size = inner_map->key_size;
0050     inner_map_meta->value_size = inner_map->value_size;
0051     inner_map_meta->map_flags = inner_map->map_flags;
0052     inner_map_meta->max_entries = inner_map->max_entries;
0053     inner_map_meta->spin_lock_off = inner_map->spin_lock_off;
0054     inner_map_meta->timer_off = inner_map->timer_off;
0055     inner_map_meta->kptr_off_tab = bpf_map_copy_kptr_off_tab(inner_map);
0056     if (inner_map->btf) {
0057         btf_get(inner_map->btf);
0058         inner_map_meta->btf = inner_map->btf;
0059     }
0060 
0061     /* Misc members not needed in bpf_map_meta_equal() check. */
0062     inner_map_meta->ops = inner_map->ops;
0063     if (inner_map->ops == &array_map_ops) {
0064         inner_map_meta->bypass_spec_v1 = inner_map->bypass_spec_v1;
0065         container_of(inner_map_meta, struct bpf_array, map)->index_mask =
0066              container_of(inner_map, struct bpf_array, map)->index_mask;
0067     }
0068 
0069     fdput(f);
0070     return inner_map_meta;
0071 }
0072 
0073 void bpf_map_meta_free(struct bpf_map *map_meta)
0074 {
0075     bpf_map_free_kptr_off_tab(map_meta);
0076     btf_put(map_meta->btf);
0077     kfree(map_meta);
0078 }
0079 
0080 bool bpf_map_meta_equal(const struct bpf_map *meta0,
0081             const struct bpf_map *meta1)
0082 {
0083     /* No need to compare ops because it is covered by map_type */
0084     return meta0->map_type == meta1->map_type &&
0085         meta0->key_size == meta1->key_size &&
0086         meta0->value_size == meta1->value_size &&
0087         meta0->timer_off == meta1->timer_off &&
0088         meta0->map_flags == meta1->map_flags &&
0089         bpf_map_equal_kptr_off_tab(meta0, meta1);
0090 }
0091 
0092 void *bpf_map_fd_get_ptr(struct bpf_map *map,
0093              struct file *map_file /* not used */,
0094              int ufd)
0095 {
0096     struct bpf_map *inner_map, *inner_map_meta;
0097     struct fd f;
0098 
0099     f = fdget(ufd);
0100     inner_map = __bpf_map_get(f);
0101     if (IS_ERR(inner_map))
0102         return inner_map;
0103 
0104     inner_map_meta = map->inner_map_meta;
0105     if (inner_map_meta->ops->map_meta_equal(inner_map_meta, inner_map))
0106         bpf_map_inc(inner_map);
0107     else
0108         inner_map = ERR_PTR(-EINVAL);
0109 
0110     fdput(f);
0111     return inner_map;
0112 }
0113 
0114 void bpf_map_fd_put_ptr(void *ptr)
0115 {
0116     /* ptr->ops->map_free() has to go through one
0117      * rcu grace period by itself.
0118      */
0119     bpf_map_put(ptr);
0120 }
0121 
0122 u32 bpf_map_fd_sys_lookup_elem(void *ptr)
0123 {
0124     return ((struct bpf_map *)ptr)->id;
0125 }