hashtab.c 9.6 KB

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