hashtab.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  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. * This program is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * General Public License for more details.
  11. */
  12. #include <linux/bpf.h>
  13. #include <linux/jhash.h>
  14. #include <linux/filter.h>
  15. #include <linux/vmalloc.h>
  16. struct bpf_htab {
  17. struct bpf_map map;
  18. struct hlist_head *buckets;
  19. spinlock_t lock;
  20. u32 count; /* number of elements in this hashtable */
  21. u32 n_buckets; /* number of hash buckets */
  22. u32 elem_size; /* size of each element in bytes */
  23. };
  24. /* each htab element is struct htab_elem + key + value */
  25. struct htab_elem {
  26. struct hlist_node hash_node;
  27. struct rcu_head rcu;
  28. u32 hash;
  29. char key[0] __aligned(8);
  30. };
  31. /* Called from syscall */
  32. static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
  33. {
  34. struct bpf_htab *htab;
  35. int err, i;
  36. htab = kzalloc(sizeof(*htab), GFP_USER);
  37. if (!htab)
  38. return ERR_PTR(-ENOMEM);
  39. /* mandatory map attributes */
  40. htab->map.key_size = attr->key_size;
  41. htab->map.value_size = attr->value_size;
  42. htab->map.max_entries = attr->max_entries;
  43. /* check sanity of attributes.
  44. * value_size == 0 may be allowed in the future to use map as a set
  45. */
  46. err = -EINVAL;
  47. if (htab->map.max_entries == 0 || htab->map.key_size == 0 ||
  48. htab->map.value_size == 0)
  49. goto free_htab;
  50. /* hash table size must be power of 2 */
  51. htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
  52. err = -E2BIG;
  53. if (htab->map.key_size > MAX_BPF_STACK)
  54. /* eBPF programs initialize keys on stack, so they cannot be
  55. * larger than max stack size
  56. */
  57. goto free_htab;
  58. err = -ENOMEM;
  59. /* prevent zero size kmalloc and check for u32 overflow */
  60. if (htab->n_buckets == 0 ||
  61. htab->n_buckets > U32_MAX / sizeof(struct hlist_head))
  62. goto free_htab;
  63. htab->buckets = kmalloc_array(htab->n_buckets, sizeof(struct hlist_head),
  64. GFP_USER | __GFP_NOWARN);
  65. if (!htab->buckets) {
  66. htab->buckets = vmalloc(htab->n_buckets * sizeof(struct hlist_head));
  67. if (!htab->buckets)
  68. goto free_htab;
  69. }
  70. for (i = 0; i < htab->n_buckets; i++)
  71. INIT_HLIST_HEAD(&htab->buckets[i]);
  72. spin_lock_init(&htab->lock);
  73. htab->count = 0;
  74. htab->elem_size = sizeof(struct htab_elem) +
  75. round_up(htab->map.key_size, 8) +
  76. htab->map.value_size;
  77. return &htab->map;
  78. free_htab:
  79. kfree(htab);
  80. return ERR_PTR(err);
  81. }
  82. static inline u32 htab_map_hash(const void *key, u32 key_len)
  83. {
  84. return jhash(key, key_len, 0);
  85. }
  86. static inline struct hlist_head *select_bucket(struct bpf_htab *htab, u32 hash)
  87. {
  88. return &htab->buckets[hash & (htab->n_buckets - 1)];
  89. }
  90. static struct htab_elem *lookup_elem_raw(struct hlist_head *head, u32 hash,
  91. void *key, u32 key_size)
  92. {
  93. struct htab_elem *l;
  94. hlist_for_each_entry_rcu(l, head, hash_node)
  95. if (l->hash == hash && !memcmp(&l->key, key, key_size))
  96. return l;
  97. return NULL;
  98. }
  99. /* Called from syscall or from eBPF program */
  100. static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
  101. {
  102. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  103. struct hlist_head *head;
  104. struct htab_elem *l;
  105. u32 hash, key_size;
  106. /* Must be called with rcu_read_lock. */
  107. WARN_ON_ONCE(!rcu_read_lock_held());
  108. key_size = map->key_size;
  109. hash = htab_map_hash(key, key_size);
  110. head = select_bucket(htab, hash);
  111. l = lookup_elem_raw(head, hash, key, key_size);
  112. if (l)
  113. return l->key + round_up(map->key_size, 8);
  114. return NULL;
  115. }
  116. /* Called from syscall */
  117. static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  118. {
  119. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  120. struct hlist_head *head;
  121. struct htab_elem *l, *next_l;
  122. u32 hash, key_size;
  123. int i;
  124. WARN_ON_ONCE(!rcu_read_lock_held());
  125. key_size = map->key_size;
  126. hash = htab_map_hash(key, key_size);
  127. head = select_bucket(htab, hash);
  128. /* lookup the key */
  129. l = lookup_elem_raw(head, hash, key, key_size);
  130. if (!l) {
  131. i = 0;
  132. goto find_first_elem;
  133. }
  134. /* key was found, get next key in the same bucket */
  135. next_l = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&l->hash_node)),
  136. struct htab_elem, hash_node);
  137. if (next_l) {
  138. /* if next elem in this hash list is non-zero, just return it */
  139. memcpy(next_key, next_l->key, key_size);
  140. return 0;
  141. }
  142. /* no more elements in this hash list, go to the next bucket */
  143. i = hash & (htab->n_buckets - 1);
  144. i++;
  145. find_first_elem:
  146. /* iterate over buckets */
  147. for (; i < htab->n_buckets; i++) {
  148. head = select_bucket(htab, i);
  149. /* pick first element in the bucket */
  150. next_l = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
  151. struct htab_elem, hash_node);
  152. if (next_l) {
  153. /* if it's not empty, just return it */
  154. memcpy(next_key, next_l->key, key_size);
  155. return 0;
  156. }
  157. }
  158. /* itereated over all buckets and all elements */
  159. return -ENOENT;
  160. }
  161. /* Called from syscall or from eBPF program */
  162. static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
  163. u64 map_flags)
  164. {
  165. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  166. struct htab_elem *l_new, *l_old;
  167. struct hlist_head *head;
  168. unsigned long flags;
  169. u32 key_size;
  170. int ret;
  171. if (map_flags > BPF_EXIST)
  172. /* unknown flags */
  173. return -EINVAL;
  174. WARN_ON_ONCE(!rcu_read_lock_held());
  175. /* allocate new element outside of lock */
  176. l_new = kmalloc(htab->elem_size, GFP_ATOMIC);
  177. if (!l_new)
  178. return -ENOMEM;
  179. key_size = map->key_size;
  180. memcpy(l_new->key, key, key_size);
  181. memcpy(l_new->key + round_up(key_size, 8), value, map->value_size);
  182. l_new->hash = htab_map_hash(l_new->key, key_size);
  183. /* bpf_map_update_elem() can be called in_irq() */
  184. spin_lock_irqsave(&htab->lock, flags);
  185. head = select_bucket(htab, l_new->hash);
  186. l_old = lookup_elem_raw(head, l_new->hash, key, key_size);
  187. if (!l_old && unlikely(htab->count >= map->max_entries)) {
  188. /* if elem with this 'key' doesn't exist and we've reached
  189. * max_entries limit, fail insertion of new elem
  190. */
  191. ret = -E2BIG;
  192. goto err;
  193. }
  194. if (l_old && map_flags == BPF_NOEXIST) {
  195. /* elem already exists */
  196. ret = -EEXIST;
  197. goto err;
  198. }
  199. if (!l_old && map_flags == BPF_EXIST) {
  200. /* elem doesn't exist, cannot update it */
  201. ret = -ENOENT;
  202. goto err;
  203. }
  204. /* add new element to the head of the list, so that concurrent
  205. * search will find it before old elem
  206. */
  207. hlist_add_head_rcu(&l_new->hash_node, head);
  208. if (l_old) {
  209. hlist_del_rcu(&l_old->hash_node);
  210. kfree_rcu(l_old, rcu);
  211. } else {
  212. htab->count++;
  213. }
  214. spin_unlock_irqrestore(&htab->lock, flags);
  215. return 0;
  216. err:
  217. spin_unlock_irqrestore(&htab->lock, flags);
  218. kfree(l_new);
  219. return ret;
  220. }
  221. /* Called from syscall or from eBPF program */
  222. static int htab_map_delete_elem(struct bpf_map *map, void *key)
  223. {
  224. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  225. struct hlist_head *head;
  226. struct htab_elem *l;
  227. unsigned long flags;
  228. u32 hash, key_size;
  229. int ret = -ENOENT;
  230. WARN_ON_ONCE(!rcu_read_lock_held());
  231. key_size = map->key_size;
  232. hash = htab_map_hash(key, key_size);
  233. spin_lock_irqsave(&htab->lock, flags);
  234. head = select_bucket(htab, hash);
  235. l = lookup_elem_raw(head, hash, key, key_size);
  236. if (l) {
  237. hlist_del_rcu(&l->hash_node);
  238. htab->count--;
  239. kfree_rcu(l, rcu);
  240. ret = 0;
  241. }
  242. spin_unlock_irqrestore(&htab->lock, flags);
  243. return ret;
  244. }
  245. static void delete_all_elements(struct bpf_htab *htab)
  246. {
  247. int i;
  248. for (i = 0; i < htab->n_buckets; i++) {
  249. struct hlist_head *head = select_bucket(htab, i);
  250. struct hlist_node *n;
  251. struct htab_elem *l;
  252. hlist_for_each_entry_safe(l, n, head, hash_node) {
  253. hlist_del_rcu(&l->hash_node);
  254. htab->count--;
  255. kfree(l);
  256. }
  257. }
  258. }
  259. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  260. static void htab_map_free(struct bpf_map *map)
  261. {
  262. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  263. /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  264. * so the programs (can be more than one that used this map) were
  265. * disconnected from events. Wait for outstanding critical sections in
  266. * these programs to complete
  267. */
  268. synchronize_rcu();
  269. /* some of kfree_rcu() callbacks for elements of this map may not have
  270. * executed. It's ok. Proceed to free residual elements and map itself
  271. */
  272. delete_all_elements(htab);
  273. kvfree(htab->buckets);
  274. kfree(htab);
  275. }
  276. static const struct bpf_map_ops htab_ops = {
  277. .map_alloc = htab_map_alloc,
  278. .map_free = htab_map_free,
  279. .map_get_next_key = htab_map_get_next_key,
  280. .map_lookup_elem = htab_map_lookup_elem,
  281. .map_update_elem = htab_map_update_elem,
  282. .map_delete_elem = htab_map_delete_elem,
  283. };
  284. static struct bpf_map_type_list htab_type __read_mostly = {
  285. .ops = &htab_ops,
  286. .type = BPF_MAP_TYPE_HASH,
  287. };
  288. static int __init register_htab_map(void)
  289. {
  290. bpf_register_map_type(&htab_type);
  291. return 0;
  292. }
  293. late_initcall(register_htab_map);