map_iter.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2020 Facebook */
  3. #include <linux/bpf.h>
  4. #include <linux/fs.h>
  5. #include <linux/filter.h>
  6. #include <linux/kernel.h>
  7. #include <linux/btf_ids.h>
  8. struct bpf_iter_seq_map_info {
  9. u32 map_id;
  10. };
  11. static void *bpf_map_seq_start(struct seq_file *seq, loff_t *pos)
  12. {
  13. struct bpf_iter_seq_map_info *info = seq->private;
  14. struct bpf_map *map;
  15. map = bpf_map_get_curr_or_next(&info->map_id);
  16. if (!map)
  17. return NULL;
  18. if (*pos == 0)
  19. ++*pos;
  20. return map;
  21. }
  22. static void *bpf_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  23. {
  24. struct bpf_iter_seq_map_info *info = seq->private;
  25. ++*pos;
  26. ++info->map_id;
  27. bpf_map_put((struct bpf_map *)v);
  28. return bpf_map_get_curr_or_next(&info->map_id);
  29. }
  30. struct bpf_iter__bpf_map {
  31. __bpf_md_ptr(struct bpf_iter_meta *, meta);
  32. __bpf_md_ptr(struct bpf_map *, map);
  33. };
  34. DEFINE_BPF_ITER_FUNC(bpf_map, struct bpf_iter_meta *meta, struct bpf_map *map)
  35. static int __bpf_map_seq_show(struct seq_file *seq, void *v, bool in_stop)
  36. {
  37. struct bpf_iter__bpf_map ctx;
  38. struct bpf_iter_meta meta;
  39. struct bpf_prog *prog;
  40. int ret = 0;
  41. ctx.meta = &meta;
  42. ctx.map = v;
  43. meta.seq = seq;
  44. prog = bpf_iter_get_info(&meta, in_stop);
  45. if (prog)
  46. ret = bpf_iter_run_prog(prog, &ctx);
  47. return ret;
  48. }
  49. static int bpf_map_seq_show(struct seq_file *seq, void *v)
  50. {
  51. return __bpf_map_seq_show(seq, v, false);
  52. }
  53. static void bpf_map_seq_stop(struct seq_file *seq, void *v)
  54. {
  55. if (!v)
  56. (void)__bpf_map_seq_show(seq, v, true);
  57. else
  58. bpf_map_put((struct bpf_map *)v);
  59. }
  60. static const struct seq_operations bpf_map_seq_ops = {
  61. .start = bpf_map_seq_start,
  62. .next = bpf_map_seq_next,
  63. .stop = bpf_map_seq_stop,
  64. .show = bpf_map_seq_show,
  65. };
  66. BTF_ID_LIST_GLOBAL_SINGLE(btf_bpf_map_id, struct, bpf_map)
  67. static const struct bpf_iter_seq_info bpf_map_seq_info = {
  68. .seq_ops = &bpf_map_seq_ops,
  69. .init_seq_private = NULL,
  70. .fini_seq_private = NULL,
  71. .seq_priv_size = sizeof(struct bpf_iter_seq_map_info),
  72. };
  73. static struct bpf_iter_reg bpf_map_reg_info = {
  74. .target = "bpf_map",
  75. .ctx_arg_info_size = 1,
  76. .ctx_arg_info = {
  77. { offsetof(struct bpf_iter__bpf_map, map),
  78. PTR_TO_BTF_ID_OR_NULL | PTR_TRUSTED },
  79. },
  80. .seq_info = &bpf_map_seq_info,
  81. };
  82. static int bpf_iter_attach_map(struct bpf_prog *prog,
  83. union bpf_iter_link_info *linfo,
  84. struct bpf_iter_aux_info *aux)
  85. {
  86. u32 key_acc_size, value_acc_size, key_size, value_size;
  87. struct bpf_map *map;
  88. bool is_percpu = false;
  89. int err = -EINVAL;
  90. if (!linfo->map.map_fd)
  91. return -EBADF;
  92. map = bpf_map_get_with_uref(linfo->map.map_fd);
  93. if (IS_ERR(map))
  94. return PTR_ERR(map);
  95. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
  96. map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
  97. map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  98. is_percpu = true;
  99. else if (map->map_type != BPF_MAP_TYPE_HASH &&
  100. map->map_type != BPF_MAP_TYPE_LRU_HASH &&
  101. map->map_type != BPF_MAP_TYPE_ARRAY)
  102. goto put_map;
  103. key_acc_size = prog->aux->max_rdonly_access;
  104. value_acc_size = prog->aux->max_rdwr_access;
  105. key_size = map->key_size;
  106. if (!is_percpu)
  107. value_size = map->value_size;
  108. else
  109. value_size = round_up(map->value_size, 8) * num_possible_cpus();
  110. if (key_acc_size > key_size || value_acc_size > value_size) {
  111. err = -EACCES;
  112. goto put_map;
  113. }
  114. aux->map = map;
  115. return 0;
  116. put_map:
  117. bpf_map_put_with_uref(map);
  118. return err;
  119. }
  120. static void bpf_iter_detach_map(struct bpf_iter_aux_info *aux)
  121. {
  122. bpf_map_put_with_uref(aux->map);
  123. }
  124. void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
  125. struct seq_file *seq)
  126. {
  127. seq_printf(seq, "map_id:\t%u\n", aux->map->id);
  128. }
  129. int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
  130. struct bpf_link_info *info)
  131. {
  132. info->iter.map.map_id = aux->map->id;
  133. return 0;
  134. }
  135. DEFINE_BPF_ITER_FUNC(bpf_map_elem, struct bpf_iter_meta *meta,
  136. struct bpf_map *map, void *key, void *value)
  137. static const struct bpf_iter_reg bpf_map_elem_reg_info = {
  138. .target = "bpf_map_elem",
  139. .attach_target = bpf_iter_attach_map,
  140. .detach_target = bpf_iter_detach_map,
  141. .show_fdinfo = bpf_iter_map_show_fdinfo,
  142. .fill_link_info = bpf_iter_map_fill_link_info,
  143. .ctx_arg_info_size = 2,
  144. .ctx_arg_info = {
  145. { offsetof(struct bpf_iter__bpf_map_elem, key),
  146. PTR_TO_BUF | PTR_MAYBE_NULL | MEM_RDONLY },
  147. { offsetof(struct bpf_iter__bpf_map_elem, value),
  148. PTR_TO_BUF | PTR_MAYBE_NULL },
  149. },
  150. };
  151. static int __init bpf_map_iter_init(void)
  152. {
  153. int ret;
  154. bpf_map_reg_info.ctx_arg_info[0].btf_id = *btf_bpf_map_id;
  155. ret = bpf_iter_reg_target(&bpf_map_reg_info);
  156. if (ret)
  157. return ret;
  158. return bpf_iter_reg_target(&bpf_map_elem_reg_info);
  159. }
  160. late_initcall(bpf_map_iter_init);
  161. __bpf_kfunc_start_defs();
  162. __bpf_kfunc s64 bpf_map_sum_elem_count(const struct bpf_map *map)
  163. {
  164. s64 *pcount;
  165. s64 ret = 0;
  166. int cpu;
  167. if (!map || !map->elem_count)
  168. return 0;
  169. for_each_possible_cpu(cpu) {
  170. pcount = per_cpu_ptr(map->elem_count, cpu);
  171. ret += READ_ONCE(*pcount);
  172. }
  173. return ret;
  174. }
  175. __bpf_kfunc_end_defs();
  176. BTF_KFUNCS_START(bpf_map_iter_kfunc_ids)
  177. BTF_ID_FLAGS(func, bpf_map_sum_elem_count, KF_TRUSTED_ARGS)
  178. BTF_KFUNCS_END(bpf_map_iter_kfunc_ids)
  179. static const struct btf_kfunc_id_set bpf_map_iter_kfunc_set = {
  180. .owner = THIS_MODULE,
  181. .set = &bpf_map_iter_kfunc_ids,
  182. };
  183. static int init_subsystem(void)
  184. {
  185. return register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &bpf_map_iter_kfunc_set);
  186. }
  187. late_initcall(init_subsystem);