hashtab.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. raw_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. raw_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. htab->map.pages = round_up(htab->n_buckets * sizeof(struct hlist_head) +
  78. htab->elem_size * htab->map.max_entries,
  79. PAGE_SIZE) >> PAGE_SHIFT;
  80. return &htab->map;
  81. free_htab:
  82. kfree(htab);
  83. return ERR_PTR(err);
  84. }
  85. static inline u32 htab_map_hash(const void *key, u32 key_len)
  86. {
  87. return jhash(key, key_len, 0);
  88. }
  89. static inline struct hlist_head *select_bucket(struct bpf_htab *htab, u32 hash)
  90. {
  91. return &htab->buckets[hash & (htab->n_buckets - 1)];
  92. }
  93. static struct htab_elem *lookup_elem_raw(struct hlist_head *head, u32 hash,
  94. void *key, u32 key_size)
  95. {
  96. struct htab_elem *l;
  97. hlist_for_each_entry_rcu(l, head, hash_node)
  98. if (l->hash == hash && !memcmp(&l->key, key, key_size))
  99. return l;
  100. return NULL;
  101. }
  102. /* Called from syscall or from eBPF program */
  103. static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
  104. {
  105. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  106. struct hlist_head *head;
  107. struct htab_elem *l;
  108. u32 hash, key_size;
  109. /* Must be called with rcu_read_lock. */
  110. WARN_ON_ONCE(!rcu_read_lock_held());
  111. key_size = map->key_size;
  112. hash = htab_map_hash(key, key_size);
  113. head = select_bucket(htab, hash);
  114. l = lookup_elem_raw(head, hash, key, key_size);
  115. if (l)
  116. return l->key + round_up(map->key_size, 8);
  117. return NULL;
  118. }
  119. /* Called from syscall */
  120. static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  121. {
  122. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  123. struct hlist_head *head;
  124. struct htab_elem *l, *next_l;
  125. u32 hash, key_size;
  126. int i;
  127. WARN_ON_ONCE(!rcu_read_lock_held());
  128. key_size = map->key_size;
  129. hash = htab_map_hash(key, key_size);
  130. head = select_bucket(htab, hash);
  131. /* lookup the key */
  132. l = lookup_elem_raw(head, hash, key, key_size);
  133. if (!l) {
  134. i = 0;
  135. goto find_first_elem;
  136. }
  137. /* key was found, get next key in the same bucket */
  138. next_l = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&l->hash_node)),
  139. struct htab_elem, hash_node);
  140. if (next_l) {
  141. /* if next elem in this hash list is non-zero, just return it */
  142. memcpy(next_key, next_l->key, key_size);
  143. return 0;
  144. }
  145. /* no more elements in this hash list, go to the next bucket */
  146. i = hash & (htab->n_buckets - 1);
  147. i++;
  148. find_first_elem:
  149. /* iterate over buckets */
  150. for (; i < htab->n_buckets; i++) {
  151. head = select_bucket(htab, i);
  152. /* pick first element in the bucket */
  153. next_l = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
  154. struct htab_elem, hash_node);
  155. if (next_l) {
  156. /* if it's not empty, just return it */
  157. memcpy(next_key, next_l->key, key_size);
  158. return 0;
  159. }
  160. }
  161. /* itereated over all buckets and all elements */
  162. return -ENOENT;
  163. }
  164. /* Called from syscall or from eBPF program */
  165. static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
  166. u64 map_flags)
  167. {
  168. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  169. struct htab_elem *l_new, *l_old;
  170. struct hlist_head *head;
  171. unsigned long flags;
  172. u32 key_size;
  173. int ret;
  174. if (map_flags > BPF_EXIST)
  175. /* unknown flags */
  176. return -EINVAL;
  177. WARN_ON_ONCE(!rcu_read_lock_held());
  178. /* allocate new element outside of lock */
  179. l_new = kmalloc(htab->elem_size, GFP_ATOMIC);
  180. if (!l_new)
  181. return -ENOMEM;
  182. key_size = map->key_size;
  183. memcpy(l_new->key, key, key_size);
  184. memcpy(l_new->key + round_up(key_size, 8), value, map->value_size);
  185. l_new->hash = htab_map_hash(l_new->key, key_size);
  186. /* bpf_map_update_elem() can be called in_irq() */
  187. raw_spin_lock_irqsave(&htab->lock, flags);
  188. head = select_bucket(htab, l_new->hash);
  189. l_old = lookup_elem_raw(head, l_new->hash, key, key_size);
  190. if (!l_old && unlikely(htab->count >= map->max_entries)) {
  191. /* if elem with this 'key' doesn't exist and we've reached
  192. * max_entries limit, fail insertion of new elem
  193. */
  194. ret = -E2BIG;
  195. goto err;
  196. }
  197. if (l_old && map_flags == BPF_NOEXIST) {
  198. /* elem already exists */
  199. ret = -EEXIST;
  200. goto err;
  201. }
  202. if (!l_old && map_flags == BPF_EXIST) {
  203. /* elem doesn't exist, cannot update it */
  204. ret = -ENOENT;
  205. goto err;
  206. }
  207. /* add new element to the head of the list, so that concurrent
  208. * search will find it before old elem
  209. */
  210. hlist_add_head_rcu(&l_new->hash_node, head);
  211. if (l_old) {
  212. hlist_del_rcu(&l_old->hash_node);
  213. kfree_rcu(l_old, rcu);
  214. } else {
  215. htab->count++;
  216. }
  217. raw_spin_unlock_irqrestore(&htab->lock, flags);
  218. return 0;
  219. err:
  220. raw_spin_unlock_irqrestore(&htab->lock, flags);
  221. kfree(l_new);
  222. return ret;
  223. }
  224. /* Called from syscall or from eBPF program */
  225. static int htab_map_delete_elem(struct bpf_map *map, void *key)
  226. {
  227. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  228. struct hlist_head *head;
  229. struct htab_elem *l;
  230. unsigned long flags;
  231. u32 hash, key_size;
  232. int ret = -ENOENT;
  233. WARN_ON_ONCE(!rcu_read_lock_held());
  234. key_size = map->key_size;
  235. hash = htab_map_hash(key, key_size);
  236. raw_spin_lock_irqsave(&htab->lock, flags);
  237. head = select_bucket(htab, hash);
  238. l = lookup_elem_raw(head, hash, key, key_size);
  239. if (l) {
  240. hlist_del_rcu(&l->hash_node);
  241. htab->count--;
  242. kfree_rcu(l, rcu);
  243. ret = 0;
  244. }
  245. raw_spin_unlock_irqrestore(&htab->lock, flags);
  246. return ret;
  247. }
  248. static void delete_all_elements(struct bpf_htab *htab)
  249. {
  250. int i;
  251. for (i = 0; i < htab->n_buckets; i++) {
  252. struct hlist_head *head = select_bucket(htab, i);
  253. struct hlist_node *n;
  254. struct htab_elem *l;
  255. hlist_for_each_entry_safe(l, n, head, hash_node) {
  256. hlist_del_rcu(&l->hash_node);
  257. htab->count--;
  258. kfree(l);
  259. }
  260. }
  261. }
  262. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  263. static void htab_map_free(struct bpf_map *map)
  264. {
  265. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  266. /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  267. * so the programs (can be more than one that used this map) were
  268. * disconnected from events. Wait for outstanding critical sections in
  269. * these programs to complete
  270. */
  271. synchronize_rcu();
  272. /* some of kfree_rcu() callbacks for elements of this map may not have
  273. * executed. It's ok. Proceed to free residual elements and map itself
  274. */
  275. delete_all_elements(htab);
  276. kvfree(htab->buckets);
  277. kfree(htab);
  278. }
  279. static const struct bpf_map_ops htab_ops = {
  280. .map_alloc = htab_map_alloc,
  281. .map_free = htab_map_free,
  282. .map_get_next_key = htab_map_get_next_key,
  283. .map_lookup_elem = htab_map_lookup_elem,
  284. .map_update_elem = htab_map_update_elem,
  285. .map_delete_elem = htab_map_delete_elem,
  286. };
  287. static struct bpf_map_type_list htab_type __read_mostly = {
  288. .ops = &htab_ops,
  289. .type = BPF_MAP_TYPE_HASH,
  290. };
  291. static int __init register_htab_map(void)
  292. {
  293. bpf_register_map_type(&htab_type);
  294. return 0;
  295. }
  296. late_initcall(register_htab_map);