hashtab.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  2. * Copyright (c) 2016 Facebook
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include <linux/bpf.h>
  14. #include <linux/jhash.h>
  15. #include <linux/filter.h>
  16. #include "percpu_freelist.h"
  17. struct bucket {
  18. struct hlist_head head;
  19. raw_spinlock_t lock;
  20. };
  21. struct bpf_htab {
  22. struct bpf_map map;
  23. struct bucket *buckets;
  24. void *elems;
  25. struct pcpu_freelist freelist;
  26. void __percpu *extra_elems;
  27. atomic_t count; /* number of elements in this hashtable */
  28. u32 n_buckets; /* number of hash buckets */
  29. u32 elem_size; /* size of each element in bytes */
  30. };
  31. enum extra_elem_state {
  32. HTAB_NOT_AN_EXTRA_ELEM = 0,
  33. HTAB_EXTRA_ELEM_FREE,
  34. HTAB_EXTRA_ELEM_USED
  35. };
  36. /* each htab element is struct htab_elem + key + value */
  37. struct htab_elem {
  38. union {
  39. struct hlist_node hash_node;
  40. struct bpf_htab *htab;
  41. struct pcpu_freelist_node fnode;
  42. };
  43. union {
  44. struct rcu_head rcu;
  45. enum extra_elem_state state;
  46. };
  47. u32 hash;
  48. char key[0] __aligned(8);
  49. };
  50. static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
  51. void __percpu *pptr)
  52. {
  53. *(void __percpu **)(l->key + key_size) = pptr;
  54. }
  55. static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
  56. {
  57. return *(void __percpu **)(l->key + key_size);
  58. }
  59. static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
  60. {
  61. return (struct htab_elem *) (htab->elems + i * htab->elem_size);
  62. }
  63. static void htab_free_elems(struct bpf_htab *htab)
  64. {
  65. int i;
  66. if (htab->map.map_type != BPF_MAP_TYPE_PERCPU_HASH)
  67. goto free_elems;
  68. for (i = 0; i < htab->map.max_entries; i++) {
  69. void __percpu *pptr;
  70. pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
  71. htab->map.key_size);
  72. free_percpu(pptr);
  73. }
  74. free_elems:
  75. bpf_map_area_free(htab->elems);
  76. }
  77. static int prealloc_elems_and_freelist(struct bpf_htab *htab)
  78. {
  79. int err = -ENOMEM, i;
  80. htab->elems = bpf_map_area_alloc(htab->elem_size *
  81. htab->map.max_entries);
  82. if (!htab->elems)
  83. return -ENOMEM;
  84. if (htab->map.map_type != BPF_MAP_TYPE_PERCPU_HASH)
  85. goto skip_percpu_elems;
  86. for (i = 0; i < htab->map.max_entries; i++) {
  87. u32 size = round_up(htab->map.value_size, 8);
  88. void __percpu *pptr;
  89. pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
  90. if (!pptr)
  91. goto free_elems;
  92. htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
  93. pptr);
  94. }
  95. skip_percpu_elems:
  96. err = pcpu_freelist_init(&htab->freelist);
  97. if (err)
  98. goto free_elems;
  99. pcpu_freelist_populate(&htab->freelist, htab->elems, htab->elem_size,
  100. htab->map.max_entries);
  101. return 0;
  102. free_elems:
  103. htab_free_elems(htab);
  104. return err;
  105. }
  106. static int alloc_extra_elems(struct bpf_htab *htab)
  107. {
  108. void __percpu *pptr;
  109. int cpu;
  110. pptr = __alloc_percpu_gfp(htab->elem_size, 8, GFP_USER | __GFP_NOWARN);
  111. if (!pptr)
  112. return -ENOMEM;
  113. for_each_possible_cpu(cpu) {
  114. ((struct htab_elem *)per_cpu_ptr(pptr, cpu))->state =
  115. HTAB_EXTRA_ELEM_FREE;
  116. }
  117. htab->extra_elems = pptr;
  118. return 0;
  119. }
  120. /* Called from syscall */
  121. static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
  122. {
  123. bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_HASH;
  124. struct bpf_htab *htab;
  125. int err, i;
  126. u64 cost;
  127. if (attr->map_flags & ~BPF_F_NO_PREALLOC)
  128. /* reserved bits should not be used */
  129. return ERR_PTR(-EINVAL);
  130. htab = kzalloc(sizeof(*htab), GFP_USER);
  131. if (!htab)
  132. return ERR_PTR(-ENOMEM);
  133. /* mandatory map attributes */
  134. htab->map.map_type = attr->map_type;
  135. htab->map.key_size = attr->key_size;
  136. htab->map.value_size = attr->value_size;
  137. htab->map.max_entries = attr->max_entries;
  138. htab->map.map_flags = attr->map_flags;
  139. /* check sanity of attributes.
  140. * value_size == 0 may be allowed in the future to use map as a set
  141. */
  142. err = -EINVAL;
  143. if (htab->map.max_entries == 0 || htab->map.key_size == 0 ||
  144. htab->map.value_size == 0)
  145. goto free_htab;
  146. /* hash table size must be power of 2 */
  147. htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
  148. err = -E2BIG;
  149. if (htab->map.key_size > MAX_BPF_STACK)
  150. /* eBPF programs initialize keys on stack, so they cannot be
  151. * larger than max stack size
  152. */
  153. goto free_htab;
  154. if (htab->map.value_size >= (1 << (KMALLOC_SHIFT_MAX - 1)) -
  155. MAX_BPF_STACK - sizeof(struct htab_elem))
  156. /* if value_size is bigger, the user space won't be able to
  157. * access the elements via bpf syscall. This check also makes
  158. * sure that the elem_size doesn't overflow and it's
  159. * kmalloc-able later in htab_map_update_elem()
  160. */
  161. goto free_htab;
  162. if (percpu && round_up(htab->map.value_size, 8) > PCPU_MIN_UNIT_SIZE)
  163. /* make sure the size for pcpu_alloc() is reasonable */
  164. goto free_htab;
  165. htab->elem_size = sizeof(struct htab_elem) +
  166. round_up(htab->map.key_size, 8);
  167. if (percpu)
  168. htab->elem_size += sizeof(void *);
  169. else
  170. htab->elem_size += round_up(htab->map.value_size, 8);
  171. /* prevent zero size kmalloc and check for u32 overflow */
  172. if (htab->n_buckets == 0 ||
  173. htab->n_buckets > U32_MAX / sizeof(struct bucket))
  174. goto free_htab;
  175. cost = (u64) htab->n_buckets * sizeof(struct bucket) +
  176. (u64) htab->elem_size * htab->map.max_entries;
  177. if (percpu)
  178. cost += (u64) round_up(htab->map.value_size, 8) *
  179. num_possible_cpus() * htab->map.max_entries;
  180. else
  181. cost += (u64) htab->elem_size * num_possible_cpus();
  182. if (cost >= U32_MAX - PAGE_SIZE)
  183. /* make sure page count doesn't overflow */
  184. goto free_htab;
  185. htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
  186. /* if map size is larger than memlock limit, reject it early */
  187. err = bpf_map_precharge_memlock(htab->map.pages);
  188. if (err)
  189. goto free_htab;
  190. err = -ENOMEM;
  191. htab->buckets = bpf_map_area_alloc(htab->n_buckets *
  192. sizeof(struct bucket));
  193. if (!htab->buckets)
  194. goto free_htab;
  195. for (i = 0; i < htab->n_buckets; i++) {
  196. INIT_HLIST_HEAD(&htab->buckets[i].head);
  197. raw_spin_lock_init(&htab->buckets[i].lock);
  198. }
  199. if (!percpu) {
  200. err = alloc_extra_elems(htab);
  201. if (err)
  202. goto free_buckets;
  203. }
  204. if (!(attr->map_flags & BPF_F_NO_PREALLOC)) {
  205. err = prealloc_elems_and_freelist(htab);
  206. if (err)
  207. goto free_extra_elems;
  208. }
  209. return &htab->map;
  210. free_extra_elems:
  211. free_percpu(htab->extra_elems);
  212. free_buckets:
  213. bpf_map_area_free(htab->buckets);
  214. free_htab:
  215. kfree(htab);
  216. return ERR_PTR(err);
  217. }
  218. static inline u32 htab_map_hash(const void *key, u32 key_len)
  219. {
  220. return jhash(key, key_len, 0);
  221. }
  222. static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
  223. {
  224. return &htab->buckets[hash & (htab->n_buckets - 1)];
  225. }
  226. static inline struct hlist_head *select_bucket(struct bpf_htab *htab, u32 hash)
  227. {
  228. return &__select_bucket(htab, hash)->head;
  229. }
  230. static struct htab_elem *lookup_elem_raw(struct hlist_head *head, u32 hash,
  231. void *key, u32 key_size)
  232. {
  233. struct htab_elem *l;
  234. hlist_for_each_entry_rcu(l, head, hash_node)
  235. if (l->hash == hash && !memcmp(&l->key, key, key_size))
  236. return l;
  237. return NULL;
  238. }
  239. /* Called from syscall or from eBPF program */
  240. static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
  241. {
  242. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  243. struct hlist_head *head;
  244. struct htab_elem *l;
  245. u32 hash, key_size;
  246. /* Must be called with rcu_read_lock. */
  247. WARN_ON_ONCE(!rcu_read_lock_held());
  248. key_size = map->key_size;
  249. hash = htab_map_hash(key, key_size);
  250. head = select_bucket(htab, hash);
  251. l = lookup_elem_raw(head, hash, key, key_size);
  252. return l;
  253. }
  254. static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
  255. {
  256. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  257. if (l)
  258. return l->key + round_up(map->key_size, 8);
  259. return NULL;
  260. }
  261. /* Called from syscall */
  262. static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  263. {
  264. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  265. struct hlist_head *head;
  266. struct htab_elem *l, *next_l;
  267. u32 hash, key_size;
  268. int i = 0;
  269. WARN_ON_ONCE(!rcu_read_lock_held());
  270. key_size = map->key_size;
  271. if (!key)
  272. goto find_first_elem;
  273. hash = htab_map_hash(key, key_size);
  274. head = select_bucket(htab, hash);
  275. /* lookup the key */
  276. l = lookup_elem_raw(head, hash, key, key_size);
  277. if (!l)
  278. goto find_first_elem;
  279. /* key was found, get next key in the same bucket */
  280. next_l = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&l->hash_node)),
  281. struct htab_elem, hash_node);
  282. if (next_l) {
  283. /* if next elem in this hash list is non-zero, just return it */
  284. memcpy(next_key, next_l->key, key_size);
  285. return 0;
  286. }
  287. /* no more elements in this hash list, go to the next bucket */
  288. i = hash & (htab->n_buckets - 1);
  289. i++;
  290. find_first_elem:
  291. /* iterate over buckets */
  292. for (; i < htab->n_buckets; i++) {
  293. head = select_bucket(htab, i);
  294. /* pick first element in the bucket */
  295. next_l = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
  296. struct htab_elem, hash_node);
  297. if (next_l) {
  298. /* if it's not empty, just return it */
  299. memcpy(next_key, next_l->key, key_size);
  300. return 0;
  301. }
  302. }
  303. /* iterated over all buckets and all elements */
  304. return -ENOENT;
  305. }
  306. static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
  307. {
  308. if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
  309. free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
  310. kfree(l);
  311. }
  312. static void htab_elem_free_rcu(struct rcu_head *head)
  313. {
  314. struct htab_elem *l = container_of(head, struct htab_elem, rcu);
  315. struct bpf_htab *htab = l->htab;
  316. /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
  317. * we're calling kfree, otherwise deadlock is possible if kprobes
  318. * are placed somewhere inside of slub
  319. */
  320. preempt_disable();
  321. __this_cpu_inc(bpf_prog_active);
  322. htab_elem_free(htab, l);
  323. __this_cpu_dec(bpf_prog_active);
  324. preempt_enable();
  325. }
  326. static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
  327. {
  328. if (l->state == HTAB_EXTRA_ELEM_USED) {
  329. l->state = HTAB_EXTRA_ELEM_FREE;
  330. return;
  331. }
  332. if (!(htab->map.map_flags & BPF_F_NO_PREALLOC)) {
  333. pcpu_freelist_push(&htab->freelist, &l->fnode);
  334. } else {
  335. atomic_dec(&htab->count);
  336. l->htab = htab;
  337. call_rcu(&l->rcu, htab_elem_free_rcu);
  338. }
  339. }
  340. static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
  341. void *value, u32 key_size, u32 hash,
  342. bool percpu, bool onallcpus,
  343. bool old_elem_exists)
  344. {
  345. u32 size = htab->map.value_size;
  346. bool prealloc = !(htab->map.map_flags & BPF_F_NO_PREALLOC);
  347. struct htab_elem *l_new;
  348. void __percpu *pptr;
  349. int err = 0;
  350. if (prealloc) {
  351. l_new = (struct htab_elem *)pcpu_freelist_pop(&htab->freelist);
  352. if (!l_new)
  353. err = -E2BIG;
  354. } else {
  355. if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
  356. atomic_dec(&htab->count);
  357. err = -E2BIG;
  358. } else {
  359. l_new = kmalloc(htab->elem_size,
  360. GFP_ATOMIC | __GFP_NOWARN);
  361. if (!l_new)
  362. return ERR_PTR(-ENOMEM);
  363. }
  364. }
  365. if (err) {
  366. if (!old_elem_exists)
  367. return ERR_PTR(err);
  368. /* if we're updating the existing element and the hash table
  369. * is full, use per-cpu extra elems
  370. */
  371. l_new = this_cpu_ptr(htab->extra_elems);
  372. if (l_new->state != HTAB_EXTRA_ELEM_FREE)
  373. return ERR_PTR(-E2BIG);
  374. l_new->state = HTAB_EXTRA_ELEM_USED;
  375. } else {
  376. l_new->state = HTAB_NOT_AN_EXTRA_ELEM;
  377. }
  378. memcpy(l_new->key, key, key_size);
  379. if (percpu) {
  380. /* round up value_size to 8 bytes */
  381. size = round_up(size, 8);
  382. if (prealloc) {
  383. pptr = htab_elem_get_ptr(l_new, key_size);
  384. } else {
  385. /* alloc_percpu zero-fills */
  386. pptr = __alloc_percpu_gfp(size, 8,
  387. GFP_ATOMIC | __GFP_NOWARN);
  388. if (!pptr) {
  389. kfree(l_new);
  390. return ERR_PTR(-ENOMEM);
  391. }
  392. }
  393. if (!onallcpus) {
  394. /* copy true value_size bytes */
  395. memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
  396. } else {
  397. int off = 0, cpu;
  398. for_each_possible_cpu(cpu) {
  399. bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
  400. value + off, size);
  401. off += size;
  402. }
  403. }
  404. if (!prealloc)
  405. htab_elem_set_ptr(l_new, key_size, pptr);
  406. } else {
  407. memcpy(l_new->key + round_up(key_size, 8), value, size);
  408. }
  409. l_new->hash = hash;
  410. return l_new;
  411. }
  412. static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
  413. u64 map_flags)
  414. {
  415. if (l_old && map_flags == BPF_NOEXIST)
  416. /* elem already exists */
  417. return -EEXIST;
  418. if (!l_old && map_flags == BPF_EXIST)
  419. /* elem doesn't exist, cannot update it */
  420. return -ENOENT;
  421. return 0;
  422. }
  423. /* Called from syscall or from eBPF program */
  424. static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
  425. u64 map_flags)
  426. {
  427. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  428. struct htab_elem *l_new = NULL, *l_old;
  429. struct hlist_head *head;
  430. unsigned long flags;
  431. struct bucket *b;
  432. u32 key_size, hash;
  433. int ret;
  434. if (unlikely(map_flags > BPF_EXIST))
  435. /* unknown flags */
  436. return -EINVAL;
  437. WARN_ON_ONCE(!rcu_read_lock_held());
  438. key_size = map->key_size;
  439. hash = htab_map_hash(key, key_size);
  440. b = __select_bucket(htab, hash);
  441. head = &b->head;
  442. /* bpf_map_update_elem() can be called in_irq() */
  443. raw_spin_lock_irqsave(&b->lock, flags);
  444. l_old = lookup_elem_raw(head, hash, key, key_size);
  445. ret = check_flags(htab, l_old, map_flags);
  446. if (ret)
  447. goto err;
  448. l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
  449. !!l_old);
  450. if (IS_ERR(l_new)) {
  451. /* all pre-allocated elements are in use or memory exhausted */
  452. ret = PTR_ERR(l_new);
  453. goto err;
  454. }
  455. /* add new element to the head of the list, so that
  456. * concurrent search will find it before old elem
  457. */
  458. hlist_add_head_rcu(&l_new->hash_node, head);
  459. if (l_old) {
  460. hlist_del_rcu(&l_old->hash_node);
  461. free_htab_elem(htab, l_old);
  462. }
  463. ret = 0;
  464. err:
  465. raw_spin_unlock_irqrestore(&b->lock, flags);
  466. return ret;
  467. }
  468. static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
  469. void *value, u64 map_flags,
  470. bool onallcpus)
  471. {
  472. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  473. struct htab_elem *l_new = NULL, *l_old;
  474. struct hlist_head *head;
  475. unsigned long flags;
  476. struct bucket *b;
  477. u32 key_size, hash;
  478. int ret;
  479. if (unlikely(map_flags > BPF_EXIST))
  480. /* unknown flags */
  481. return -EINVAL;
  482. WARN_ON_ONCE(!rcu_read_lock_held());
  483. key_size = map->key_size;
  484. hash = htab_map_hash(key, key_size);
  485. b = __select_bucket(htab, hash);
  486. head = &b->head;
  487. /* bpf_map_update_elem() can be called in_irq() */
  488. raw_spin_lock_irqsave(&b->lock, flags);
  489. l_old = lookup_elem_raw(head, hash, key, key_size);
  490. ret = check_flags(htab, l_old, map_flags);
  491. if (ret)
  492. goto err;
  493. if (l_old) {
  494. void __percpu *pptr = htab_elem_get_ptr(l_old, key_size);
  495. u32 size = htab->map.value_size;
  496. /* per-cpu hash map can update value in-place */
  497. if (!onallcpus) {
  498. memcpy(this_cpu_ptr(pptr), value, size);
  499. } else {
  500. int off = 0, cpu;
  501. size = round_up(size, 8);
  502. for_each_possible_cpu(cpu) {
  503. bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
  504. value + off, size);
  505. off += size;
  506. }
  507. }
  508. } else {
  509. l_new = alloc_htab_elem(htab, key, value, key_size,
  510. hash, true, onallcpus, false);
  511. if (IS_ERR(l_new)) {
  512. ret = PTR_ERR(l_new);
  513. goto err;
  514. }
  515. hlist_add_head_rcu(&l_new->hash_node, head);
  516. }
  517. ret = 0;
  518. err:
  519. raw_spin_unlock_irqrestore(&b->lock, flags);
  520. return ret;
  521. }
  522. static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
  523. void *value, u64 map_flags)
  524. {
  525. return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
  526. }
  527. /* Called from syscall or from eBPF program */
  528. static int htab_map_delete_elem(struct bpf_map *map, void *key)
  529. {
  530. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  531. struct hlist_head *head;
  532. struct bucket *b;
  533. struct htab_elem *l;
  534. unsigned long flags;
  535. u32 hash, key_size;
  536. int ret = -ENOENT;
  537. WARN_ON_ONCE(!rcu_read_lock_held());
  538. key_size = map->key_size;
  539. hash = htab_map_hash(key, key_size);
  540. b = __select_bucket(htab, hash);
  541. head = &b->head;
  542. raw_spin_lock_irqsave(&b->lock, flags);
  543. l = lookup_elem_raw(head, hash, key, key_size);
  544. if (l) {
  545. hlist_del_rcu(&l->hash_node);
  546. free_htab_elem(htab, l);
  547. ret = 0;
  548. }
  549. raw_spin_unlock_irqrestore(&b->lock, flags);
  550. return ret;
  551. }
  552. static void delete_all_elements(struct bpf_htab *htab)
  553. {
  554. int i;
  555. for (i = 0; i < htab->n_buckets; i++) {
  556. struct hlist_head *head = select_bucket(htab, i);
  557. struct hlist_node *n;
  558. struct htab_elem *l;
  559. hlist_for_each_entry_safe(l, n, head, hash_node) {
  560. hlist_del_rcu(&l->hash_node);
  561. if (l->state != HTAB_EXTRA_ELEM_USED)
  562. htab_elem_free(htab, l);
  563. }
  564. }
  565. }
  566. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  567. static void htab_map_free(struct bpf_map *map)
  568. {
  569. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  570. /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  571. * so the programs (can be more than one that used this map) were
  572. * disconnected from events. Wait for outstanding critical sections in
  573. * these programs to complete
  574. */
  575. synchronize_rcu();
  576. /* some of free_htab_elem() callbacks for elements of this map may
  577. * not have executed. Wait for them.
  578. */
  579. rcu_barrier();
  580. if (htab->map.map_flags & BPF_F_NO_PREALLOC) {
  581. delete_all_elements(htab);
  582. } else {
  583. htab_free_elems(htab);
  584. pcpu_freelist_destroy(&htab->freelist);
  585. }
  586. free_percpu(htab->extra_elems);
  587. bpf_map_area_free(htab->buckets);
  588. kfree(htab);
  589. }
  590. static const struct bpf_map_ops htab_ops = {
  591. .map_alloc = htab_map_alloc,
  592. .map_free = htab_map_free,
  593. .map_get_next_key = htab_map_get_next_key,
  594. .map_lookup_elem = htab_map_lookup_elem,
  595. .map_update_elem = htab_map_update_elem,
  596. .map_delete_elem = htab_map_delete_elem,
  597. };
  598. static struct bpf_map_type_list htab_type __read_mostly = {
  599. .ops = &htab_ops,
  600. .type = BPF_MAP_TYPE_HASH,
  601. };
  602. /* Called from eBPF program */
  603. static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
  604. {
  605. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  606. if (l)
  607. return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
  608. else
  609. return NULL;
  610. }
  611. int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
  612. {
  613. struct htab_elem *l;
  614. void __percpu *pptr;
  615. int ret = -ENOENT;
  616. int cpu, off = 0;
  617. u32 size;
  618. /* per_cpu areas are zero-filled and bpf programs can only
  619. * access 'value_size' of them, so copying rounded areas
  620. * will not leak any kernel data
  621. */
  622. size = round_up(map->value_size, 8);
  623. rcu_read_lock();
  624. l = __htab_map_lookup_elem(map, key);
  625. if (!l)
  626. goto out;
  627. pptr = htab_elem_get_ptr(l, map->key_size);
  628. for_each_possible_cpu(cpu) {
  629. bpf_long_memcpy(value + off,
  630. per_cpu_ptr(pptr, cpu), size);
  631. off += size;
  632. }
  633. ret = 0;
  634. out:
  635. rcu_read_unlock();
  636. return ret;
  637. }
  638. int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
  639. u64 map_flags)
  640. {
  641. int ret;
  642. rcu_read_lock();
  643. ret = __htab_percpu_map_update_elem(map, key, value, map_flags, true);
  644. rcu_read_unlock();
  645. return ret;
  646. }
  647. static const struct bpf_map_ops htab_percpu_ops = {
  648. .map_alloc = htab_map_alloc,
  649. .map_free = htab_map_free,
  650. .map_get_next_key = htab_map_get_next_key,
  651. .map_lookup_elem = htab_percpu_map_lookup_elem,
  652. .map_update_elem = htab_percpu_map_update_elem,
  653. .map_delete_elem = htab_map_delete_elem,
  654. };
  655. static struct bpf_map_type_list htab_percpu_type __read_mostly = {
  656. .ops = &htab_percpu_ops,
  657. .type = BPF_MAP_TYPE_PERCPU_HASH,
  658. };
  659. static int __init register_htab_map(void)
  660. {
  661. bpf_register_map_type(&htab_type);
  662. bpf_register_map_type(&htab_percpu_type);
  663. return 0;
  664. }
  665. late_initcall(register_htab_map);