map_in_map.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. CLASS(fd, f)(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. return ERR_PTR(-EINVAL);
  19. if (!inner_map->ops->map_meta_equal)
  20. return ERR_PTR(-ENOTSUPP);
  21. inner_map_meta_size = sizeof(*inner_map_meta);
  22. /* In some cases verifier needs to access beyond just base map. */
  23. if (inner_map->ops == &array_map_ops || inner_map->ops == &percpu_array_map_ops)
  24. inner_map_meta_size = sizeof(struct bpf_array);
  25. inner_map_meta = kzalloc(inner_map_meta_size, GFP_USER);
  26. if (!inner_map_meta)
  27. return ERR_PTR(-ENOMEM);
  28. inner_map_meta->map_type = inner_map->map_type;
  29. inner_map_meta->key_size = inner_map->key_size;
  30. inner_map_meta->value_size = inner_map->value_size;
  31. inner_map_meta->map_flags = inner_map->map_flags;
  32. inner_map_meta->max_entries = inner_map->max_entries;
  33. inner_map_meta->record = btf_record_dup(inner_map->record);
  34. if (IS_ERR(inner_map_meta->record)) {
  35. /* btf_record_dup returns NULL or valid pointer in case of
  36. * invalid/empty/valid, but ERR_PTR in case of errors. During
  37. * equality NULL or IS_ERR is equivalent.
  38. */
  39. struct bpf_map *ret = ERR_CAST(inner_map_meta->record);
  40. kfree(inner_map_meta);
  41. return ret;
  42. }
  43. /* Note: We must use the same BTF, as we also used btf_record_dup above
  44. * which relies on BTF being same for both maps, as some members like
  45. * record->fields.list_head have pointers like value_rec pointing into
  46. * inner_map->btf.
  47. */
  48. if (inner_map->btf) {
  49. btf_get(inner_map->btf);
  50. inner_map_meta->btf = inner_map->btf;
  51. }
  52. /* Misc members not needed in bpf_map_meta_equal() check. */
  53. inner_map_meta->ops = inner_map->ops;
  54. if (inner_map->ops == &array_map_ops || inner_map->ops == &percpu_array_map_ops) {
  55. struct bpf_array *inner_array_meta =
  56. container_of(inner_map_meta, struct bpf_array, map);
  57. struct bpf_array *inner_array = container_of(inner_map, struct bpf_array, map);
  58. inner_array_meta->index_mask = inner_array->index_mask;
  59. inner_array_meta->elem_size = inner_array->elem_size;
  60. inner_map_meta->bypass_spec_v1 = inner_map->bypass_spec_v1;
  61. }
  62. return inner_map_meta;
  63. }
  64. void bpf_map_meta_free(struct bpf_map *map_meta)
  65. {
  66. bpf_map_free_record(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->map_flags == meta1->map_flags &&
  78. btf_record_equal(meta0->record, meta1->record);
  79. }
  80. void *bpf_map_fd_get_ptr(struct bpf_map *map,
  81. struct file *map_file /* not used */,
  82. int ufd)
  83. {
  84. struct bpf_map *inner_map, *inner_map_meta;
  85. CLASS(fd, f)(ufd);
  86. inner_map = __bpf_map_get(f);
  87. if (IS_ERR(inner_map))
  88. return inner_map;
  89. inner_map_meta = map->inner_map_meta;
  90. if (inner_map_meta->ops->map_meta_equal(inner_map_meta, inner_map))
  91. bpf_map_inc(inner_map);
  92. else
  93. inner_map = ERR_PTR(-EINVAL);
  94. return inner_map;
  95. }
  96. void bpf_map_fd_put_ptr(struct bpf_map *map, void *ptr, bool need_defer)
  97. {
  98. struct bpf_map *inner_map = ptr;
  99. /* Defer the freeing of inner map according to the sleepable attribute
  100. * of bpf program which owns the outer map, so unnecessary waiting for
  101. * RCU tasks trace grace period can be avoided.
  102. */
  103. if (need_defer) {
  104. if (atomic64_read(&map->sleepable_refcnt))
  105. WRITE_ONCE(inner_map->free_after_mult_rcu_gp, true);
  106. else
  107. WRITE_ONCE(inner_map->free_after_rcu_gp, true);
  108. }
  109. bpf_map_put(inner_map);
  110. }
  111. u32 bpf_map_fd_sys_lookup_elem(void *ptr)
  112. {
  113. return ((struct bpf_map *)ptr)->id;
  114. }