map_in_map.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (c) 2017 Facebook
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/bpf.h>
  9. #include "map_in_map.h"
  10. struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
  11. {
  12. struct bpf_map *inner_map, *inner_map_meta;
  13. struct fd f;
  14. f = fdget(inner_map_ufd);
  15. inner_map = __bpf_map_get(f);
  16. if (IS_ERR(inner_map))
  17. return inner_map;
  18. /* prog_array->owner_prog_type and owner_jited
  19. * is a runtime binding. Doing static check alone
  20. * in the verifier is not enough.
  21. */
  22. if (inner_map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
  23. fdput(f);
  24. return ERR_PTR(-ENOTSUPP);
  25. }
  26. /* Does not support >1 level map-in-map */
  27. if (inner_map->inner_map_meta) {
  28. fdput(f);
  29. return ERR_PTR(-EINVAL);
  30. }
  31. inner_map_meta = kzalloc(sizeof(*inner_map_meta), GFP_USER);
  32. if (!inner_map_meta) {
  33. fdput(f);
  34. return ERR_PTR(-ENOMEM);
  35. }
  36. inner_map_meta->map_type = inner_map->map_type;
  37. inner_map_meta->key_size = inner_map->key_size;
  38. inner_map_meta->value_size = inner_map->value_size;
  39. inner_map_meta->map_flags = inner_map->map_flags;
  40. inner_map_meta->ops = inner_map->ops;
  41. inner_map_meta->max_entries = inner_map->max_entries;
  42. fdput(f);
  43. return inner_map_meta;
  44. }
  45. void bpf_map_meta_free(struct bpf_map *map_meta)
  46. {
  47. kfree(map_meta);
  48. }
  49. bool bpf_map_meta_equal(const struct bpf_map *meta0,
  50. const struct bpf_map *meta1)
  51. {
  52. /* No need to compare ops because it is covered by map_type */
  53. return meta0->map_type == meta1->map_type &&
  54. meta0->key_size == meta1->key_size &&
  55. meta0->value_size == meta1->value_size &&
  56. meta0->map_flags == meta1->map_flags &&
  57. meta0->max_entries == meta1->max_entries;
  58. }
  59. void *bpf_map_fd_get_ptr(struct bpf_map *map,
  60. struct file *map_file /* not used */,
  61. int ufd)
  62. {
  63. struct bpf_map *inner_map;
  64. struct fd f;
  65. f = fdget(ufd);
  66. inner_map = __bpf_map_get(f);
  67. if (IS_ERR(inner_map))
  68. return inner_map;
  69. if (bpf_map_meta_equal(map->inner_map_meta, inner_map))
  70. inner_map = bpf_map_inc(inner_map, false);
  71. else
  72. inner_map = ERR_PTR(-EINVAL);
  73. fdput(f);
  74. return inner_map;
  75. }
  76. void bpf_map_fd_put_ptr(void *ptr)
  77. {
  78. /* ptr->ops->map_free() has to go through one
  79. * rcu grace period by itself.
  80. */
  81. bpf_map_put(ptr);
  82. }
  83. u32 bpf_map_fd_sys_lookup_elem(void *ptr)
  84. {
  85. return ((struct bpf_map *)ptr)->id;
  86. }