map_in_map.c 3.3 KB

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