bpf_local_storage.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2019 Facebook
  4. * Copyright 2020 Google LLC.
  5. */
  6. #ifndef _BPF_LOCAL_STORAGE_H
  7. #define _BPF_LOCAL_STORAGE_H
  8. #include <linux/bpf.h>
  9. #include <linux/filter.h>
  10. #include <linux/rculist.h>
  11. #include <linux/list.h>
  12. #include <linux/hash.h>
  13. #include <linux/types.h>
  14. #include <uapi/linux/btf.h>
  15. #define BPF_LOCAL_STORAGE_CACHE_SIZE 16
  16. #define bpf_rcu_lock_held() \
  17. (rcu_read_lock_held() || rcu_read_lock_trace_held() || \
  18. rcu_read_lock_bh_held())
  19. struct bpf_local_storage_map_bucket {
  20. struct hlist_head list;
  21. raw_spinlock_t lock;
  22. };
  23. /* Thp map is not the primary owner of a bpf_local_storage_elem.
  24. * Instead, the container object (eg. sk->sk_bpf_storage) is.
  25. *
  26. * The map (bpf_local_storage_map) is for two purposes
  27. * 1. Define the size of the "local storage". It is
  28. * the map's value_size.
  29. *
  30. * 2. Maintain a list to keep track of all elems such
  31. * that they can be cleaned up during the map destruction.
  32. *
  33. * When a bpf local storage is being looked up for a
  34. * particular object, the "bpf_map" pointer is actually used
  35. * as the "key" to search in the list of elem in
  36. * the respective bpf_local_storage owned by the object.
  37. *
  38. * e.g. sk->sk_bpf_storage is the mini-map with the "bpf_map" pointer
  39. * as the searching key.
  40. */
  41. struct bpf_local_storage_map {
  42. struct bpf_map map;
  43. /* Lookup elem does not require accessing the map.
  44. *
  45. * Updating/Deleting requires a bucket lock to
  46. * link/unlink the elem from the map. Having
  47. * multiple buckets to improve contention.
  48. */
  49. struct bpf_local_storage_map_bucket *buckets;
  50. u32 bucket_log;
  51. u16 elem_size;
  52. u16 cache_idx;
  53. };
  54. struct bpf_local_storage_data {
  55. /* smap is used as the searching key when looking up
  56. * from the object's bpf_local_storage.
  57. *
  58. * Put it in the same cacheline as the data to minimize
  59. * the number of cachelines accessed during the cache hit case.
  60. */
  61. struct bpf_local_storage_map __rcu *smap;
  62. u8 data[] __aligned(8);
  63. };
  64. /* Linked to bpf_local_storage and bpf_local_storage_map */
  65. struct bpf_local_storage_elem {
  66. struct hlist_node map_node; /* Linked to bpf_local_storage_map */
  67. struct hlist_node snode; /* Linked to bpf_local_storage */
  68. struct bpf_local_storage __rcu *local_storage;
  69. struct rcu_head rcu;
  70. /* 8 bytes hole */
  71. /* The data is stored in another cacheline to minimize
  72. * the number of cachelines access during a cache hit.
  73. */
  74. struct bpf_local_storage_data sdata ____cacheline_aligned;
  75. };
  76. struct bpf_local_storage {
  77. struct bpf_local_storage_data __rcu *cache[BPF_LOCAL_STORAGE_CACHE_SIZE];
  78. struct hlist_head list; /* List of bpf_local_storage_elem */
  79. void *owner; /* The object that owns the above "list" of
  80. * bpf_local_storage_elem.
  81. */
  82. struct rcu_head rcu;
  83. raw_spinlock_t lock; /* Protect adding/removing from the "list" */
  84. };
  85. /* U16_MAX is much more than enough for sk local storage
  86. * considering a tcp_sock is ~2k.
  87. */
  88. #define BPF_LOCAL_STORAGE_MAX_VALUE_SIZE \
  89. min_t(u32, \
  90. (KMALLOC_MAX_SIZE - MAX_BPF_STACK - \
  91. sizeof(struct bpf_local_storage_elem)), \
  92. (U16_MAX - sizeof(struct bpf_local_storage_elem)))
  93. #define SELEM(_SDATA) \
  94. container_of((_SDATA), struct bpf_local_storage_elem, sdata)
  95. #define SDATA(_SELEM) (&(_SELEM)->sdata)
  96. #define BPF_LOCAL_STORAGE_CACHE_SIZE 16
  97. struct bpf_local_storage_cache {
  98. spinlock_t idx_lock;
  99. u64 idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE];
  100. };
  101. #define DEFINE_BPF_STORAGE_CACHE(name) \
  102. static struct bpf_local_storage_cache name = { \
  103. .idx_lock = __SPIN_LOCK_UNLOCKED(name.idx_lock), \
  104. }
  105. u16 bpf_local_storage_cache_idx_get(struct bpf_local_storage_cache *cache);
  106. void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache,
  107. u16 idx);
  108. /* Helper functions for bpf_local_storage */
  109. int bpf_local_storage_map_alloc_check(union bpf_attr *attr);
  110. struct bpf_local_storage_map *bpf_local_storage_map_alloc(union bpf_attr *attr);
  111. struct bpf_local_storage_data *
  112. bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
  113. struct bpf_local_storage_map *smap,
  114. bool cacheit_lockit);
  115. void bpf_local_storage_map_free(struct bpf_local_storage_map *smap,
  116. int __percpu *busy_counter);
  117. int bpf_local_storage_map_check_btf(const struct bpf_map *map,
  118. const struct btf *btf,
  119. const struct btf_type *key_type,
  120. const struct btf_type *value_type);
  121. void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
  122. struct bpf_local_storage_elem *selem);
  123. bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage,
  124. struct bpf_local_storage_elem *selem,
  125. bool uncharge_omem, bool use_trace_rcu);
  126. void bpf_selem_unlink(struct bpf_local_storage_elem *selem, bool use_trace_rcu);
  127. void bpf_selem_link_map(struct bpf_local_storage_map *smap,
  128. struct bpf_local_storage_elem *selem);
  129. void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem);
  130. struct bpf_local_storage_elem *
  131. bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, void *value,
  132. bool charge_mem, gfp_t gfp_flags);
  133. int
  134. bpf_local_storage_alloc(void *owner,
  135. struct bpf_local_storage_map *smap,
  136. struct bpf_local_storage_elem *first_selem,
  137. gfp_t gfp_flags);
  138. struct bpf_local_storage_data *
  139. bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
  140. void *value, u64 map_flags, gfp_t gfp_flags);
  141. void bpf_local_storage_free_rcu(struct rcu_head *rcu);
  142. #endif /* _BPF_LOCAL_STORAGE_H */