hashtab.c 18 KB

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