map_in_map.c 2.7 KB

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