map_in_map.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. inner_map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE ||
  24. inner_map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
  25. fdput(f);
  26. return ERR_PTR(-ENOTSUPP);
  27. }
  28. /* Does not support >1 level map-in-map */
  29. if (inner_map->inner_map_meta) {
  30. fdput(f);
  31. return ERR_PTR(-EINVAL);
  32. }
  33. inner_map_meta = kzalloc(sizeof(*inner_map_meta), 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->ops = inner_map->ops;
  43. inner_map_meta->max_entries = inner_map->max_entries;
  44. fdput(f);
  45. return inner_map_meta;
  46. }
  47. void bpf_map_meta_free(struct bpf_map *map_meta)
  48. {
  49. kfree(map_meta);
  50. }
  51. bool bpf_map_meta_equal(const struct bpf_map *meta0,
  52. const struct bpf_map *meta1)
  53. {
  54. /* No need to compare ops because it is covered by map_type */
  55. return meta0->map_type == meta1->map_type &&
  56. meta0->key_size == meta1->key_size &&
  57. meta0->value_size == meta1->value_size &&
  58. meta0->map_flags == meta1->map_flags &&
  59. meta0->max_entries == meta1->max_entries;
  60. }
  61. void *bpf_map_fd_get_ptr(struct bpf_map *map,
  62. struct file *map_file /* not used */,
  63. int ufd)
  64. {
  65. struct bpf_map *inner_map;
  66. struct fd f;
  67. f = fdget(ufd);
  68. inner_map = __bpf_map_get(f);
  69. if (IS_ERR(inner_map))
  70. return inner_map;
  71. if (bpf_map_meta_equal(map->inner_map_meta, inner_map))
  72. inner_map = bpf_map_inc(inner_map, false);
  73. else
  74. inner_map = ERR_PTR(-EINVAL);
  75. fdput(f);
  76. return inner_map;
  77. }
  78. void bpf_map_fd_put_ptr(void *ptr)
  79. {
  80. /* ptr->ops->map_free() has to go through one
  81. * rcu grace period by itself.
  82. */
  83. bpf_map_put(ptr);
  84. }
  85. u32 bpf_map_fd_sys_lookup_elem(void *ptr)
  86. {
  87. return ((struct bpf_map *)ptr)->id;
  88. }