hashtab.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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 <linux/vmalloc.h>
  17. #include "percpu_freelist.h"
  18. struct bucket {
  19. struct hlist_head head;
  20. raw_spinlock_t lock;
  21. };
  22. struct bpf_htab {
  23. struct bpf_map map;
  24. struct bucket *buckets;
  25. void *elems;
  26. struct pcpu_freelist freelist;
  27. void __percpu *extra_elems;
  28. atomic_t count; /* number of elements in this hashtable */
  29. u32 n_buckets; /* number of hash buckets */
  30. u32 elem_size; /* size of each element in bytes */
  31. };
  32. enum extra_elem_state {
  33. HTAB_NOT_AN_EXTRA_ELEM = 0,
  34. HTAB_EXTRA_ELEM_FREE,
  35. HTAB_EXTRA_ELEM_USED
  36. };
  37. /* each htab element is struct htab_elem + key + value */
  38. struct htab_elem {
  39. union {
  40. struct hlist_node hash_node;
  41. struct bpf_htab *htab;
  42. struct pcpu_freelist_node fnode;
  43. };
  44. union {
  45. struct rcu_head rcu;
  46. enum extra_elem_state state;
  47. };
  48. u32 hash;
  49. char key[0] __aligned(8);
  50. };
  51. static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
  52. void __percpu *pptr)
  53. {
  54. *(void __percpu **)(l->key + key_size) = pptr;
  55. }
  56. static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
  57. {
  58. return *(void __percpu **)(l->key + key_size);
  59. }
  60. static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
  61. {
  62. return (struct htab_elem *) (htab->elems + i * htab->elem_size);
  63. }
  64. static void htab_free_elems(struct bpf_htab *htab)
  65. {
  66. int i;
  67. if (htab->map.map_type != BPF_MAP_TYPE_PERCPU_HASH)
  68. goto free_elems;
  69. for (i = 0; i < htab->map.max_entries; i++) {
  70. void __percpu *pptr;
  71. pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
  72. htab->map.key_size);
  73. free_percpu(pptr);
  74. }
  75. free_elems:
  76. vfree(htab->elems);
  77. }
  78. static int prealloc_elems_and_freelist(struct bpf_htab *htab)
  79. {
  80. int err = -ENOMEM, i;
  81. htab->elems = vzalloc(htab->elem_size * 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 = kmalloc_array(htab->n_buckets, sizeof(struct bucket),
  192. GFP_USER | __GFP_NOWARN);
  193. if (!htab->buckets) {
  194. htab->buckets = vmalloc(htab->n_buckets * sizeof(struct bucket));
  195. if (!htab->buckets)
  196. goto free_htab;
  197. }
  198. for (i = 0; i < htab->n_buckets; i++) {
  199. INIT_HLIST_HEAD(&htab->buckets[i].head);
  200. raw_spin_lock_init(&htab->buckets[i].lock);
  201. }
  202. if (!percpu) {
  203. err = alloc_extra_elems(htab);
  204. if (err)
  205. goto free_buckets;
  206. }
  207. if (!(attr->map_flags & BPF_F_NO_PREALLOC)) {
  208. err = prealloc_elems_and_freelist(htab);
  209. if (err)
  210. goto free_extra_elems;
  211. }
  212. return &htab->map;
  213. free_extra_elems:
  214. free_percpu(htab->extra_elems);
  215. free_buckets:
  216. kvfree(htab->buckets);
  217. free_htab:
  218. kfree(htab);
  219. return ERR_PTR(err);
  220. }
  221. static inline u32 htab_map_hash(const void *key, u32 key_len)
  222. {
  223. return jhash(key, key_len, 0);
  224. }
  225. static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
  226. {
  227. return &htab->buckets[hash & (htab->n_buckets - 1)];
  228. }
  229. static inline struct hlist_head *select_bucket(struct bpf_htab *htab, u32 hash)
  230. {
  231. return &__select_bucket(htab, hash)->head;
  232. }
  233. static struct htab_elem *lookup_elem_raw(struct hlist_head *head, u32 hash,
  234. void *key, u32 key_size)
  235. {
  236. struct htab_elem *l;
  237. hlist_for_each_entry_rcu(l, head, hash_node)
  238. if (l->hash == hash && !memcmp(&l->key, key, key_size))
  239. return l;
  240. return NULL;
  241. }
  242. /* Called from syscall or from eBPF program */
  243. static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
  244. {
  245. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  246. struct hlist_head *head;
  247. struct htab_elem *l;
  248. u32 hash, key_size;
  249. /* Must be called with rcu_read_lock. */
  250. WARN_ON_ONCE(!rcu_read_lock_held());
  251. key_size = map->key_size;
  252. hash = htab_map_hash(key, key_size);
  253. head = select_bucket(htab, hash);
  254. l = lookup_elem_raw(head, hash, key, key_size);
  255. return l;
  256. }
  257. static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
  258. {
  259. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  260. if (l)
  261. return l->key + round_up(map->key_size, 8);
  262. return NULL;
  263. }
  264. /* Called from syscall */
  265. static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  266. {
  267. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  268. struct hlist_head *head;
  269. struct htab_elem *l, *next_l;
  270. u32 hash, key_size;
  271. int i;
  272. WARN_ON_ONCE(!rcu_read_lock_held());
  273. key_size = map->key_size;
  274. hash = htab_map_hash(key, key_size);
  275. head = select_bucket(htab, hash);
  276. /* lookup the key */
  277. l = lookup_elem_raw(head, hash, key, key_size);
  278. if (!l) {
  279. i = 0;
  280. goto find_first_elem;
  281. }
  282. /* key was found, get next key in the same bucket */
  283. next_l = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&l->hash_node)),
  284. struct htab_elem, hash_node);
  285. if (next_l) {
  286. /* if next elem in this hash list is non-zero, just return it */
  287. memcpy(next_key, next_l->key, key_size);
  288. return 0;
  289. }
  290. /* no more elements in this hash list, go to the next bucket */
  291. i = hash & (htab->n_buckets - 1);
  292. i++;
  293. find_first_elem:
  294. /* iterate over buckets */
  295. for (; i < htab->n_buckets; i++) {
  296. head = select_bucket(htab, i);
  297. /* pick first element in the bucket */
  298. next_l = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
  299. struct htab_elem, hash_node);
  300. if (next_l) {
  301. /* if it's not empty, just return it */
  302. memcpy(next_key, next_l->key, key_size);
  303. return 0;
  304. }
  305. }
  306. /* iterated over all buckets and all elements */
  307. return -ENOENT;
  308. }
  309. static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
  310. {
  311. if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
  312. free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
  313. kfree(l);
  314. }
  315. static void htab_elem_free_rcu(struct rcu_head *head)
  316. {
  317. struct htab_elem *l = container_of(head, struct htab_elem, rcu);
  318. struct bpf_htab *htab = l->htab;
  319. /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
  320. * we're calling kfree, otherwise deadlock is possible if kprobes
  321. * are placed somewhere inside of slub
  322. */
  323. preempt_disable();
  324. __this_cpu_inc(bpf_prog_active);
  325. htab_elem_free(htab, l);
  326. __this_cpu_dec(bpf_prog_active);
  327. preempt_enable();
  328. }
  329. static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
  330. {
  331. if (l->state == HTAB_EXTRA_ELEM_USED) {
  332. l->state = HTAB_EXTRA_ELEM_FREE;
  333. return;
  334. }
  335. if (!(htab->map.map_flags & BPF_F_NO_PREALLOC)) {
  336. pcpu_freelist_push(&htab->freelist, &l->fnode);
  337. } else {
  338. atomic_dec(&htab->count);
  339. l->htab = htab;
  340. call_rcu(&l->rcu, htab_elem_free_rcu);
  341. }
  342. }
  343. static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
  344. void *value, u32 key_size, u32 hash,
  345. bool percpu, bool onallcpus,
  346. bool old_elem_exists)
  347. {
  348. u32 size = htab->map.value_size;
  349. bool prealloc = !(htab->map.map_flags & BPF_F_NO_PREALLOC);
  350. struct htab_elem *l_new;
  351. void __percpu *pptr;
  352. int err = 0;
  353. if (prealloc) {
  354. l_new = (struct htab_elem *)pcpu_freelist_pop(&htab->freelist);
  355. if (!l_new)
  356. err = -E2BIG;
  357. } else {
  358. if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
  359. atomic_dec(&htab->count);
  360. err = -E2BIG;
  361. } else {
  362. l_new = kmalloc(htab->elem_size,
  363. GFP_ATOMIC | __GFP_NOWARN);
  364. if (!l_new)
  365. return ERR_PTR(-ENOMEM);
  366. }
  367. }
  368. if (err) {
  369. if (!old_elem_exists)
  370. return ERR_PTR(err);
  371. /* if we're updating the existing element and the hash table
  372. * is full, use per-cpu extra elems
  373. */
  374. l_new = this_cpu_ptr(htab->extra_elems);
  375. if (l_new->state != HTAB_EXTRA_ELEM_FREE)
  376. return ERR_PTR(-E2BIG);
  377. l_new->state = HTAB_EXTRA_ELEM_USED;
  378. } else {
  379. l_new->state = HTAB_NOT_AN_EXTRA_ELEM;
  380. }
  381. memcpy(l_new->key, key, key_size);
  382. if (percpu) {
  383. /* round up value_size to 8 bytes */
  384. size = round_up(size, 8);
  385. if (prealloc) {
  386. pptr = htab_elem_get_ptr(l_new, key_size);
  387. } else {
  388. /* alloc_percpu zero-fills */
  389. pptr = __alloc_percpu_gfp(size, 8,
  390. GFP_ATOMIC | __GFP_NOWARN);
  391. if (!pptr) {
  392. kfree(l_new);
  393. return ERR_PTR(-ENOMEM);
  394. }
  395. }
  396. if (!onallcpus) {
  397. /* copy true value_size bytes */
  398. memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
  399. } else {
  400. int off = 0, cpu;
  401. for_each_possible_cpu(cpu) {
  402. bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
  403. value + off, size);
  404. off += size;
  405. }
  406. }
  407. if (!prealloc)
  408. htab_elem_set_ptr(l_new, key_size, pptr);
  409. } else {
  410. memcpy(l_new->key + round_up(key_size, 8), value, size);
  411. }
  412. l_new->hash = hash;
  413. return l_new;
  414. }
  415. static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
  416. u64 map_flags)
  417. {
  418. if (l_old && map_flags == BPF_NOEXIST)
  419. /* elem already exists */
  420. return -EEXIST;
  421. if (!l_old && map_flags == BPF_EXIST)
  422. /* elem doesn't exist, cannot update it */
  423. return -ENOENT;
  424. return 0;
  425. }
  426. /* Called from syscall or from eBPF program */
  427. static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
  428. u64 map_flags)
  429. {
  430. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  431. struct htab_elem *l_new = NULL, *l_old;
  432. struct hlist_head *head;
  433. unsigned long flags;
  434. struct bucket *b;
  435. u32 key_size, hash;
  436. int ret;
  437. if (unlikely(map_flags > BPF_EXIST))
  438. /* unknown flags */
  439. return -EINVAL;
  440. WARN_ON_ONCE(!rcu_read_lock_held());
  441. key_size = map->key_size;
  442. hash = htab_map_hash(key, key_size);
  443. b = __select_bucket(htab, hash);
  444. head = &b->head;
  445. /* bpf_map_update_elem() can be called in_irq() */
  446. raw_spin_lock_irqsave(&b->lock, flags);
  447. l_old = lookup_elem_raw(head, hash, key, key_size);
  448. ret = check_flags(htab, l_old, map_flags);
  449. if (ret)
  450. goto err;
  451. l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
  452. !!l_old);
  453. if (IS_ERR(l_new)) {
  454. /* all pre-allocated elements are in use or memory exhausted */
  455. ret = PTR_ERR(l_new);
  456. goto err;
  457. }
  458. /* add new element to the head of the list, so that
  459. * concurrent search will find it before old elem
  460. */
  461. hlist_add_head_rcu(&l_new->hash_node, head);
  462. if (l_old) {
  463. hlist_del_rcu(&l_old->hash_node);
  464. free_htab_elem(htab, l_old);
  465. }
  466. ret = 0;
  467. err:
  468. raw_spin_unlock_irqrestore(&b->lock, flags);
  469. return ret;
  470. }
  471. static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
  472. void *value, u64 map_flags,
  473. bool onallcpus)
  474. {
  475. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  476. struct htab_elem *l_new = NULL, *l_old;
  477. struct hlist_head *head;
  478. unsigned long flags;
  479. struct bucket *b;
  480. u32 key_size, hash;
  481. int ret;
  482. if (unlikely(map_flags > BPF_EXIST))
  483. /* unknown flags */
  484. return -EINVAL;
  485. WARN_ON_ONCE(!rcu_read_lock_held());
  486. key_size = map->key_size;
  487. hash = htab_map_hash(key, key_size);
  488. b = __select_bucket(htab, hash);
  489. head = &b->head;
  490. /* bpf_map_update_elem() can be called in_irq() */
  491. raw_spin_lock_irqsave(&b->lock, flags);
  492. l_old = lookup_elem_raw(head, hash, key, key_size);
  493. ret = check_flags(htab, l_old, map_flags);
  494. if (ret)
  495. goto err;
  496. if (l_old) {
  497. void __percpu *pptr = htab_elem_get_ptr(l_old, key_size);
  498. u32 size = htab->map.value_size;
  499. /* per-cpu hash map can update value in-place */
  500. if (!onallcpus) {
  501. memcpy(this_cpu_ptr(pptr), value, size);
  502. } else {
  503. int off = 0, cpu;
  504. size = round_up(size, 8);
  505. for_each_possible_cpu(cpu) {
  506. bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
  507. value + off, size);
  508. off += size;
  509. }
  510. }
  511. } else {
  512. l_new = alloc_htab_elem(htab, key, value, key_size,
  513. hash, true, onallcpus, false);
  514. if (IS_ERR(l_new)) {
  515. ret = PTR_ERR(l_new);
  516. goto err;
  517. }
  518. hlist_add_head_rcu(&l_new->hash_node, head);
  519. }
  520. ret = 0;
  521. err:
  522. raw_spin_unlock_irqrestore(&b->lock, flags);
  523. return ret;
  524. }
  525. static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
  526. void *value, u64 map_flags)
  527. {
  528. return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
  529. }
  530. /* Called from syscall or from eBPF program */
  531. static int htab_map_delete_elem(struct bpf_map *map, void *key)
  532. {
  533. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  534. struct hlist_head *head;
  535. struct bucket *b;
  536. struct htab_elem *l;
  537. unsigned long flags;
  538. u32 hash, key_size;
  539. int ret = -ENOENT;
  540. WARN_ON_ONCE(!rcu_read_lock_held());
  541. key_size = map->key_size;
  542. hash = htab_map_hash(key, key_size);
  543. b = __select_bucket(htab, hash);
  544. head = &b->head;
  545. raw_spin_lock_irqsave(&b->lock, flags);
  546. l = lookup_elem_raw(head, hash, key, key_size);
  547. if (l) {
  548. hlist_del_rcu(&l->hash_node);
  549. free_htab_elem(htab, l);
  550. ret = 0;
  551. }
  552. raw_spin_unlock_irqrestore(&b->lock, flags);
  553. return ret;
  554. }
  555. static void delete_all_elements(struct bpf_htab *htab)
  556. {
  557. int i;
  558. for (i = 0; i < htab->n_buckets; i++) {
  559. struct hlist_head *head = select_bucket(htab, i);
  560. struct hlist_node *n;
  561. struct htab_elem *l;
  562. hlist_for_each_entry_safe(l, n, head, hash_node) {
  563. hlist_del_rcu(&l->hash_node);
  564. if (l->state != HTAB_EXTRA_ELEM_USED)
  565. htab_elem_free(htab, l);
  566. }
  567. }
  568. }
  569. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  570. static void htab_map_free(struct bpf_map *map)
  571. {
  572. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  573. /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  574. * so the programs (can be more than one that used this map) were
  575. * disconnected from events. Wait for outstanding critical sections in
  576. * these programs to complete
  577. */
  578. synchronize_rcu();
  579. /* some of free_htab_elem() callbacks for elements of this map may
  580. * not have executed. Wait for them.
  581. */
  582. rcu_barrier();
  583. if (htab->map.map_flags & BPF_F_NO_PREALLOC) {
  584. delete_all_elements(htab);
  585. } else {
  586. htab_free_elems(htab);
  587. pcpu_freelist_destroy(&htab->freelist);
  588. }
  589. free_percpu(htab->extra_elems);
  590. kvfree(htab->buckets);
  591. kfree(htab);
  592. }
  593. static const struct bpf_map_ops htab_ops = {
  594. .map_alloc = htab_map_alloc,
  595. .map_free = htab_map_free,
  596. .map_get_next_key = htab_map_get_next_key,
  597. .map_lookup_elem = htab_map_lookup_elem,
  598. .map_update_elem = htab_map_update_elem,
  599. .map_delete_elem = htab_map_delete_elem,
  600. };
  601. static struct bpf_map_type_list htab_type __read_mostly = {
  602. .ops = &htab_ops,
  603. .type = BPF_MAP_TYPE_HASH,
  604. };
  605. /* Called from eBPF program */
  606. static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
  607. {
  608. struct htab_elem *l = __htab_map_lookup_elem(map, key);
  609. if (l)
  610. return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
  611. else
  612. return NULL;
  613. }
  614. int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
  615. {
  616. struct htab_elem *l;
  617. void __percpu *pptr;
  618. int ret = -ENOENT;
  619. int cpu, off = 0;
  620. u32 size;
  621. /* per_cpu areas are zero-filled and bpf programs can only
  622. * access 'value_size' of them, so copying rounded areas
  623. * will not leak any kernel data
  624. */
  625. size = round_up(map->value_size, 8);
  626. rcu_read_lock();
  627. l = __htab_map_lookup_elem(map, key);
  628. if (!l)
  629. goto out;
  630. pptr = htab_elem_get_ptr(l, map->key_size);
  631. for_each_possible_cpu(cpu) {
  632. bpf_long_memcpy(value + off,
  633. per_cpu_ptr(pptr, cpu), size);
  634. off += size;
  635. }
  636. ret = 0;
  637. out:
  638. rcu_read_unlock();
  639. return ret;
  640. }
  641. int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
  642. u64 map_flags)
  643. {
  644. int ret;
  645. rcu_read_lock();
  646. ret = __htab_percpu_map_update_elem(map, key, value, map_flags, true);
  647. rcu_read_unlock();
  648. return ret;
  649. }
  650. static const struct bpf_map_ops htab_percpu_ops = {
  651. .map_alloc = htab_map_alloc,
  652. .map_free = htab_map_free,
  653. .map_get_next_key = htab_map_get_next_key,
  654. .map_lookup_elem = htab_percpu_map_lookup_elem,
  655. .map_update_elem = htab_percpu_map_update_elem,
  656. .map_delete_elem = htab_map_delete_elem,
  657. };
  658. static struct bpf_map_type_list htab_percpu_type __read_mostly = {
  659. .ops = &htab_percpu_ops,
  660. .type = BPF_MAP_TYPE_PERCPU_HASH,
  661. };
  662. static int __init register_htab_map(void)
  663. {
  664. bpf_register_map_type(&htab_type);
  665. bpf_register_map_type(&htab_percpu_type);
  666. return 0;
  667. }
  668. late_initcall(register_htab_map);